-
Notifications
You must be signed in to change notification settings - Fork 470
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
Seal internal private types analyzer #5594
Seal internal private types analyzer #5594
Conversation
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.Fixer.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.cs
Outdated
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypesTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypesTests.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.Fixer.cs
Show resolved
Hide resolved
Codecov Report
@@ Coverage Diff @@
## main #5594 +/- ##
==========================================
- Coverage 96.04% 96.03% -0.01%
==========================================
Files 1323 1326 +3
Lines 305370 305731 +361
Branches 9687 9693 +6
==========================================
+ Hits 293294 293621 +327
- Misses 9739 9774 +35
+ Partials 2337 2336 -1 |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.cs
Show resolved
Hide resolved
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.cs
Show resolved
Hide resolved
This means the logic will differ from CA1812 analyzer, which skips compilations which have IVT. I presume this was considered in the design meeting though. Lines 53 to 65 in e52a3f1
|
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.
Overall LGTM.
Note that the code fixer is never going to show up in VS as this is a compilation end diagnostic. It is still useful to retain it so a fix all can be applied from command line tooling, such as AnalyzerRunner or dotnet-format. In future, if we add some incremental analysis API such that compilation end analyzers and their code fixes can be enabled in the IDE, hopefully we will get the code fix to show up in VS.
Also calling out that compilation end diagnostics are essentially build-only diagnostics, which will only show up on build if the user bumps the severity to a warning or sets AnalysisMode to AllEnabledByDefault. They will only show up in live/intellisense diagnostics if full solution analysis mode is enabled. So basically, this will be disabled by default analyzer.
@NewellClark Can you please resolve the merge conflicts? |
@mavasani is this good to merge? |
It would be nice to add the test @Youssef1313 was mentioning regarding |
@pranavkm We need to resolve merge conflicts and respond to review feedback from @Youssef1313 and @Evangelink. This should be very close to merge. |
@ericstj ask me to post my remark here I think the analyzer should exclude classes declared as using System.Runtime.InteropServices;
_ = (IFileSaveDialog)new FileSaveDialogRCW(); // If FileSaveDialogRCW is sealed: error CS0030: Cannot convert type 'FileSaveDialogRCW' to 'IFileSaveDialog'
[ComImport]
[Guid("C0B4E2F3-BA21-4773-8DBA-335EC946EB8B")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[CoClass(typeof(FileSaveDialogRCW))]
internal interface IFileSaveDialog
{
}
[ComImport]
[ClassInterface(ClassInterfaceType.None)]
[Guid("C0B4E2F3-BA21-4773-8DBA-335EC946EB8B")]
class FileSaveDialogRCW // 👈 sealed is not valid here because of `(IFileSaveDialog)new NativeFileSaveDialog()`
{
} |
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/MicrosoftNetCoreAnalyzersResources.resx
Outdated
Show resolved
Hide resolved
@NewellClark, I'd like to see this merged. Are you still able to work on it? If not, I'll take it over. Thanks for your work on it! |
4b264fa
to
1e7b136
Compare
1e7b136
to
8c7b41d
Compare
8c7b41d
to
1ac1aad
Compare
src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.cs
Outdated
Show resolved
Hide resolved
…InternalTypes.cs Co-authored-by: Youssef Victor <youssefvictor00@gmail.com>
.NET 7 includes a new analyzer [1] that checks for internal types that can be sealed. We therefore need to seal all private classes that don't have derived types to avoid CA1852 compiler warnings on upgrading to .NET 7. There's probably a case for also sealing all public types that we don't intend to extend, but that can be deferred to a separate branch. [1] dotnet/roslyn-analyzers#5594
As mentioned in the previous commit, .NET 7 includes a new analyzer [1] that checks for internal types that can be sealed. Unfortunately that analyzer is currently generating false positives for generated `Program` classes [2] and therefore has to be suppressed manually. A fix [3] has already been committed to the Roslyn Analyzers repo so we should be able to remove this suppression once that fix has made its way into an SDK update. [1] dotnet/roslyn-analyzers#5594 [2] dotnet/roslyn-analyzers#6141 [3] dotnet/roslyn-analyzers#6278
Fix #49944.
As per the design meeting, this analyzer ignores
InternalsVisibleToAttribute
s.I've implemented a fixer, but due to this issue the code fixer won't show up in the IDE because it is reported at the end of compilation.
I've also found violations in dotnet/runtime (violations in src/tasks, violations in libs).