Skip to content

Commit

Permalink
Merge pull request #69458 from Blokyk/main
Browse files Browse the repository at this point in the history
Don't treat user-defined casts as constants in analyzers for "use pattern matching" and "convert if to switch"
  • Loading branch information
CyrusNajmabadi authored Sep 25, 2023
2 parents 6520e90 + fc794c0 commit 6ae46d6
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public static BinaryOperatorKind Flip(BinaryOperatorKind operatorKind)
private static bool IsConstant(IOperation operation)
{
// By-design, constants will not propagate to conversions.
return operation is IConversionOperation op
return operation is IConversionOperation { Conversion.IsUserDefined: false } op
? IsConstant(op.Operand)
: operation.ConstantValue.HasValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,30 @@ bool M1(int v)
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/66787")]
public async Task TestConvertedConstants()
{
await TestAllAsync(
"""
class C
{
bool M(long l)
{
return {|FixAllInDocument:(l > int.MaxValue || l < int.MinValue)|};
}
}
""",
"""
class C
{
bool M(long l)
{
return (l is > int.MaxValue or < int.MinValue);
}
}
""");
}

[Fact]
public async Task TestMissingInExpressionTree()
{
Expand Down Expand Up @@ -397,6 +421,42 @@ private static bool IsS(char[] ch, int count)
}}");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/66787")]
public async Task TestMissingForImplicitUserDefinedCasts1()
{
await TestMissingAsync(
"""
using System;
class C
{
void M0(Int128 i)
{
if (i == int.MaxValue [||] i == int.MinValue)
{
}
}
}
""");
}

[Fact, WorkItem("https://github.com/dotnet/roslyn/issues/66787")]
public async Task TestMissingForImplicitUserDefinedCasts2()
{
await TestMissingAsync(
"""
using System;
class C
{
void M0(Int128 i)
{
if (i > int.MaxValue [||] i < int.MinValue)
{
}
}
}
""");
}

[Fact]
public async Task TestOnSideEffects1()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ private static bool IsRelationalOperator(BinaryOperatorKind operatorKind)
private static bool IsConstant(IOperation operation)
{
// Constants do not propagate to conversions
return operation is IConversionOperation op
return operation is IConversionOperation { Conversion.IsUserDefined: false } op
? IsConstant(op.Operand)
: operation.ConstantValue.HasValue;
}
Expand Down

0 comments on commit 6ae46d6

Please sign in to comment.