-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add analyzer "Invalid argument null check" (RCS1256) (#888)
- Loading branch information
1 parent
73bbf05
commit abe91e6
Showing
8 changed files
with
446 additions
and
17 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
40 changes: 40 additions & 0 deletions
40
src/Analyzers.CodeFixes/CSharp/CodeFixes/InvalidArgumentNullCheckCodeFixProvider.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,40 @@ | ||
// 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.Composition; | ||
using System.Threading.Tasks; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeActions; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Roslynator.CodeFixes; | ||
|
||
namespace Roslynator.CSharp.CodeFixes; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(InvalidArgumentNullCheckCodeFixProvider))] | ||
[Shared] | ||
public sealed class InvalidArgumentNullCheckCodeFixProvider : BaseCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds | ||
{ | ||
get { return ImmutableArray.Create(DiagnosticIdentifiers.InvalidArgumentNullCheck); } | ||
} | ||
|
||
public override async Task RegisterCodeFixesAsync(CodeFixContext context) | ||
{ | ||
SyntaxNode root = await context.GetSyntaxRootAsync().ConfigureAwait(false); | ||
|
||
if (!TryFindFirstAncestorOrSelf(root, context.Span, out StatementSyntax statement)) | ||
return; | ||
|
||
Document document = context.Document; | ||
Diagnostic diagnostic = context.Diagnostics[0]; | ||
|
||
CodeAction codeAction = CodeAction.Create( | ||
"Remove null check", | ||
ct => context.Document.RemoveStatementAsync(statement, ct), | ||
GetEquivalenceKey(diagnostic)); | ||
|
||
context.RegisterCodeFix(codeAction, diagnostic); | ||
} | ||
} |
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
138 changes: 138 additions & 0 deletions
138
src/Analyzers/CSharp/Analysis/InvalidArgumentNullCheckAnalyzer.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,138 @@ | ||
// 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 Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Roslynator.CSharp.Analysis; | ||
|
||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class InvalidArgumentNullCheckAnalyzer : BaseDiagnosticAnalyzer | ||
{ | ||
private static ImmutableArray<DiagnosticDescriptor> _supportedDiagnostics; | ||
|
||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics | ||
{ | ||
get | ||
{ | ||
if (_supportedDiagnostics.IsDefault) | ||
Immutable.InterlockedInitialize(ref _supportedDiagnostics, DiagnosticRules.InvalidArgumentNullCheck); | ||
|
||
return _supportedDiagnostics; | ||
} | ||
} | ||
|
||
public override void Initialize(AnalysisContext context) | ||
{ | ||
base.Initialize(context); | ||
|
||
context.RegisterSyntaxNodeAction(f => AnalyzeMethodDeclaration(f), SyntaxKind.MethodDeclaration); | ||
context.RegisterSyntaxNodeAction(f => AnalyzeConstructorDeclaration(f), SyntaxKind.ConstructorDeclaration); | ||
} | ||
|
||
private static void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context) | ||
{ | ||
var methodDeclaration = (MethodDeclarationSyntax)context.Node; | ||
AnalyzeParameterList(context, methodDeclaration.ParameterList, methodDeclaration.Body); | ||
} | ||
|
||
private static void AnalyzeConstructorDeclaration(SyntaxNodeAnalysisContext context) | ||
{ | ||
var constructorDeclaration = (ConstructorDeclarationSyntax)context.Node; | ||
AnalyzeParameterList(context, constructorDeclaration.ParameterList, constructorDeclaration.Body); | ||
} | ||
|
||
private static void AnalyzeParameterList(SyntaxNodeAnalysisContext context, ParameterListSyntax parameterList, BlockSyntax body) | ||
{ | ||
if (body is null) | ||
return; | ||
|
||
if (parameterList is null) | ||
return; | ||
|
||
SeparatedSyntaxList<ParameterSyntax> parameters = parameterList.Parameters; | ||
|
||
if (!parameters.Any()) | ||
return; | ||
|
||
SyntaxList<StatementSyntax> statements = body.Statements; | ||
|
||
int statementCount = statements.Count; | ||
|
||
if (statementCount == 0) | ||
return; | ||
|
||
int lastIndex = -1; | ||
|
||
foreach (ParameterSyntax parameter in parameters) | ||
{ | ||
if (parameter.IsParams()) | ||
break; | ||
|
||
lastIndex++; | ||
} | ||
|
||
if (lastIndex == -1) | ||
return; | ||
|
||
foreach (StatementSyntax statement in statements) | ||
{ | ||
ArgumentNullCheckAnalysis nullCheck = ArgumentNullCheckAnalysis.Create(statement, context.SemanticModel, context.CancellationToken); | ||
|
||
if (nullCheck.Success) | ||
{ | ||
ParameterSyntax parameter = FindParameter(nullCheck.Name); | ||
|
||
if (parameter is not null) | ||
{ | ||
if (parameter.Default?.Value.IsKind( | ||
SyntaxKind.NullLiteralExpression, | ||
SyntaxKind.DefaultLiteralExpression, | ||
SyntaxKind.DefaultExpression) == true | ||
|| IsNullableReferenceType(context, parameter)) | ||
{ | ||
if (statement is IfStatementSyntax ifStatement) | ||
{ | ||
context.ReportDiagnostic(DiagnosticRules.InvalidArgumentNullCheck, ifStatement.IfKeyword); | ||
} | ||
else | ||
{ | ||
context.ReportDiagnostic(DiagnosticRules.InvalidArgumentNullCheck, statement); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
bool IsNullableReferenceType(SyntaxNodeAnalysisContext context, ParameterSyntax parameter) | ||
{ | ||
TypeSyntax type = parameter.Type; | ||
|
||
if (type.IsKind(SyntaxKind.NullableType)) | ||
{ | ||
ITypeSymbol typeSymbol = context.SemanticModel.GetTypeSymbol(type, context.CancellationToken); | ||
|
||
if (typeSymbol?.IsKind(SymbolKind.ErrorType) == false | ||
&& typeSymbol.IsReferenceType) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
ParameterSyntax FindParameter(string name) | ||
{ | ||
for (int i = 0; i <= lastIndex; i++) | ||
{ | ||
if (parameters[i].Identifier.ValueText == name) | ||
return parameters[i]; | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.