Skip to content

Commit 1e7b136

Browse files
committed
Address PR feedback
1 parent f6061f1 commit 1e7b136

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

src/NetAnalyzers/Core/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypes.cs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,35 @@ public override void Initialize(AnalysisContext context)
3636

3737
private static void OnCompilationStart(CompilationStartAnalysisContext context)
3838
{
39+
INamedTypeSymbol? comImportAttributeType = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeInteropServicesComImportAttribute);
40+
3941
var candidateTypes = PooledConcurrentSet<INamedTypeSymbol>.GetInstance(SymbolEqualityComparer.Default);
4042
var baseTypes = PooledConcurrentSet<INamedTypeSymbol>.GetInstance(SymbolEqualityComparer.Default);
4143

4244
context.RegisterSymbolAction(context =>
4345
{
4446
var type = (INamedTypeSymbol)context.Symbol;
4547

46-
if (type.TypeKind is TypeKind.Class && !type.IsAbstract && !type.IsStatic && !type.IsSealed && !type.IsExternallyVisible())
48+
if (type.TypeKind is TypeKind.Class &&
49+
!type.IsAbstract &&
50+
!type.IsStatic &&
51+
!type.IsSealed &&
52+
!type.IsExternallyVisible() &&
53+
(comImportAttributeType is null || !type.HasAttribute(comImportAttributeType)))
54+
{
4755
candidateTypes.Add(type);
56+
}
4857

49-
for (var baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType)
58+
for (INamedTypeSymbol? baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType)
59+
{
5060
baseTypes.Add(baseType.OriginalDefinition);
61+
}
5162

5263
}, SymbolKind.NamedType);
5364

5465
context.RegisterCompilationEndAction(context =>
5566
{
56-
foreach (var type in candidateTypes)
67+
foreach (INamedTypeSymbol type in candidateTypes)
5768
{
5869
if (!baseTypes.Contains(type.OriginalDefinition))
5970
{

src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Runtime/SealInternalTypesTests.cs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,30 @@ End Class
202202
#endregion
203203

204204
#region No Diagnostic
205+
[Fact]
206+
public Task PublicClassType_NoDiagnostic_CS()
207+
{
208+
string source = $"public class C {{ protected class P {{ }} }}";
209+
210+
return VerifyCS.VerifyCodeFixAsync(source, source);
211+
}
212+
213+
[Fact]
214+
public Task AlreadySealedType_NoDiagnostic_CS()
215+
{
216+
string source = $"internal sealed class C {{ }}";
217+
218+
return VerifyCS.VerifyCodeFixAsync(source, source);
219+
}
220+
221+
[Fact]
222+
public Task ComImportAttributedType_NoDiagnostic_CS()
223+
{
224+
string source = $"[System.Runtime.InteropServices.ComImport] [System.Runtime.InteropServices.Guid(\"E8D59775-E821-4D6C-B63D-BB0D969361DA\")] internal class C {{ }}";
225+
226+
return VerifyCS.VerifyCodeFixAsync(source, source);
227+
}
228+
205229
[Theory]
206230
[InlineData("interface I { }")]
207231
[InlineData("struct S { }")]
@@ -331,6 +355,37 @@ public Task PartialClass_ReportedAndFixedAtAllLocations_CS()
331355
return test.RunAsync();
332356
}
333357

358+
[Fact(Skip = "Changes are being applied to .g.cs file")]
359+
public Task PartialClass_OneGenerated_ReportedAndFixedAtAllNonGeneratedLocations_CS()
360+
{
361+
var test = new VerifyCS.Test
362+
{
363+
TestState =
364+
{
365+
Sources =
366+
{
367+
("File1.cs", @"internal class Base { }"),
368+
("File2.cs", @"internal partial class {|#0:Derived|} : Base { }"),
369+
("File3.g.cs", @"internal partial class {|#1:Derived|} : Base { }")
370+
},
371+
ExpectedDiagnostics =
372+
{
373+
VerifyCS.Diagnostic(Rule).WithArguments("Derived").WithLocation(0).WithLocation(1)
374+
}
375+
},
376+
FixedState =
377+
{
378+
Sources =
379+
{
380+
("File1.cs", @"internal class Base { }"),
381+
("File2.cs", @"internal sealed partial class {|#0:Derived|} : Base { }"),
382+
("File3.g.cs", @"internal partial class {|#1:Derived|} : Base { }")
383+
}
384+
}
385+
};
386+
return test.RunAsync();
387+
}
388+
334389
[Fact]
335390
public Task PartialClass_ReportedAndFixedAtAllLocations_VB()
336391
{

src/Utilities/Compiler/WellKnownTypeNames.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ internal static class WellKnownTypeNames
296296
public const string SystemRuntimeExceptionServicesHandleProcessCorruptedStateExceptionsAttribute = "System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute";
297297
public const string SystemRuntimeInteropServicesCharSet = "System.Runtime.InteropServices.CharSet";
298298
public const string SystemRuntimeInteropServicesCoClassAttribute = "System.Runtime.InteropServices.CoClassAttribute";
299+
public const string SystemRuntimeInteropServicesComImportAttribute = "System.Runtime.InteropServices.ComImportAttribute";
299300
public const string SystemRuntimeInteropServicesComSourceInterfacesAttribute = "System.Runtime.InteropServices.ComSourceInterfacesAttribute";
300301
public const string SystemRuntimeInteropServicesComVisibleAttribute = "System.Runtime.InteropServices.ComVisibleAttribute";
301302
public const string SystemRuntimeInteropServicesDefaultDllImportSearchPathsAttribute = "System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute";

0 commit comments

Comments
 (0)