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
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 @@ private ExpressionSyntax LogicallyInvertImpl(
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()
{
}
}
");
}
}