Skip to content

Commit

Permalink
Fix RCS1208 (#1153)
Browse files Browse the repository at this point in the history
Co-authored-by: Josef Pihrt <josef@pihrt.net>
  • Loading branch information
jamesHargreaves12 and josefpihrt committed Aug 16, 2023
1 parent 47b002c commit 64944d2
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 6 deletions.
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()
{
}
}
");
}
}

0 comments on commit 64944d2

Please sign in to comment.