diff --git a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotRaiseExceptionsInUnexpectedLocations.cs b/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotRaiseExceptionsInUnexpectedLocations.cs index 36b38ea1c4..8a52badbb8 100644 --- a/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotRaiseExceptionsInUnexpectedLocations.cs +++ b/src/NetAnalyzers/Core/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotRaiseExceptionsInUnexpectedLocations.cs @@ -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; @@ -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))) diff --git a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotRaiseExceptionsInUnexpectedLocationsTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotRaiseExceptionsInUnexpectedLocationsTests.cs index 18fca105b5..4c8d2c9ef3 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotRaiseExceptionsInUnexpectedLocationsTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/DoNotRaiseExceptionsInUnexpectedLocationsTests.cs @@ -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