-
Notifications
You must be signed in to change notification settings - Fork 466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New analyzer: Do not call BeginInvoke on a delegate for .NET Core #2807 #2972
Conversation
@sharwell for review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need to update the detection of .NET Standard scenarios
src/Microsoft.NetCore.Analyzers/Core/Tasks/DoNotCallBeginInvokeOnDelegate.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.NetCore.Analyzers/Core/Tasks/DoNotCallBeginInvokeOnDelegate.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.NetCore.Analyzers/Core/Tasks/DoNotCallBeginInvokeOnDelegate.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.NetCore.Analyzers/UnitTests/Tasks/DoNotCallBeginInvokeOnDelegateTests.cs
Outdated
Show resolved
Hide resolved
How are we thinking about analyzers like this vs https://github.com/microsoft/dotnet-apiport/tree/dev/docs/VSExtension and https://docs.microsoft.com/en-us/dotnet/standard/analyzers/api-analyzer ? There are a bunch of cases of APIs that aren't 100% portable across either version of .NET or OS platform, and I'm not sure we want to be adding a new rule for each. Shouldn't we instead be investing in the aforementioned existing analyzers? cc: @terrajobst |
I forgot to add my open questions:
|
@duracellko Thanks for your PR. However, as per @stephentoub's comment, I think we need to first revisit if this is the correct repo for this analyzer and whether this is the space we want to invest in right now as there are large number of such APIs. |
src/Microsoft.NetCore.Analyzers/UnitTests/Tasks/DoNotCallBeginInvokeOnDelegateTests.cs
Outdated
Show resolved
Hide resolved
// This condition should be removed in future. | ||
// It only ensures that tests are successful, because tests compilate to .NET Framework. | ||
if (compilation.ReferencedAssemblyNames.Any(identity => string.Equals(identity.Name, "netstandard", StringComparison.OrdinalIgnoreCase))) | ||
{ | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💭 I think we should remove this from the start. With a small amount of work to complete dotnet/roslyn-sdk#329 we should be able to get this working properly.
3b2021d
to
b0b9d71
Compare
Marking as Blocked pending decision above |
Is this PR still active? If not, can we close out? |
internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId, | ||
s_localizableTitle, | ||
s_localizableMessage, | ||
DiagnosticCategory.Design, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Should this be in Interoperability
instead?
[DiagnosticAnalyzer(LanguageNames.CSharp, LanguageNames.VisualBasic)] | ||
public sealed class DoNotCallBeginInvokeOnDelegate : DiagnosticAnalyzer | ||
{ | ||
internal const string RuleId = "CA1069"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❓ Why would this not be an integral part of PlatformCompatibilityAnalyzer
?
private static DiagnosticResult GetCSResultAt(int line, int column) | ||
{ | ||
return VerifyCS.Diagnostic(DoNotCallBeginInvokeOnDelegate.RuleId) | ||
.WithMessage(MicrosoftNetCoreAnalyzersResources.DoNotCallBeginInvokeOnDelegateMessage) | ||
.WithLocation(line, column); | ||
} | ||
|
||
private static DiagnosticResult GetVBResultAt(int line, int column) | ||
{ | ||
return VerifyVB.Diagnostic(DoNotCallBeginInvokeOnDelegate.RuleId) | ||
.WithMessage(MicrosoftNetCoreAnalyzersResources.DoNotCallBeginInvokeOnDelegateMessage) | ||
.WithLocation(line, column); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 These helpers can now be replaced with markup syntax ([|...|]
)
using Microsoft.CodeAnalysis.Testing; | ||
using Xunit; | ||
|
||
using VerifyCS = Test.Utilities.CSharpSecurityCodeFixVerifier< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❕ These are not security analyzers
using VerifyCS = Test.Utilities.CSharpSecurityCodeFixVerifier< | |
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
📝 Previously, CSharpSecurityCodeFixVerifier
was used due to a limitation in the test library. This limitation no longer applies and CSharpCodeFixVerifier
will now work without the need for other workarounds.
Microsoft.NetCore.Analyzers.Tasks.DoNotCallBeginInvokeOnDelegate, | ||
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>; | ||
|
||
using VerifyVB = Test.Utilities.VisualBasicSecurityCodeFixVerifier< |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
using VerifyVB = Test.Utilities.VisualBasicSecurityCodeFixVerifier< | |
using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier< |
End Sub | ||
End Class | ||
"; | ||
await VerifyVB.VerifyAnalyzerAsync(code, GetVBResultAt(7, 9)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After the conversion to markup, it would be good to expand these tests:
await new VerifyVB.Test
{
ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard10,
TestCode = code,
}.RunAsync();
await new VerifyVB.Test
{
ReferenceAssemblies = ReferenceAssemblies.NetStandard.NetStandard20,
TestCode = code,
}.RunAsync();
await new VerifyVB.Test
{
ReferenceAssemblies = ReferenceAssemblies.NetCore.NetCoreApp31,
TestCode = code,
}.RunAsync();
await new VerifyVB.Test
{
ReferenceAssemblies = ReferenceAssemblies.Net.Net50,
TestCode = code,
}.RunAsync();
await new VerifyVB.Test
{
ReferenceAssemblies = ReferenceAssemblies.NetFramework.NetFramework472.Default,
TestState =
{
Sources = { code },
// Ignore markup; no diagnostics will be reported for .NET Framework
MarkupMode = MarkupMode.Ignore,
},
}.RunAsync();
Seems like there hasn't been any progress or response in a year and a half. I'll close it for now. Thanks for the efforts. |
Implements #2807
New analyzer: CA2069:DoNotCallBeginInvokeOnDelegate
Analyzer reports warning on a call of
BeginInvoke
method on Delegate. It reports only on compilation targeting .NET Standard or .NET Core, because BeginInvoke is not implemented in .NET Core.