Dotnet scanner seems to ignore #pragma warning disable

Hi,

It was suggested that I raise this issue over here as well:

It appears that scanner for dotnet (5.0 and 5.2) seems to ignore:
#pragma warning disable
lines.

If we directly use the sq roslyn analyzers they show up a warning, we then use the vs2019 action: suppress in source, and vs2019 prefers to wrap the line with #pragma warning disable/enable:

#pragma warning disable S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic));
#pragma warning enable S3011 // Reflection should not be used to increase accessibility of classes, methods, or fields

That appeases the roslyn analysis. However, when sonarscanner is used it seems to now ignore #pragma warning disable and the warning is shown in the msbuild binary log and on the SQ server.

Using:
[SuppressMessage("SonarQube", "S3011: Reflection should not be used to increase accessibility of classes, methods, or fields", Justification = "By design")]
does work, but has a broader scope, eg function or even class, not just on the one line, and also means converting things.

Is there a configuration setting I can change/investigate? I don’t know what exactly Scanner does, I suspect it’s pre-processing the .cs files, but the fact it causes csc to not process #pragma surprises me.

Thanks,
Chris

@cg110 : Let’s focus on handling this in your Commercial Support Ticket . I’ll close this thread.

1 Like

FYI for other community users who want to use pragmas in C# or VB.NET:

Both pragma and [SuppressMessage] are handled by the Microsoft compiler infrastructure. Neither the scanner nor our analyzers have any special code relating to either of those built-in suppression mechanisms.

I couldn’t repro this issue (either with v5.1 or v5.2 of the scanner, or the NET FX or NET Core versions). Issues suppress with a pragma don’t appear in the Roslyn error log created by the compiler, which is what is expected.

1 Like