【问题标题】:Where to find the list of Check IDs for SuppressMessage?在哪里可以找到 SuppressMessage 的检查 ID 列表?
【发布时间】:2017-12-02 10:57:30
【问题描述】:

在哪里可以找到SuppressMessage 的检查 ID 列表?

下面的代码摘自微软关于SuppressMessageAttribute.CheckId的在线文档。我想知道SuppressMessage 的有效值列表,例如"Microsoft.Performance""CA1804:RemoveUnusedLocals"

[SuppressMessage("Microsoft.Performance", "CA1801:ReviewUnusedParameters", MessageId = "isChecked")]
[SuppressMessage("Microsoft.Performance", "CA1804:RemoveUnusedLocals", MessageId = "fileIdentifier")]
static void FileNode(string name, bool isChecked)
{
    string fileIdentifier = name;
    string fileName = name;
    string version = String.Empty;
}

我想取消警告“为方法使用表达式主体”,但我不知道应该为 SuppressMessage 赋予什么值。

代码示例:

namespace MyNameSpace
{
    public class MyClass
    {
        // This code raises a warning "Use expression body for methods".
        public string MyMethod()
        {
            return MyPrivateMethod();
        }

        // This code raises a warning "Use expression body for methods".
        string MyPrivateMethod()
        {
            return "Hello";
        }

        // This code raises a warning "Use block body for methods".
        public string MyMethod2() => MyPrivateMethod2();

        // This code raises a warning "Use block body for methods".
        string MyPrivateMethod2() => "Hello";
    }
}

在方法名称上移动光标将显示警告。该行为会阻止显示该方法的文档注释(如果有)。


Visual Studio 开发者社区:IDE0022 missmatch to IDE description

【问题讨论】:

  • 请向我们展示生成您试图抑制的警告的代码。 minimal reproducible example
  • @mjwills 我添加了一个生成警告的代码示例。
  • @mjwills Visual Studio for Mac(社区),版本 7.2.2(内部版本 11)。将光标移动到方法名称上并显示警告。该行为会阻止显示该方法的文档注释。

标签: c# visual-studio suppress-warnings visual-studio-mac


【解决方案1】:

所以我假设您正在谈论当您将鼠标悬停在某些文本上而不是构建警告时 VS for Mac 显示的工具提示。

您可以在 Text Editor - Source Analysis - C# 部分的首选项对话框中查看代码规则警告列表。

如果您在此处取消选中代码规则,它应该会阻止 VS for Mac 在文本编辑器中显示工具提示。

【讨论】:

    【解决方案2】:

    似乎IDE0022 抑制了“对方法使用表达式体”“对方法使用块体”

    using System.Diagnostics.CodeAnalysis;
    
    namespace MyNameSpace
    {
        public class MyClass
        {
            [SuppressMessage("ArbitraryCategoryNameSeemsToWork", "IDE0022")]
            public string MyMethod()
            {
                return MyPrivateMethod();
            }
    
            // This code raises a warning "Use expression body for methods".
            string MyPrivateMethod()
            {
                return "Hello";
            }
    
            // This code raises a warning "Use block body for methods".
            [SuppressMessage("ArbitraryCategoryNameSeemsToWork", "IDE0022")]
            public string MyMethod2() => MyPrivateMethod2();
    
            // This code raises a warning "Use block body for methods".
            string MyPrivateMethod2() => "Hello";
        }
    }
    

    我在 .NET coding convention settings for EditorConfig 中找到了 IDE0022。似乎警告是由 Visual Studio 引发的(而不是由 C# 编译器或类似的东西)。

    我在 "Code Analysis Warnings for Managed Code by CheckId" 中找到了 CheckIds 列表(以 CA 开头)。

    我发现 "Use expression body for methods""Use block body for methods" 之间的循环问题在 "IDE0022 missmatch to IDE description 中报告"(Visual Studio 开发者社区)。

    我可以在本地 PC 中更改 Visual Studio 的设置以抑制警告,但我正在寻找一种方法来抑制它们,即使在其他人的环境中也是如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-14
      • 1970-01-01
      相关资源
      最近更新 更多