为什么#pragma warning禁用S2068不起作用?

mar*_*ark 2 c# sonarqube

我在工具中有以下代码:

\n
var o = new OptionSet()\n    .Add("password=|pwd=|p=", "The database password. Only when using SQL authentication.", p => password = p.TrimQuotes())\n    ...\n;\n
Run Code Online (Sandbox Code Playgroud)\n

我的 PR 构建将声纳警告提升为编译错误,因此代码失败并显示:

\n
HashPasswordsArgs.cs(49,22): warning S2068: "password" detected here, make sure this is not a hard-coded credential. [C:\\xyz\\HashPasswords\\src\\HashPasswords\\HashPasswords.csproj]\n
Run Code Online (Sandbox Code Playgroud)\n

好的,我知道这是一个误报,并想在代码中抑制它(这是我具体且有意识的愿望,请不要建议去 SonarQube 服务器做任何事情)。

\n

我尝试添加//NOSONAR(//后有或没有空格),我还尝试#pragma warning disable S2068在文件顶部添加。没有任何帮助。

\n

我缺少什么?

\n

以下是运行示例:

\n

//诺声纳

\n
C:\\xyz\\HashPasswords [master \xe2\x89\xa1]> cat .\\src\\HashPasswords\\HashPasswordsArgs.cs |sls SONAR\n\n                .Add("password=|pwd=|p=", "The database password. Only when using SQL authentication.", p => password = p.TrimQuotes()) //NOSONAR\n\n\nC:\\xyz\\HashPasswords [master \xe2\x89\xa1 +1 ~2 -0 !]> dotnet build\nMicrosoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Core\nCopyright (C) Microsoft Corporation. All rights reserved.\n\n  Determining projects to restore...\n  All projects are up-to-date for restore.\nHashPasswordsArgs.cs(49,22): warning S2068: "password" detected here, make sure this is not a hard-coded credential. [C:\\xyz\\HashPasswords\\src\\HashPasswords\\HashPasswords.csproj]\n  HashPasswords -> C:\\xyz\\HashPasswords\\src\\HashPasswords\\bin\\Debug\\net472\\HashPasswords.exe\n  Sonar: (HashPasswords.csproj) Project processed successfully\n\nBuild succeeded.\n\nHashPasswordsArgs.cs(49,22): warning S2068: "password" detected here, make sure this is not a hard-coded credential. [C:\\xyz\\HashPasswords\\src\\HashPasswords\\HashPasswords.csproj]\n    1 Warning(s)\n    0 Error(s)\n\nTime Elapsed 00:00:01.23\nC:\\xyz\\HashPasswords [master \xe2\x89\xa1 +1 ~2 -0 !]>\n
Run Code Online (Sandbox Code Playgroud)\n

#pragma警告禁用S2068

\n
C:\\xyz\\HashPasswords [master \xe2\x89\xa1 +1 ~2 -0 !]> dir .\\src\\HashPasswords\\HashPasswordsArgs.cs |sls S2068\n\nsrc\\HashPasswords\\HashPasswordsArgs.cs:6:#pragma warning disable S2068\n\n\nC:\\xyz\\HashPasswords [master \xe2\x89\xa1 +1 ~2 -0 !]> dotnet build\nMicrosoft (R) Build Engine version 16.6.0+5ff7b0c9e for .NET Core\nCopyright (C) Microsoft Corporation. All rights reserved.\n\n  Determining projects to restore...\n  All projects are up-to-date for restore.\nHashPasswordsArgs.cs(51,22): warning S2068: "password" detected here, make sure this is not a hard-coded credential. [C:\\xyz\\HashPasswords\\src\\HashPasswords\\HashPasswords.csproj]\n  HashPasswords -> C:\\xyz\\HashPasswords\\src\\HashPasswords\\bin\\Debug\\net472\\HashPasswords.exe\n  Sonar: (HashPasswords.csproj) Project processed successfully\n\nBuild succeeded.\n\nHashPasswordsArgs.cs(51,22): warning S2068: "password" detected here, make sure this is not a hard-coded credential. [C:\\xyz\\HashPasswords\\src\\HashPasswords\\HashPasswords.csproj]\n    1 Warning(s)\n    0 Error(s)\n\nTime Elapsed 00:00:01.28\nC:\\xyz\\HashPasswords [master \xe2\x89\xa1 +1 ~2 -0 !]>\n
Run Code Online (Sandbox Code Playgroud)\n

我很想知道为什么 #pragma 不起作用。欢迎任何其他基于代码的抑制。越集中越好,即理想情况下,我只想抑制特定行上的这一警告。

\n

小智 5

下面的应该可以工作。

[System.Diagnostics.CodeAnalysis.SuppressMessage("Sonar Code Smell", "S2068:Credentials should not be hard-coded", Justification = "<Pending>")]

它也适用于我的以下属性 [SuppressMessage("Sonar Code Smell", "S2068:Credentials should not be hard-coded", Justification = "")]