Skip to content
Merged
Changes from 2 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 @@ -413,6 +413,60 @@ await VerifyCS.VerifyAnalyzerAsync(source,
.WithArguments("Method", "Test"));
}

[Theory]
[InlineData("StringMarshalling = StringMarshalling.Utf16")]
[InlineData("StringMarshalling = StringMarshalling.Utf8")]
[InlineData("")]
public async Task StringBuilderNotSupported_ReportsDiagnostic(string stringMarshallingArg)
Comment thread
jkoritzinsky marked this conversation as resolved.
{
string marshallingPart = string.IsNullOrEmpty(stringMarshallingArg)
? ""
: $", {stringMarshallingArg}";

// StringBuilder as a simple parameter
string source = $$"""

using System.Runtime.InteropServices;
using System.Text;
partial class Test
{
[LibraryImport("DoesNotExist"{{marshallingPart}})]
public static partial void Method(StringBuilder {|#0:sb|});
}
""";

await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(GeneratorDiagnostics.ParameterTypeNotSupported)
.WithLocation(0)
.WithArguments("System.Text.StringBuilder", "sb"));
}
Comment thread
jkoritzinsky marked this conversation as resolved.

[Theory]
[InlineData("StringMarshalling = StringMarshalling.Utf16")]
[InlineData("StringMarshalling = StringMarshalling.Utf8")]
public async Task StringBuilderNotSupported_WithStringParam_ReportsDiagnostic(string stringMarshallingArg)
{
// StringBuilder with [Out] alongside a string parameter (repro from dotnet/runtime#126687)
Comment thread
jkoritzinsky marked this conversation as resolved.
Outdated
string source = $$"""

using System.Runtime.InteropServices;
using System.Text;
partial class Test
{
[LibraryImport("DoesNotExist", {{stringMarshallingArg}})]
internal static partial int Method(
string volumeMountPoint,
[Out] StringBuilder {|#0:volumeName|},
int bufferLength);
}
""";

await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(GeneratorDiagnostics.ParameterTypeNotSupported)
.WithLocation(0)
.WithArguments("System.Text.StringBuilder", "volumeName"));
}

private static void VerifyDiagnostics(DiagnosticResult[] expectedDiagnostics, Diagnostic[] actualDiagnostics)
{
Assert.True(expectedDiagnostics.Length == actualDiagnostics.Length,
Expand Down
Loading