Pragmas do not disable rosyln rules - C#

roslyn.sonaranalyzer.security.cs:S5146 - rules that come from the roslyn rule sets seem to not be able to be disabled by adding the normal pragma statements. It is not unique to this rule, but have seen it on some other roslyn rules too.

#pragma warning disable S5146 // Will be redirecting to unknown URL

Using C# with Visual Studio 2022 and have installed the latest SonarLint extension and shows as a violation in SonarCloud

Hello @jfoegen,

S5146 is a security rule that is raised outside of the Roslyn build of your project.
Therefore you can’t disable it by using pragmas.

If you want to disable it, you can either:

  • remove the rule from your quality profile, that would apply to your whole project
  • accept the issue in SonarCloud for each file you want to disable the rule

I hope this helps.

Have a nice day!

Appreciate the reply. At least we know why we cannot defeat it with pragmas.

We prefer not to disable on a project or file basis. If there is something flagged we cannot immediately fix because of testing or whatever, we would rather do something to that method to disable it and then add a work item to address it in a future sprint. But when doing it on a more global basis, I feel the problem may be glossed over.

The other problem is consistency. We would like to deal with disabling rules in a consistent manner, rather some rules are done this way and now if that doesn’t work, we need a whole different procedure.