Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Analyzer: Simplify argument null check (RCS1255) (#994)
- Loading branch information
1 parent
fba159b
commit bae4d29
Showing
18 changed files
with
303 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
src/Analyzers/CSharp/Analysis/SimplifyArgumentNullCheckAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Immutable; | ||
using System.Linq; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Roslynator.CSharp.Syntax; | ||
|
||
namespace Roslynator.CSharp.Analysis; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class SimplifyArgumentNullCheckAnalyzer : BaseDiagnosticAnalyzer | ||
{ | ||
private static ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics; | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics | ||
{ | ||
get | ||
{ | ||
if (_supportedDiagnostics.IsDefault) | ||
Immutable.InterlockedInitialize(ref _supportedDiagnostics, DiagnosticRules.SimplifyArgumentNullCheck); | ||
|
||
return _supportedDiagnostics; | ||
} | ||
} | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
base.Initialize(context); | ||
|
||
context.RegisterSyntaxNodeAction(f => AnalyzeIfStatement(f), SyntaxKind.IfStatement); | ||
} | ||
|
||
private static void AnalyzeIfStatement(SyntaxNodeAnalysisContext context) | ||
{ | ||
var ifStatement = (IfStatementSyntax)context.Node; | ||
|
||
if (!ifStatement.IsSimpleIf()) | ||
return; | ||
|
||
NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo( | ||
ifStatement.Condition, | ||
NullCheckStyles.EqualsToNull | NullCheckStyles.IsNull); | ||
|
||
if (!nullCheck.Success) | ||
return; | ||
|
||
if (nullCheck.Expression is not IdentifierNameSyntax identifierName) | ||
return; | ||
|
||
if (ifStatement.SingleNonBlockStatementOrDefault() is not ThrowStatementSyntax throwStatement) | ||
return; | ||
|
||
if (throwStatement.Expression is not ObjectCreationExpressionSyntax objectCreation) | ||
return; | ||
|
||
ExpressionSyntax expression = objectCreation.ArgumentList?.Arguments.SingleOrDefault(shouldThrow: false)?.Expression; | ||
|
||
if (expression is null) | ||
return; | ||
|
||
if (ifStatement.ContainsUnbalancedIfElseDirectives()) | ||
return; | ||
|
||
INamedTypeSymbol containingTypeSymbol = context.SemanticModel | ||
.GetSymbol(objectCreation, context.CancellationToken)? | ||
.ContainingType; | ||
|
||
if (containingTypeSymbol?.HasMetadataName(MetadataNames.System_ArgumentNullException) != true) | ||
return; | ||
|
||
if (!containingTypeSymbol.GetMembers("ThrowIfNull").Any(f => f.IsKind(SymbolKind.Method))) | ||
return; | ||
|
||
if (expression.IsKind(SyntaxKind.StringLiteralExpression)) | ||
{ | ||
var literal = (LiteralExpressionSyntax)expression; | ||
|
||
if (string.Equals(identifierName.Identifier.ValueText, literal.Token.ValueText)) | ||
ReportDiagnostic(context, ifStatement); | ||
} | ||
else if (expression is InvocationExpressionSyntax invocationExpression) | ||
{ | ||
if (CSharpUtility.IsNameOfExpression(invocationExpression, context.SemanticModel, context.CancellationToken) | ||
&& invocationExpression.ArgumentList.Arguments.FirstOrDefault().Expression is IdentifierNameSyntax identifierName2 | ||
&& string.Equals(identifierName.Identifier.ValueText, identifierName2.Identifier.ValueText)) | ||
{ | ||
ReportDiagnostic(context, ifStatement); | ||
} | ||
} | ||
} | ||
|
||
private static void ReportDiagnostic(SyntaxNodeAnalysisContext context, IfStatementSyntax ifStatement) | ||
{ | ||
context.ReportDiagnostic(DiagnosticRules.SimplifyArgumentNullCheck, ifStatement.IfKeyword); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/Tests/Analyzers.Tests/RCS1255SimplifyArgumentNullCheckTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Roslynator.CSharp.CodeFixes; | ||
using Roslynator.Testing.CSharp; | ||
using Xunit; | ||
|
||
namespace Roslynator.CSharp.Analysis.Tests; | ||
|
||
public class RCS1255SimplifyArgumentNullCheckTests : AbstractCSharpDiagnosticVerifier<SimplifyArgumentNullCheckAnalyzer, IfStatementCodeFixProvider> | ||
{ | ||
public override DiagnosticDescriptor Descriptor { get; } = DiagnosticRules.SimplifyArgumentNullCheck; | ||
|
||
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.SimplifyArgumentNullCheck)] | ||
public async Task Test_IfStatement_Block_Nameof() | ||
{ | ||
await VerifyDiagnosticAndFixAsync(@" | ||
using System; | ||
class C | ||
{ | ||
C(string x) | ||
{ | ||
[|if|] (x is null) | ||
{ | ||
throw new ArgumentNullException(nameof(x)); | ||
} | ||
} | ||
} | ||
", @" | ||
using System; | ||
class C | ||
{ | ||
C(string x) | ||
{ | ||
ArgumentNullException.ThrowIfNull(x); | ||
} | ||
} | ||
"); | ||
} | ||
|
||
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.SimplifyArgumentNullCheck)] | ||
public async Task Test_IfStatement_Block_Literal() | ||
{ | ||
await VerifyDiagnosticAndFixAsync(@" | ||
using System; | ||
class C | ||
{ | ||
C(string x) | ||
{ | ||
[|if|] (x is null) | ||
{ | ||
throw new ArgumentNullException(""x""); | ||
} | ||
} | ||
} | ||
", @" | ||
using System; | ||
class C | ||
{ | ||
C(string x) | ||
{ | ||
ArgumentNullException.ThrowIfNull(x); | ||
} | ||
} | ||
"); | ||
} | ||
|
||
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.SimplifyArgumentNullCheck)] | ||
public async Task Test_IfStatement_Embedded_Nameof() | ||
{ | ||
await VerifyDiagnosticAndFixAsync(@" | ||
using System; | ||
class C | ||
{ | ||
C(string x) | ||
{ | ||
[|if|] (x is null) | ||
throw new ArgumentNullException(nameof(x)); | ||
} | ||
} | ||
", @" | ||
using System; | ||
class C | ||
{ | ||
C(string x) | ||
{ | ||
ArgumentNullException.ThrowIfNull(x); | ||
} | ||
} | ||
"); | ||
} | ||
|
||
[Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.SimplifyArgumentNullCheck)] | ||
public async Task TestNoDiagnostic_TwoArguments() | ||
{ | ||
await VerifyNoDiagnosticAsync(@" | ||
using System; | ||
class C | ||
{ | ||
C(string x) | ||
{ | ||
if (x is null) | ||
{ | ||
throw new ArgumentNullException(nameof(x), ""message""); | ||
} | ||
} | ||
} | ||
"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.