-
-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Detect ArgumentNullException.ThrowIfNull (#974)
- Loading branch information
1 parent
967440f
commit 77f4e4a
Showing
5 changed files
with
140 additions
and
46 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
// 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; | ||
using System.Threading; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Roslynator.CSharp.Syntax; | ||
|
||
namespace Roslynator.CSharp | ||
{ | ||
internal readonly struct ArgumentNullCheckAnalysis | ||
{ | ||
private ArgumentNullCheckAnalysis(ArgumentNullCheckStyle style, string name, bool success) | ||
{ | ||
Style = style; | ||
Name = name; | ||
Success = success; | ||
} | ||
|
||
public ArgumentNullCheckStyle Style { get; } | ||
|
||
public string Name { get; } | ||
|
||
public bool Success { get; } | ||
|
||
public static ArgumentNullCheckAnalysis Create( | ||
StatementSyntax statement, | ||
SemanticModel semanticModel, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return Create(statement, semanticModel, name: null, cancellationToken); | ||
} | ||
|
||
public static ArgumentNullCheckAnalysis Create( | ||
StatementSyntax statement, | ||
SemanticModel semanticModel, | ||
string name, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
var style = ArgumentNullCheckStyle.None; | ||
string identifier = null; | ||
var success = false; | ||
|
||
if (statement is IfStatementSyntax ifStatement) | ||
{ | ||
if (ifStatement.SingleNonBlockStatementOrDefault() is ThrowStatementSyntax throwStatement | ||
&& throwStatement.Expression is ObjectCreationExpressionSyntax objectCreation) | ||
{ | ||
NullCheckExpressionInfo nullCheck = SyntaxInfo.NullCheckExpressionInfo( | ||
ifStatement.Condition, | ||
semanticModel, | ||
NullCheckStyles.EqualsToNull | NullCheckStyles.IsNull, | ||
cancellationToken: cancellationToken); | ||
|
||
if (nullCheck.Success) | ||
{ | ||
style = ArgumentNullCheckStyle.IfStatement; | ||
|
||
if (nullCheck.Expression is IdentifierNameSyntax identifierName) | ||
{ | ||
identifier = identifierName.Identifier.ValueText; | ||
|
||
if (name is null | ||
|| string.Equals(name, identifier, StringComparison.Ordinal)) | ||
{ | ||
if (semanticModel | ||
.GetSymbol(objectCreation, cancellationToken)? | ||
.ContainingType? | ||
.HasMetadataName(MetadataNames.System_ArgumentNullException) == true) | ||
{ | ||
success = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return new ArgumentNullCheckAnalysis(style, identifier, success); | ||
} | ||
} | ||
else if (statement is ExpressionStatementSyntax expressionStatement) | ||
{ | ||
SimpleMemberInvocationStatementInfo invocationInfo = SyntaxInfo.SimpleMemberInvocationStatementInfo(expressionStatement); | ||
|
||
if (invocationInfo.Success | ||
&& string.Equals(invocationInfo.NameText, "ThrowIfNull", StringComparison.Ordinal) | ||
&& semanticModel | ||
.GetSymbol(invocationInfo.InvocationExpression, cancellationToken)? | ||
.ContainingType? | ||
.HasMetadataName(MetadataNames.System_ArgumentNullException) == true) | ||
{ | ||
style = ArgumentNullCheckStyle.ThrowIfNullMethod; | ||
|
||
if (invocationInfo.Arguments.SingleOrDefault(shouldThrow: false)?.Expression is IdentifierNameSyntax identifierName) | ||
{ | ||
identifier = identifierName.Identifier.ValueText; | ||
|
||
if (string.Equals(name, identifier, StringComparison.Ordinal)) | ||
success = true; | ||
} | ||
} | ||
} | ||
|
||
return new ArgumentNullCheckAnalysis(style, identifier, success); | ||
} | ||
|
||
public static bool IsArgumentNullCheck( | ||
StatementSyntax statement, | ||
SemanticModel semanticModel, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return IsArgumentNullCheck(statement, semanticModel, name: null, cancellationToken); | ||
} | ||
|
||
public static bool IsArgumentNullCheck( | ||
StatementSyntax statement, | ||
SemanticModel semanticModel, | ||
string name, | ||
CancellationToken cancellationToken = default) | ||
{ | ||
return Create(statement, semanticModel, name, cancellationToken).Success; | ||
} | ||
} | ||
} |
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,11 @@ | ||
// Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Roslynator.CSharp | ||
{ | ||
internal enum ArgumentNullCheckStyle | ||
{ | ||
None, | ||
IfStatement, | ||
ThrowIfNullMethod, | ||
} | ||
} |
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