-
Notifications
You must be signed in to change notification settings - Fork 467
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
CA1416 should Ignore diagnostics on throw statements #4545
Comments
@buyaa-n FYI: We already have a helper to detect this, which is used by many existing analyzers: roslyn-analyzers/src/Utilities/Compiler/Extensions/OperationBlockAnalysisContextExtension.cs Line 14 in 690be17
Hopefully, this should be a one line fix. |
Great, thanks! |
Actually, you might have to extend that helper to detect the platform not supported exception type: roslyn-analyzers/src/Utilities/Compiler/Extensions/OperationBlockAnalysisContextExtension.cs Lines 56 to 60 in 690be17
So maybe a 2 line fix :-) |
I was actually thinking to ignore for any type of exceptions, anyway for now it is fine to ignore for only those exceptions |
The helper seems for throw only methods without any other statements, and it covers the issue mentioned, but i think we don't need to flag for exceptions thrown with some other statements too, probably add check for string message as constructor argument, what do you think @jeffhandley @terrajobst? I can add another extension method if we want to handle exception throwing differently. [assembly: SupportedOSPlatform("Browser")]
public static class SR
{
public static string Message {get; set;}
public static void M1() { }
}
[UnsupportedOSPlatform("browser")]
public class Test
{
void OnlyThrows() // Works well, not warning
{
throw new PlatformNotSupportedException(SR.Message);
}
void ThrowsWithOtherStatements()
{
[|SR.M1()|]; // should only warn here
throw new PlatformNotSupportedException(SR.Message); // Fail, warning here too
}
} |
Thanks @mavasani, I am little worried that adding another exception the condition might break/malfunction other analyzers using that method. Run all tests in the repo no any failures though, maybe its OK. |
@buyaa-n - you can add a flag to the existing extension method to decide if it should flag only throw with specific exception types (not implemented and not supported) or all exception types. |
Sure, i can do that or create separate extension method, just meant about updating the existing method, will update depending on how we want to disable warnings |
It's OK with me to ignore throws of |
I was thinking ignore any string argument passed for any exceptions, but i am OK to restrict to only PNSE and NIE as they are the main scenario |
Fixed with #4577 |
Describe the improvement
When supported attributes added by SDK for the targeted platform we are seeing warnings in the statements just throwing PNSE, the warnings for the resource string used for the exception message which we couldn't separate for specific build.
Related to dotnet/runtime#44231.
Describe suggestions on how to achieve the rule
We could ignore diagnostics in throw statements as they are not platform specific operation at all
Additional context
For example in
net5.0-Browser
targeted buildSupportedOSPlatform("Browser")
attribute will be added to the project's AssemblyInfo.cs by MSBuld, which makes everything included in the assembly areBrowser
only including resource stringsSo for the above code we will see warnings:
cc @jeffhandley @terrajobst
The text was updated successfully, but these errors were encountered: