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 @@ -135,13 +135,20 @@ public override void Initialize(AnalysisContext context)
context.RegisterOperationAction(context =>
{
var operation = (IConversionOperation)context.Operation;

if (operation.Type is INamedTypeSymbol { IsComImport: true })
{
IOperation operand = operation.Operand;
if (operand is IConversionOperation { Type.SpecialType: SpecialType.System_Object } objConversion)
{
operand = objConversion.Operand;
}
if (operand.Type is null)
{
// Some operations like the "null" literal expression don't have a type.
// These expressions definitely aren't a source-generated COM type, so we can skip them.
return;
}
foreach (var recognizer in sourceGeneratedComRecognizers)
{
if (recognizer(operand.Type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public async Task SetComObjectData()
public interface I
{
}

[GeneratedComClass]
public class C : I
{
Expand Down Expand Up @@ -63,12 +63,12 @@ public async Task GetComObjectData()
public interface I
{
}

[GeneratedComClass]
public class C : I
{
}

public static class Program
{
public static void Foo(I i)
Expand All @@ -95,13 +95,13 @@ public async Task ReleaseComObject()
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public interface I
{
}

[GeneratedComClass]
public class C : I
{
Expand Down Expand Up @@ -133,18 +133,18 @@ public async Task FinalReleaseComObject()
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public interface I
{
}

[GeneratedComClass]
public class C : I
{
}

public static class Program
{
public static void Foo(I i)
Expand All @@ -171,13 +171,13 @@ public async Task CreateAggregatedObject()
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public interface I
{
}

[GeneratedComClass]
public class C : I
{
Expand Down Expand Up @@ -269,7 +269,7 @@ public static void Foo(ComObject i)
""";

await VerifyAnalyzerAsync(source);
}
}

[Fact]
public async Task GetTypedObjectForIUnknown()
Expand Down Expand Up @@ -309,18 +309,18 @@ public async Task GetIUnknownForObject()
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public interface I
{
}

[GeneratedComClass]
public class C : I
{
}

public static class Program
{
public static void Foo(I i)
Expand All @@ -347,18 +347,18 @@ public async Task GetIDispatchForObject()
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public interface I
{
}

[GeneratedComClass]
public class C : I
{
}

public static class Program
{
public static void Foo(I i)
Expand All @@ -385,18 +385,18 @@ public async Task GetComInterfaceForObject()
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public interface I
{
}

[GeneratedComClass]
public class C : I
{
}

public static class Program
{
public static void Foo(I i)
Expand Down Expand Up @@ -432,13 +432,13 @@ public async Task CastsBetweenComImportAndGeneratedComTypes()
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public interface I
{
}

[GeneratedComClass]
public class C : I
{
Expand All @@ -449,7 +449,7 @@ public class C : I
public interface J
{
}

public static class Program
{
public static void Foo(I i)
Expand Down Expand Up @@ -483,18 +483,18 @@ public async Task GetObjectForIUnknown()
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[GeneratedComInterface]
[Guid("0B7171CD-04A3-41B6-AD10-FE86D52197DD")]
public interface I
{
}

[GeneratedComClass]
public class C : I
{
}

public static class Program
{
public static void Foo(nint i)
Expand All @@ -509,6 +509,28 @@ public static void Foo(nint i)
await VerifyAnalyzerAsync(source);
}

[Fact]
public async Task SetNullToComImportField()
{
string source = """
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.Marshalling;

[ComImport]
[Guid("0BADBF92-749A-44DB-9DA0-C8E2EEC783E2")]
public interface J
{
}

public class X
{
public static J j = null;
}
""";

await VerifyAnalyzerAsync(source);
}

private Task VerifyAnalyzerAsync(string source)
{
var test = new VerifyCS.Test
Expand Down