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 @@ -14183,4 +14183,104 @@ public void ConvertFieldValueForStorage(object value)
ReferenceAssemblies = ReferenceAssemblies.Net.Net80,
}.RunAsync();
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79180")]
public async Task ImplicitNumericConversionCastInTernary1()
{
await VerifyCS.VerifyAnalyzerAsync("""
class C
{
private const int ConstInt = 1;

uint M(bool condition)
{
return condition ? (uint)ConstInt : 0;
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79180")]
public async Task ImplicitNumericConversionCastInTernary2()
{
await VerifyCS.VerifyCodeFixAsync("""
class C
{
private const int ConstInt = 1;

uint M(bool condition)
{
return condition ? [|(uint)|]ConstInt : 0u;
}
}
""", """
class C
{
private const int ConstInt = 1;

uint M(bool condition)
{
return condition ? ConstInt : 0u;
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79180")]
public async Task ImplicitNumericConversionCastInTernary3()
{
await VerifyCS.VerifyCodeFixAsync("""
class C
{
private const int ConstInt = 1;

uint M(bool condition)
{
return condition ? [|(uint)|]ConstInt : [|(uint)|]0;
}
}
""", """
class C
{
private const int ConstInt = 1;

uint M(bool condition)
{
return condition ? ConstInt : (uint)0;
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/79180")]
public async Task ImplicitNumericConversionCastInTernary4()
{
await new VerifyCS.Test
{
TestCode = """
class C
{
private const int ConstInt = 1;

uint M(bool condition)
{
return condition ? [|(uint)|]ConstInt : [|(uint)|]0;
}
}
""",
FixedCode = """
class C
{
private const int ConstInt = 1;

uint M(bool condition)
{
return condition ? (uint)ConstInt : 0;
}
}
""",
CodeFixTestBehaviors = CodeFixTestBehaviors.FixOne | CodeFixTestBehaviors.SkipFixAllCheck,
DiagnosticSelector = d => d[1],
}.RunAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ private static bool IsConversionCastSafeToRemove(
}
}

// We can safely remove convertion to object in interpolated strings regardless of nullability
// We can safely remove conversion to object in interpolated strings regardless of nullability
if (castNode.IsParentKind(SyntaxKind.Interpolation) && originalConversionOperation.Type?.SpecialType is SpecialType.System_Object)
return true;

Expand Down Expand Up @@ -922,13 +922,17 @@ bool IsConditionalCastSafeToRemoveDueToConversionToOtherBranch()
if (!otherSideType.Equals(thisSideRewrittenType))
return false;

// Now check that with the (T) cast removed, that the outer `x ? y : z` is still immediately converted to a
// 'T'. If so, we can remove this inner (T) cast.
// Now check that with the (T) cast removed, that the outer `x ? y : z` is still
// immediately implicitly converted to a 'T'. If so, we can remove this inner (T) cast.

var rewrittenConditionalConvertedType = rewrittenSemanticModel.GetTypeInfo(rewrittenConditionalExpression, cancellationToken).ConvertedType;
if (rewrittenConditionalConvertedType is null)
return false;

var outerConversion = rewrittenSemanticModel.GetConversion(rewrittenConditionalExpression, cancellationToken);
if (!outerConversion.IsImplicit)
return false;

return rewrittenConditionalConvertedType.Equals(conversionOperation.Type);
}
}
Expand Down
Loading