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

Don't treat user-defined casts as constants in analyzers for "use pattern matching" and "convert if to switch" #69458

Merged
merged 4 commits into from
Sep 25, 2023
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 @@ -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