Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not raise CA1065 when in delegate #6986

Merged
merged 3 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (ThrowOperationOccursInDelegate(throwOperation))
{
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 All @@ -109,6 +115,22 @@ public override void Initialize(AnalysisContext context)
});
}

private static bool ThrowOperationOccursInDelegate(IThrowOperation throwOperation)
mavasani marked this conversation as resolved.
Show resolved Hide resolved
{
var throwParent = throwOperation.Parent;
while (throwParent is not null)
{
if (throwParent is IDelegateCreationOperation)
{
return true;
}

throwParent = throwParent.Parent;
}

return false;
}

/// <summary>
/// This object describes a class of methods where exception throwing statements should be analyzed.
/// </summary>
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