Skip to content

Commit

Permalink
Merge pull request #6986 from CollinAlpert/issue_6963
Browse files Browse the repository at this point in the history
Do not raise CA1065 when in delegate
  • Loading branch information
mavasani authored Oct 30, 2023
2 parents b40db00 + 9bfbf63 commit cf0cf55
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public override void Initialize(AnalysisContext context)
// Find out if this given method is one of the interesting categories of methods.
// For example, certain Equals methods or certain accessors etc.
MethodCategory methodCategory = methodCategories.FirstOrDefault(l => l.IsMatch(methodSymbol, compilation));
MethodCategory? methodCategory = methodCategories.FirstOrDefault(l => l.IsMatch(methodSymbol, compilation));
if (methodCategory == null)
{
return;
Expand All @@ -94,8 +94,14 @@ public override void Initialize(AnalysisContext context)
// Throw statements.
operationBlockContext.RegisterOperationAction(operationContext =>
{
var throwOperation = (IThrowOperation)operationContext.Operation;
if (throwOperation.TryGetContainingAnonymousFunctionOrLocalFunction() is not null)
{
return;
}
// Get ThrowOperation's ExceptionType
if (((IThrowOperation)operationContext.Operation).GetThrownExceptionType() is INamedTypeSymbol thrownExceptionType && thrownExceptionType.DerivesFrom(exceptionType))
if (throwOperation.GetThrownExceptionType() is INamedTypeSymbol thrownExceptionType && thrownExceptionType.DerivesFrom(exceptionType))
{
// If no exceptions are allowed or if the thrown exceptions is not an allowed one..
if (methodCategory.AllowedExceptions.IsEmpty || !methodCategory.AllowedExceptions.Any(n => thrownExceptionType.IsAssignableTo(n, compilation)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,45 @@ End Class
await VerifyVB.VerifyAnalyzerAsync(code,
GetBasicNoExceptionsResultAt(6, 9, "Finalize", "Exception"));
}

[Fact, WorkItem(6963, "https://github.com/dotnet/roslyn-analyzers/issues/6963")]
public Task Lambda_NoDiagnostic()
{
const string code = """
using System;

public class ShouldNotViolate
{
static readonly Action a;

static ShouldNotViolate()
{
a = () => throw new DivideByZeroException();
}
}
""";

return VerifyCS.VerifyAnalyzerAsync(code);
}

[Fact, WorkItem(6963, "https://github.com/dotnet/roslyn-analyzers/issues/6963")]
public Task VB_Lambda_NoDiagnostic()
{
const string code = """
Imports System

Public Class ShouldNotViolate
Shared ReadOnly a As Action

Shared Sub New()
a = Sub () Throw New DivideByZeroException()
End Sub
End Class
""";

return VerifyVB.VerifyAnalyzerAsync(code);
}

#endregion

#region Operator tests
Expand Down

0 comments on commit cf0cf55

Please sign in to comment.