Skip to content

Commit

Permalink
Rename analyzer SimplifyBooleanExpression to UnnecessaryNullCheck (RC…
Browse files Browse the repository at this point in the history
…S1199) (fix #373)
  • Loading branch information
josefpihrt committed Sep 18, 2020
1 parent 9a411ef commit c26a06d
Show file tree
Hide file tree
Showing 9 changed files with 659 additions and 224 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Text;
using Roslynator.CodeFixes;
using Roslynator.CSharp.Refactorings;
using Roslynator.CSharp.Syntax;
Expand Down Expand Up @@ -38,7 +39,7 @@ public sealed override ImmutableArray<string> FixableDiagnosticIds
DiagnosticIdentifiers.ValueTypeObjectIsNeverEqualToNull,
DiagnosticIdentifiers.JoinStringExpressions,
DiagnosticIdentifiers.UseExclusiveOrOperator,
DiagnosticIdentifiers.SimplifyBooleanExpression,
DiagnosticIdentifiers.UnnecessaryNullCheck,
DiagnosticIdentifiers.UseShortCircuitingOperator,
DiagnosticIdentifiers.UnnecessaryOperator);
}
Expand Down Expand Up @@ -198,11 +199,11 @@ public sealed override async Task RegisterCodeFixesAsync(CodeFixContext context)
context.RegisterCodeFix(codeAction, diagnostic);
break;
}
case DiagnosticIdentifiers.SimplifyBooleanExpression:
case DiagnosticIdentifiers.UnnecessaryNullCheck:
{
CodeAction codeAction = CodeAction.Create(
"Simplify boolean expression",
cancellationToken => SimplifyBooleanExpressionRefactoring.RefactorAsync(document, binaryExpression, cancellationToken),
"Remove unnecessary null check",
ct => RemoveUnnecessaryNullCheckAsync(document, binaryExpression, ct),
GetEquivalenceKey(diagnostic));

context.RegisterCodeFix(codeAction, diagnostic);
Expand Down Expand Up @@ -335,5 +336,53 @@ private static ConditionalAccessExpressionSyntax CreateConditionalAccess(Express
expression.Parenthesize(),
MemberBindingExpression(IdentifierName("Length")));
}

private static async Task<Document> RemoveUnnecessaryNullCheckAsync(
Document document,
BinaryExpressionSyntax logicalAnd,
CancellationToken cancellationToken)
{
BinaryExpressionInfo binaryExpressionInfo = SyntaxInfo.BinaryExpressionInfo(logicalAnd);

ExpressionSyntax right = binaryExpressionInfo.Right;

SemanticModel semanticModel = await document.GetSemanticModelAsync(cancellationToken).ConfigureAwait(false);

NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(binaryExpressionInfo.Left, semanticModel, NullCheckStyles.HasValue | NullCheckStyles.NotEqualsToNull);

var binaryExpression = right as BinaryExpressionSyntax;

ExpressionSyntax newRight;
switch (right.Kind())
{
case SyntaxKind.SimpleMemberAccessExpression:
{
newRight = TrueLiteralExpression().WithTriviaFrom(right);
break;
}
case SyntaxKind.LogicalNotExpression:
{
newRight = FalseLiteralExpression().WithTriviaFrom(right);
break;
}
default:
{
newRight = binaryExpression.Right;
break;
}
}

BinaryExpressionSyntax newBinaryExpression = BinaryExpression(
(binaryExpression != null)
? right.Kind()
: SyntaxKind.EqualsExpression,
nullCheck.Expression.WithLeadingTrivia(logicalAnd.GetLeadingTrivia()),
(binaryExpression != null)
? ((BinaryExpressionSyntax)right).OperatorToken
: Token(SyntaxKind.EqualsEqualsToken).WithTriviaFrom(logicalAnd.OperatorToken),
newRight).WithFormatterAnnotation();

return await document.ReplaceNodeAsync(logicalAnd, newBinaryExpression, cancellationToken).ConfigureAwait(false);
}
}
}

This file was deleted.

19 changes: 16 additions & 3 deletions src/Analyzers/Analyzers.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3930,10 +3930,10 @@ string x = s + i; // [|Id|]]]></Before>
</Sample>
</Samples>
</Analyzer>
<Analyzer Identifier="SimplifyBooleanExpression">
<Analyzer Identifier="UnnecessaryNullCheck">
<Id>RCS1199</Id>
<Title>Simplify boolean expression.</Title>
<Category>Simplification</Category>
<Title>Unncessary null check.</Title>
<Category>Redundancy</Category>
<DefaultSeverity>Info</DefaultSeverity>
<IsEnabledByDefault>true</IsEnabledByDefault>
<Samples>
Expand All @@ -3947,6 +3947,19 @@ if (x.HasValue && x.Value) // [|Id|]
}]]></Before>
<After><![CDATA[if (x == true)
{
}]]></After>
</Sample>
<Sample>
<Before><![CDATA[bool? x = null;
bool y = false;
// ...
if (x != null && x.Value == y) // [|Id|]
{
}]]></Before>
<After><![CDATA[if (x == y)
{
}]]></After>
</Sample>
</Samples>
Expand Down
118 changes: 0 additions & 118 deletions src/Analyzers/CSharp/Analysis/SimplifyBooleanExpressionAnalyzer.cs

This file was deleted.

Loading

0 comments on commit c26a06d

Please sign in to comment.