-
Notifications
You must be signed in to change notification settings - Fork 468
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
Implement the ModuleInitializerAttributeShouldNotBeUsedInLibraries analyzer #5347
Implement the ModuleInitializerAttributeShouldNotBeUsedInLibraries analyzer #5347
Conversation
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
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.
The analyzer doesn't really check whether we're in a library or not.
So the false positive rate can be high.
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Outdated
Show resolved
Hide resolved
We expect the Is it possible to check if the type is in a class library assembly? |
Yeah I think it's possible: |
Thanks! This will still allow the analyzer to run for web and mobile apps I presume, but it will remove false positives from console apps and windows apps, so that's still an improvement. |
I think it won't run for web apps. Not sure about mobile. |
Thanks for the thorough review, @Youssef1313! I've addressed all of your feedback if you want to take another look. |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
I forgot to run |
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
Codecov Report
@@ Coverage Diff @@
## release/6.0.1xx #5347 +/- ##
===================================================
+ Coverage 95.60% 95.62% +0.01%
===================================================
Files 1236 1238 +2
Lines 283895 284725 +830
Branches 17032 17083 +51
===================================================
+ Hits 271414 272261 +847
+ Misses 10179 10157 -22
- Partials 2302 2307 +5 |
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Show resolved
Hide resolved
...osoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibrariesTests.cs
Show resolved
Hide resolved
.../Microsoft.NetCore.Analyzers/Runtime/ModuleInitializerAttributeShouldNotBeUsedInLibraries.cs
Outdated
Show resolved
Hide resolved
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.
Left few NITs overall looks good, thanks!
Fixes dotnet/runtime#43328 by raising a diagnostic any time the
[ModuleInitializer]
attribute is applied to a method (in conformance with the C# language feature that supports this attribute).Per the API review of this analyzer, the analyzer will be defaulted to on as a warning.