Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
stephentoub committed Apr 15, 2022
1 parent f6061f1 commit 1e7b136
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,35 @@ public override void Initialize(AnalysisContext context)

private static void OnCompilationStart(CompilationStartAnalysisContext context)
{
INamedTypeSymbol? comImportAttributeType = context.Compilation.GetOrCreateTypeByMetadataName(WellKnownTypeNames.SystemRuntimeInteropServicesComImportAttribute);

var candidateTypes = PooledConcurrentSet<INamedTypeSymbol>.GetInstance(SymbolEqualityComparer.Default);
var baseTypes = PooledConcurrentSet<INamedTypeSymbol>.GetInstance(SymbolEqualityComparer.Default);

context.RegisterSymbolAction(context =>
{
var type = (INamedTypeSymbol)context.Symbol;

if (type.TypeKind is TypeKind.Class && !type.IsAbstract && !type.IsStatic && !type.IsSealed && !type.IsExternallyVisible())
if (type.TypeKind is TypeKind.Class &&
!type.IsAbstract &&
!type.IsStatic &&
!type.IsSealed &&
!type.IsExternallyVisible() &&
(comImportAttributeType is null || !type.HasAttribute(comImportAttributeType)))
{
candidateTypes.Add(type);
}

for (var baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType)
for (INamedTypeSymbol? baseType = type.BaseType; baseType is not null; baseType = baseType.BaseType)
{
baseTypes.Add(baseType.OriginalDefinition);
}

}, SymbolKind.NamedType);

context.RegisterCompilationEndAction(context =>
{
foreach (var type in candidateTypes)
foreach (INamedTypeSymbol type in candidateTypes)
{
if (!baseTypes.Contains(type.OriginalDefinition))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,30 @@ End Class
#endregion

#region No Diagnostic
[Fact]
public Task PublicClassType_NoDiagnostic_CS()
{
string source = $"public class C {{ protected class P {{ }} }}";

return VerifyCS.VerifyCodeFixAsync(source, source);
}

[Fact]
public Task AlreadySealedType_NoDiagnostic_CS()
{
string source = $"internal sealed class C {{ }}";

return VerifyCS.VerifyCodeFixAsync(source, source);
}

[Fact]
public Task ComImportAttributedType_NoDiagnostic_CS()
{
string source = $"[System.Runtime.InteropServices.ComImport] [System.Runtime.InteropServices.Guid(\"E8D59775-E821-4D6C-B63D-BB0D969361DA\")] internal class C {{ }}";

return VerifyCS.VerifyCodeFixAsync(source, source);
}

[Theory]
[InlineData("interface I { }")]
[InlineData("struct S { }")]
Expand Down Expand Up @@ -331,6 +355,37 @@ public Task PartialClass_ReportedAndFixedAtAllLocations_CS()
return test.RunAsync();
}

[Fact(Skip = "Changes are being applied to .g.cs file")]
public Task PartialClass_OneGenerated_ReportedAndFixedAtAllNonGeneratedLocations_CS()
{
var test = new VerifyCS.Test
{
TestState =
{
Sources =
{
("File1.cs", @"internal class Base { }"),
("File2.cs", @"internal partial class {|#0:Derived|} : Base { }"),
("File3.g.cs", @"internal partial class {|#1:Derived|} : Base { }")
},
ExpectedDiagnostics =
{
VerifyCS.Diagnostic(Rule).WithArguments("Derived").WithLocation(0).WithLocation(1)
}
},
FixedState =
{
Sources =
{
("File1.cs", @"internal class Base { }"),
("File2.cs", @"internal sealed partial class {|#0:Derived|} : Base { }"),
("File3.g.cs", @"internal partial class {|#1:Derived|} : Base { }")
}
}
};
return test.RunAsync();
}

[Fact]
public Task PartialClass_ReportedAndFixedAtAllLocations_VB()
{
Expand Down
1 change: 1 addition & 0 deletions src/Utilities/Compiler/WellKnownTypeNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ internal static class WellKnownTypeNames
public const string SystemRuntimeExceptionServicesHandleProcessCorruptedStateExceptionsAttribute = "System.Runtime.ExceptionServices.HandleProcessCorruptedStateExceptionsAttribute";
public const string SystemRuntimeInteropServicesCharSet = "System.Runtime.InteropServices.CharSet";
public const string SystemRuntimeInteropServicesCoClassAttribute = "System.Runtime.InteropServices.CoClassAttribute";
public const string SystemRuntimeInteropServicesComImportAttribute = "System.Runtime.InteropServices.ComImportAttribute";
public const string SystemRuntimeInteropServicesComSourceInterfacesAttribute = "System.Runtime.InteropServices.ComSourceInterfacesAttribute";
public const string SystemRuntimeInteropServicesComVisibleAttribute = "System.Runtime.InteropServices.ComVisibleAttribute";
public const string SystemRuntimeInteropServicesDefaultDllImportSearchPathsAttribute = "System.Runtime.InteropServices.DefaultDllImportSearchPathsAttribute";
Expand Down

0 comments on commit 1e7b136

Please sign in to comment.