Skip to content
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

Closed
wants to merge 8 commits into from

Conversation

duracellko
Copy link
Contributor

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.

@mavasani
Copy link
Contributor

@sharwell for review

Copy link
Member

@sharwell sharwell left a 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

@stephentoub
Copy link
Member

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

@duracellko
Copy link
Contributor Author

I forgot to add my open questions:

  • Is Rule ID correct?
  • Is Role category and analyzer class namespace correct?
  • Is there need to add the Rule into any other file in repository for documentation?

@mavasani
Copy link
Contributor

@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/Test.Utilities/VisualBasicCodeFixVerifier`2.cs Outdated Show resolved Hide resolved
Comment on lines 43 to 48
// 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;
}
Copy link
Member

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.

@sharwell
Copy link
Member

Marking as Blocked pending decision above

Base automatically changed from master to main March 5, 2021 02:20
@jinujoseph jinujoseph requested a review from a team as a code owner March 5, 2021 02:20
@mavasani
Copy link
Contributor

Is this PR still active? If not, can we close out?

internal static DiagnosticDescriptor Rule = new DiagnosticDescriptor(RuleId,
s_localizableTitle,
s_localizableMessage,
DiagnosticCategory.Design,
Copy link
Member

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";
Copy link
Member

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?

Comment on lines +642 to +654
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);
}
Copy link
Member

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<
Copy link
Member

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

Suggested change
using VerifyCS = Test.Utilities.CSharpSecurityCodeFixVerifier<
using VerifyCS = Test.Utilities.CSharpCodeFixVerifier<

Copy link
Member

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<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using VerifyVB = Test.Utilities.VisualBasicSecurityCodeFixVerifier<
using VerifyVB = Test.Utilities.VisualBasicCodeFixVerifier<

End Sub
End Class
";
await VerifyVB.VerifyAnalyzerAsync(code, GetVBResultAt(7, 9));
Copy link
Member

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();

@stephentoub
Copy link
Member

Is this PR still active? If not, can we close out?

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants