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

CA2241: Non supported heuristically found format methods should not be checked. #7024

Merged
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 @@ -388,10 +388,18 @@ public StringFormatInfo(Compilation compilation)
var determineAdditionalStringFormattingMethodsAutomatically = context.Options.GetBoolOptionValue(EditorConfigOptionNames.TryDetermineAdditionalStringFormattingMethodsAutomatically,
ArgumentCountRule, context.Operation.Syntax.SyntaxTree, context.Compilation, defaultValue: false);
if (determineAdditionalStringFormattingMethodsAutomatically &&
TryGetFormatInfoByParameterName(method, out info) &&
info.ExpectedStringFormatArgumentCount == -1)
TryGetFormatInfoByParameterName(method, out info))
{
return info;
if (info.ExpectedStringFormatArgumentCount == -1)
{
return info;
}
else
{
// TryGetFormatInfoByParameterName always adds info to _map
// We only support heuristically found format method that use 'params'.
_map.TryRemove(method, out _);
}
}

_map.TryAdd(method, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,69 @@ End Class"
await basicTest.RunAsync();
}

[Fact]
[WorkItem(7023, "https://github.com/dotnet/roslyn-analyzers/issues/7023")]

public async Task EditorConfigConfiguration_HeuristicAdditionalStringFormattingMethodsShouldNotConsiderIFormattableToString()
{
string editorConfigText =
"dotnet_code_quality.try_determine_additional_string_formatting_methods_automatically = true";

var csharpTest = new VerifyCS.Test
{
TestState =
{
Sources =
{
@"
class Test
{
string Formatted(double value1, double value2)
{
return value1 >= value2 ?
value1.ToString(""F1"", System.Globalization.CultureInfo.InvariantCulture) :
value2.ToString(""F1"", System.Globalization.CultureInfo.InvariantCulture);
}
}"
},
AnalyzerConfigFiles = { ("/.editorconfig", $@"root = true

[*]
{editorConfigText}
") }
}
};

await csharpTest.RunAsync();

var basicTest = new VerifyVB.Test
{
TestState =
{
Sources =
{
@"
Class Test
Private Function M1(ByVal value1 as Double, ByVal value2 as Double) As String
If value1 > value2
Return value1.ToString(""F1"", System.Globalization.CultureInfo.InvariantCulture)
Else
Return value2.ToString(""F1"", System.Globalization.CultureInfo.InvariantCulture)
End If
End Function
End Class"
},
AnalyzerConfigFiles = { ("/.editorconfig", $@"root = true

[*]
{editorConfigText}
") }
}
};

await basicTest.RunAsync();
}

[Theory]
[WorkItem(2799, "https://github.com/dotnet/roslyn-analyzers/issues/2799")]
// No configuration - validate no diagnostics in default configuration
Expand Down