Skip to content

Commit

Permalink
Mark X and B number formats as invariant culture
Browse files Browse the repository at this point in the history
Fix #744
  • Loading branch information
meziantou committed Jul 26, 2024
1 parent 359a6a4 commit 3073490
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ private bool IsCultureSensitiveType(ITypeSymbol? symbol, IOperation? format, IOp
return false;
}

if (symbol.IsNumberType() && format is { ConstantValue: { HasValue: true, Value: string formatString } } && formatString is "B" or ['x', ..] or ['X', ..])
return false;

if (symbol.IsDateTime() || symbol.IsEqualToAny(DateTimeOffsetSymbol, DateOnlySymbol, TimeOnlySymbol))
{
if (IsInvariantDateTimeFormat(format))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,27 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Theory]
[InlineData("x")]
[InlineData("x8")]
[InlineData("X")]
[InlineData("X8")]
[InlineData("B")]
public async Task Int32_InvariantFormat(string format)
{
await CreateProjectBuilder()
.WithSourceCode($$"""
class TypeName
{
public void Test()
{
(-1).ToString("{{format}}");
}
}
""")
.ValidateAsync();
}

[Fact]
public async Task BooleanToStringWithoutCultureInfo_ShouldNotReportDiagnostic()
{
Expand Down

0 comments on commit 3073490

Please sign in to comment.