Skip to content

Commit

Permalink
Use pattern matching (RCS1146) (dotnet#999)
Browse files Browse the repository at this point in the history
  • Loading branch information
josefpihrt authored and JochemHarmes committed Oct 30, 2023
1 parent e1ef43d commit 46fe8c8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 6 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Expand Up @@ -34,6 +34,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fix refactoring ([RR0014](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RR0014.md)) ([#988](https://github.com/josefpihrt/roslynator/pull/988)).
- Fix refactoring ([RR0180](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RR0180.md)) ([#988](https://github.com/josefpihrt/roslynator/pull/988)).
- Recognize `ArgumentNullException.ThrowIfNull` ([RCS1227](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1227.md)) ([#992](https://github.com/josefpihrt/roslynator/pull/992)).
- Detect pattern matching in [RCS1146](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1146.md) ([#999](https://github.com/josefpihrt/roslynator/pull/999)).

## [4.1.2] - 2022-10-31

Expand Down
Expand Up @@ -84,8 +84,8 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context)
(ExpressionSyntax left, ExpressionSyntax right) = UseConditionalAccessAnalyzer.GetFixableExpressions(binaryExpression, kind, semanticModel, cancellationToken);

NullCheckStyles allowedStyles = (kind == SyntaxKind.LogicalAndExpression)
? NullCheckStyles.NotEqualsToNull
: NullCheckStyles.EqualsToNull;
? (NullCheckStyles.NotEqualsToNull | NullCheckStyles.IsNotNull)
: (NullCheckStyles.EqualsToNull | NullCheckStyles.IsNull);

NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(left, semanticModel, allowedStyles: allowedStyles, cancellationToken: cancellationToken);

Expand Down Expand Up @@ -190,7 +190,7 @@ int GetParenTokenDiff()

StatementSyntax newStatement = statement;

NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(ifStatement.Condition, NullCheckStyles.NotEqualsToNull);
NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(ifStatement.Condition, NullCheckStyles.NotEqualsToNull | NullCheckStyles.IsNotNull);

SimpleMemberInvocationStatementInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationStatementInfo(statement);

Expand Down
8 changes: 5 additions & 3 deletions src/Analyzers/CSharp/Analysis/UseConditionalAccessAnalyzer.cs
Expand Up @@ -58,7 +58,9 @@ private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context)
if (ifStatement.SpanContainsDirectives())
return;

NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(ifStatement.Condition, allowedStyles: NullCheckStyles.NotEqualsToNull);
NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(
ifStatement.Condition,
allowedStyles: NullCheckStyles.NotEqualsToNull | NullCheckStyles.IsNotNull);

ExpressionSyntax expression = nullCheck.Expression;

Expand Down Expand Up @@ -165,8 +167,8 @@ static bool ExistsImplicitConversionToBoolean(INamedTypeSymbol typeSymbol)
CancellationToken cancellationToken)
{
NullCheckStyles allowedStyles = (binaryExpressionKind == SyntaxKind.LogicalAndExpression)
? NullCheckStyles.NotEqualsToNull
: NullCheckStyles.EqualsToNull;
? (NullCheckStyles.NotEqualsToNull | NullCheckStyles.IsNotNull)
: (NullCheckStyles.EqualsToNull | NullCheckStyles.IsNull);

NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo(left, semanticModel, allowedStyles: allowedStyles, cancellationToken: cancellationToken);

Expand Down
55 changes: 55 additions & 0 deletions src/Tests/Analyzers.Tests/RCS1146UseConditionalAccessTests.cs
Expand Up @@ -80,6 +80,35 @@ void M()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseConditionalAccess)]
public async Task Test_IfStatement_PatternMatching()
{
await VerifyDiagnosticAndFixAsync(@"
class C
{
void M()
{
C x = null;
[|if (x is not null)
{
x.M();
}|]
}
}
", @"
class C
{
void M()
{
C x = null;
x?.M();
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseConditionalAccess)]
public async Task Test_LogicalAnd_ReferenceType()
{
Expand Down Expand Up @@ -183,6 +212,32 @@ void M()
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseConditionalAccess)]
public async Task Test_LogicalAnd_ReferenceType_PatternMatching()
{
await VerifyDiagnosticAndFixAsync(@"
class Foo
{
void M()
{
Foo x = null;
if ([|x is not null && !x.Equals(x)|]) { }
}
}
", @"
class Foo
{
void M()
{
Foo x = null;
if (x?.Equals(x) == false) { }
}
}
");
}

[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.UseConditionalAccess)]
public async Task Test_LogicalOr_ReferenceType()
{
Expand Down

0 comments on commit 46fe8c8

Please sign in to comment.