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

CA1065 raised inappropriately in lambda in static constructor #6963

Closed
bradwilson opened this issue Sep 23, 2023 · 0 comments · Fixed by #6986
Closed

CA1065 raised inappropriately in lambda in static constructor #6963

bradwilson opened this issue Sep 23, 2023 · 0 comments · Fixed by #6986
Labels
False_Positive A diagnostic is reported for non-problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting
Milestone

Comments

@bradwilson
Copy link

Analyzer

Diagnostic ID: CA1065: Do not raise exceptions in unexpected locations

Analyzer source

SDK: Built-in CA analyzers in .NET 5 SDK or later

Version: SDK 7.0.401

Describe the bug

When writing a static constructor which constructs a lambda that throws an exception (not inside the constructor itself), CA1065 is inappropriately triggered.

Steps To Reproduce

Given the following csproj:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <AnalysisLevel>latest-All</AnalysisLevel>
    <EnableNETAnalyzers>true</EnableNETAnalyzers>
    <EnforceCodeStyleInBuild>true</EnforceCodeStyleInBuild>
    <LangVersion>10.0</LangVersion>
    <Nullable>enable</Nullable>
    <TargetFramework>net472</TargetFramework>
  </PropertyGroup>

</Project>

and source:

#pragma warning disable CA1810  // Disabled because it's orthogonal to this bug

using System;

namespace Empty;

public class ShouldViolate
{
    static ShouldViolate()
    {
        throw new DivideByZeroException();  // line 11
    }
}

public class ShouldNotViolate
{
    static readonly Action a;

    static ShouldNotViolate()
    {
        a = () => throw new DivideByZeroException();  // line 21
    }
}

Expected behavior

Only ShouldViolate should be flagged.

Actual behavior

Both classes are flagged:

MSBuild version 17.7.3+8ec440e68 for .NET
  Determining projects to restore...
  All projects are up-to-date for restore.
C:\Dev\repro\empty\Sample.cs(11,9): warning CA1065: .cctor creates an exception of type DivideByZeroException. Exceptions should not be raised in this type of method. If thisexception instance might be raised, change this method's logic so it no longer raises an exception. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1065) [C:\Dev\repro\empty\empty.csproj]
C:\Dev\repro\empty\Sample.cs(21,19): warning CA1065: .cctor creates an exception of type DivideByZeroException. Exceptions should not be raised in this type of method. If this exception instance might be raised, change this method's logic so it no longer raises an exception. (https://learn.microsoft.com/dotnet/fundamentals/code-analysis/quality-rules/ca1065) [C:\Dev\repro\empty\empty.csproj]
@mavasani mavasani added Area-Microsoft.CodeQuality.Analyzers False_Positive A diagnostic is reported for non-problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting labels Oct 11, 2023
@mavasani mavasani added this to the Unknown milestone Oct 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
False_Positive A diagnostic is reported for non-problematic case help wanted The issue is up-for-grabs, and can be claimed by commenting
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants