Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Runtime90357 6012 #6885

Merged
merged 7 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
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 @@ -65,6 +65,13 @@ public override void Initialize(AnalysisContext context)

int expectedStringFormatArgumentCount = GetFormattingArguments(stringFormat);

if (expectedStringFormatArgumentCount < 0)
{
// Malformed format specification
operationContext.ReportDiagnostic(operationContext.Operation.Syntax.CreateDiagnostic(Rule));
return;
}

// explicit parameter case
if (info.ExpectedStringFormatArgumentCount >= 0)
{
Expand All @@ -82,6 +89,11 @@ public override void Initialize(AnalysisContext context)

return;
}
else if (info.ExpectedStringFormatArgumentCount == -2)
{
// Not a formatting method, we only checked the format specification.
return;
}

// ensure argument is an array
IArgumentOperation paramsArgument = invocation.Arguments[info.FormatStringIndex + 1];
Expand Down Expand Up @@ -424,6 +436,13 @@ private bool TryGetFormatInfo(IMethodSymbol method, int formatIndex, [NotNullWhe

private static int GetExpectedNumberOfArguments(ImmutableArray<IParameterSymbol> parameters, int formatIndex)
{
if (formatIndex == parameters.Length - 1)
{
// format specification is the last parameter (e.g. CompositeFormat.Parse)
// this is therefore not a formatting method.
return -2;
}

// check params
IParameterSymbol nextParameter = parameters[formatIndex + 1];
if (nextParameter.IsParams)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,70 @@ End Class"
await basicTest.RunAsync();
}

[Fact]
[WorkItem(90357, "https://github.com/dotnet/runtime/issues/90357")]
buyaa-n marked this conversation as resolved.
Show resolved Hide resolved
public async Task CA2241CSharpPassingMethodWithNoPossibleArguments()
{
var csharpTest = new VerifyCS.Test
{
TestState =
{
Sources =
{
@"
using System.Diagnostics.CodeAnalysis;

class Test
{
public static int Parse([StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format) => -1;

void M1(string param)
{
var a = Parse(""{0} {1}"");
}
}"
},
ReferenceAssemblies = ReferenceAssemblies.Net.Net70,
}
};

await csharpTest.RunAsync();
}

[Fact]
[WorkItem(90357, "https://github.com/dotnet/runtime/issues/90357")]
public async Task CA2241CSharpFailingMethodWithNoPossibleArgumentsButInValidFormat()
{
var csharpTest = new VerifyCS.Test
{
TestState =
{
Sources =
{
@"
using System.Diagnostics.CodeAnalysis;

class Test
{
public static int Parse([StringSyntax(StringSyntaxAttribute.CompositeFormat)] string format) => -1;

void M1(string param)
{
var a = Parse(""{0 {1}"");
}
}"
},
ReferenceAssemblies = ReferenceAssemblies.Net.Net70,
}
};

csharpTest.ExpectedDiagnostics.Add(
// Test0.cs(10,17): warning CA2241: Provide correct arguments to formatting methods
GetBasicResultAt(10, 17));

await csharpTest.RunAsync();
}

#endregion

private static DiagnosticResult GetCSharpResultAt(int line, int column)
Expand Down