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 @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ private static async Task<Document> UseConditionalAccessAsync(
(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 @@ private static async Task<Document> UseConditionalAccessAsync(

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
Original file line number Diff line number Diff line change
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 @@ private static bool IsFixable(
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
Original file line number Diff line number Diff line change
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