Skip to content

Commit

Permalink
MA0050 detects ArgumentException.Throws methods
Browse files Browse the repository at this point in the history
  • Loading branch information
meziantou committed Oct 23, 2023
1 parent eaebd97 commit 0971083
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ internal void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
return;

var lastThrowIndex = descendants
.Where(node => (node.IsKind(SyntaxKind.ThrowStatement) || node.IsKind(SyntaxKind.ThrowExpression)) && IsArgumentException(context, node))
.Where(node => IsArgumentValidation(context, node))
.DefaultIfEmpty()
.Max(node => GetEndOfBlockIndex(context, node));

Expand All @@ -97,6 +97,25 @@ internal void AnalyzeMethodDeclaration(SyntaxNodeAnalysisContext context)
}
}

private bool IsArgumentValidation(SyntaxNodeAnalysisContext context, SyntaxNode node)
{
if ((node.IsKind(SyntaxKind.ThrowStatement) || node.IsKind(SyntaxKind.ThrowExpression)) && IsArgumentException(context, node))
return true;

if (node is InvocationExpressionSyntax invocationExpression)
{
if (context.SemanticModel.GetOperation(invocationExpression, context.CancellationToken) is IInvocationOperation operation)
{
var targetMethod = operation.TargetMethod;
return targetMethod.IsStatic &&
targetMethod.ContainingType.IsOrInheritFrom(_argumentExceptionSymbol) &&
targetMethod.Name.Contains("Throw", System.StringComparison.Ordinal);
}
}

return false;
}

public bool IsArgumentException(SyntaxNodeAnalysisContext context, SyntaxNode syntaxNode)
{
var exceptionExpression = syntaxNode switch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,35 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Fact]
public async Task ValidValidation_ThrowIfNull()
{
const string SourceCode = @"using System.Collections.Generic;
class TypeName
{
IEnumerable<int> A(string a)
{
System.ArgumentNullException.ThrowIfNull(a);
return A();
IEnumerable<int> A()
{
yield return 0;
if (a == null)
{
yield return 1;
}
}
}
}";

await CreateProjectBuilder()
.WithSourceCode(SourceCode)
.WithTargetFramework(TargetFramework.Net8_0)
.ValidateAsync();
}

[Fact]
public async Task ReportDiagnostic_IAsyncEnumerable()
{
Expand Down Expand Up @@ -235,4 +264,43 @@ await CreateProjectBuilder()
.ValidateAsync();
}

[Fact]
public async Task ReportDiagnostic_IAsyncEnumerable_ThrowIfNull()
{
const string SourceCode = @"using System.Collections.Generic;
class TypeName
{
async IAsyncEnumerable<int> [||]A(string a)
{
System.ArgumentNullException.ThrowIfNull(a);
await System.Threading.Tasks.Task.Delay(1);
yield return 0;
}
}";

const string CodeFix = @"using System.Collections.Generic;
class TypeName
{
IAsyncEnumerable<int> A(string a)
{
System.ArgumentNullException.ThrowIfNull(a);
return A();
async IAsyncEnumerable<int> A()
{
await System.Threading.Tasks.Task.Delay(1);
yield return 0;
}
}
}";

await CreateProjectBuilder()
.WithTargetFramework(TargetFramework.Net8_0)
.WithSourceCode(SourceCode)
.ShouldFixCodeWith(CodeFix)
.ValidateAsync();
}
}

0 comments on commit 0971083

Please sign in to comment.