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

Use pattern matching (RCS1146) #999

Merged
merged 4 commits into from Nov 23, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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