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

Fix RCS1208 #1153

Merged
merged 14 commits into from
Aug 16, 2023
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Fix [RCS1187](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1187.md) ([#1150](https://github.com/JosefPihrt/Roslynator/pull/1150)).
- Fix [RCS1056](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1056.md) ([#1154](https://github.com/JosefPihrt/Roslynator/pull/1154)).
- Fix [RCS1208](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1208.md) ([#1153](https://github.com/JosefPihrt/Roslynator/pull/1153))

## [4.4.0] - 2023-08-01

Expand Down
25 changes: 19 additions & 6 deletions src/CSharp.Workspaces/CSharp/SyntaxLogicalInverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,26 @@ internal static SyntaxLogicalInverter GetInstance(Document document)
case SyntaxKind.IsExpression:
{
var isExpression = (BinaryExpressionSyntax)expression;
var rightTypeSymbol = (ITypeSymbol)semanticModel.GetSymbol(isExpression.Right, cancellationToken)!;

return IsPatternExpression(
isExpression.Left,
isExpression.OperatorToken.WithTrailingTrivia(Space),
UnaryPattern(
Token(SyntaxKind.NotKeyword).WithTrailingTrivia(isExpression.OperatorToken.TrailingTrivia),
TypePattern((TypeSyntax)isExpression.Right)));
TypeSyntax type = rightTypeSymbol.ToTypeSyntax();

if (SymbolEqualityComparer.Default.Equals(
semanticModel.GetSpeculativeSymbolInfo(isExpression.SpanStart, isExpression.Right, SpeculativeBindingOption.BindAsExpression).Symbol,
semanticModel.GetSpeculativeSymbolInfo(isExpression.SpanStart, isExpression.Right, SpeculativeBindingOption.BindAsTypeOrNamespace).Symbol))
{
type = type.WithSimplifierAnnotation();
}

return (Options.UseNotPattern)
? IsPatternExpression(
isExpression.Left,
isExpression.OperatorToken.WithTrailingTrivia(Space),
UnaryPattern(
Token(SyntaxKind.NotKeyword)
.WithTrailingTrivia(isExpression.OperatorToken.TrailingTrivia),
TypePattern(type)))
: DefaultInvert(expression);
}
case SyntaxKind.AsExpression:
{
Expand Down
126 changes: 126 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,56 @@ void M2()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)]
public async Task Test_IsWhenIsNotIsInvalid()
{
await VerifyDiagnosticAndFixAsync(@"
class X
{
}

class C
{
public X X { get; set; }

C(object o)
{
[|if|] (o is X)
{
M2();
}
}

void M2()
{
}
}
", @"
class X
{
}

class C
{
public X X { get; set; }

C(object o)
{
if (o is not global::X)
{
return;
}

M2();
}

void M2()
{
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)]
public async Task Test_WhenParentIsConversionOperator()
{
Expand Down Expand Up @@ -614,6 +664,82 @@ void M2()
{
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)]
public async Task Test_WhenIsExpressionCsharp8()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
void M(object o)
{
[|if|] (o is string)
{
M2();
}
}

void M2()
{
}
}
", @"
class C
{
void M(object o)
{
if (!(o is string))
{
return;
}

M2();
}

void M2()
{
}
}
", options: WellKnownCSharpTestOptions.Default_CSharp8);
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)]
public async Task Test_WhenIsExpression()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
void M(object o)
{
[|if|] (o is string)
{
M2();
}
}

void M2()
{
}
}
", @"
class C
{
void M(object o)
{
if (o is not string)
{
return;
}

M2();
}

void M2()
{
}
}
");
}
}