Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override void Initialize(AnalysisContext context)

context.RegisterCompilationStartAction(ctx =>
{
var analyzerContext = new AnalyzerContext();
var analyzerContext = new AnalyzerContext(ctx.Compilation);

ctx.RegisterSymbolAction(analyzerContext.AnalyzeNamedTypeSymbol, SymbolKind.NamedType);
ctx.RegisterSymbolAction(analyzerContext.AnalyzePropertyOrFieldSymbol, SymbolKind.Property, SymbolKind.Field);
Expand Down Expand Up @@ -67,11 +67,13 @@ private static bool IsPotentialUnusedType(INamedTypeSymbol symbol, CancellationT
return true;
}

private sealed class AnalyzerContext
private sealed class AnalyzerContext(Compilation compilation)
{
private readonly List<ITypeSymbol> _potentialUnusedTypes = [];
private readonly HashSet<ITypeSymbol> _usedTypes = new(SymbolEqualityComparer.Default);

private INamedTypeSymbol? CoClassAttributeSymbol { get; } = compilation.GetBestTypeByMetadataName("System.Runtime.InteropServices.CoClassAttribute");

public void AnalyzeNamedTypeSymbol(SymbolAnalysisContext context)
{
var symbol = (INamedTypeSymbol)context.Symbol;
Expand All @@ -98,6 +100,24 @@ public void AnalyzeNamedTypeSymbol(SymbolAnalysisContext context)
}
}

// Track CoClass attribute on interfaces - the interface and CoClass implementation form a COM interop pair
if (symbol.TypeKind == TypeKind.Interface && CoClassAttributeSymbol is not null)
{
foreach (var attribute in symbol.GetAttributes())
{
if (attribute.AttributeClass.IsEqualTo(CoClassAttributeSymbol))
{
var attributeValue = attribute.ConstructorArguments.FirstOrDefault();
if (!attributeValue.IsNull && attributeValue.Kind == TypedConstantKind.Type && attributeValue.Value is ITypeSymbol coClassType)
{
// Mark both the interface and the CoClass implementation as used
AddUsedType(symbol);
AddUsedType(coClassType);
}
}
}
}

// Track types used in generic constraints
foreach (var typeParameter in symbol.TypeParameters)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,32 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Fact]
public async Task InterfaceWithCoClassAttribute_NoDiagnostic()
{
const string SourceCode = """
using System.Runtime.InteropServices;

[ComImport]
[Guid("00000000-0000-0000-0000-000000000001")]
[CoClass(typeof(FileSaveDialogRCW))]
internal interface NativeFileSaveDialog
{
}

[ComImport]
[ClassInterface(ClassInterfaceType.None)]
[Guid("00000000-0000-0000-0000-000000000002")]
internal sealed class FileSaveDialogRCW
{
}
""";

await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.ValidateAsync();
}

[Fact]
public async Task InternalStructUsedAsPointerInMethodParameter_NoDiagnostic()
Comment thread
meziantou marked this conversation as resolved.
{
Expand Down