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 @@ -289,8 +289,9 @@ public CultureSensitivity GetCultureSensitivity(IOperation operation, CultureSen
if (operation is IBinaryOperation binaryOperation)
return GetCultureSensitivity(binaryOperation.Type, format: null, instance: null, options);

// Unknown operation
return CultureSensitivity.CultureSensitive;
// Unknown operation (conditional expression, coalesce expression, switch expression, await expression, ...).
// The formatting depends on the type of the value, so use it to determine the culture sensitivity.
return GetCultureSensitivity(operation.Type, format: null, instance: operation, options);
}

public bool IsInInterpolatedStringHandlerContext(IInterpolatedStringOperation operation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,146 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Fact]
public async Task Concat_ConditionalExpression_CultureInsensitiveBranches_NoDiagnostic()
{
var sourceCode = """
using System;
using System.Globalization;
class Test
{
void A(DateTime? date)
{
_ = "test" + (date.HasValue ? date.Value.ToString(CultureInfo.InvariantCulture) : string.Empty);
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Concat_ConditionalExpression_CultureSensitiveType()
{
var sourceCode = """
class Test
{
void A(bool condition, double a, double b)
{
_ = "test" + ([|condition ? a : b|]);
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Concat_CoalesceExpression_NoDiagnostic()
{
var sourceCode = """
class Test
{
void A(string value)
{
_ = "test" + (value ?? "");
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Concat_CoalesceExpression_CultureSensitiveType()
{
var sourceCode = """
class Test
{
void A(double? value)
{
_ = "test" + ([|value ?? 1.5|]);
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Concat_SwitchExpression_NoDiagnostic()
{
var sourceCode = """
class Test
{
void A(int value)
{
_ = "test" + (value switch { 0 => "a", _ => "b" });
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Concat_SwitchExpression_CultureSensitiveType()
{
var sourceCode = """
class Test
{
void A(int value)
{
_ = "test" + ([|value switch { 0 => 1.5, _ => 2.5 }|]);
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Concat_AwaitExpression_NoDiagnostic()
{
var sourceCode = """
using System.Threading.Tasks;
class Test
{
async Task A(Task<string> task)
{
_ = "test" + await task;
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

[Fact]
public async Task Concat_AwaitExpression_CultureSensitiveType()
{
var sourceCode = """
using System.Threading.Tasks;
class Test
{
async Task A(Task<double> task)
{
_ = "test" + [|await task|];
}
}
""";
await CreateProjectBuilder()
.WithSourceCode(sourceCode)
.ValidateAsync();
}

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