c# - 无法抑制 Rider 中的 CA1307 警告

标签 c# rider editorconfig

我们有一个 .editorconfig 文件,其中包含我们所有项目的编码风格要求。队伍中一部分使用Rider,另一部分使用VS。我正在尝试抑制 CA1307 警告,如下所示:

dotnet_diagnostic.CA1307.severity = none

Visual Studio 可以很好地识别它,但 Rider 仍然将其显示为警告。但当我使用时它会被抑制

#pragma warning disable CA1307

这种奇怪的行为只适用于这个特定的规则,其他工作都可以。它有什么特别的地方还是我做错了什么?

最佳答案

我想出了什么问题,所以想分享我的发现,以防有人遇到同样的问题。看来这条规则和其他一些规则,如 1304、1305、1309、1311 等都是全局的,因此不能在 .editorconfig 中被抑制。有多种方法可以做到这一点:

  1. <NoWarn>1304,1305 ...</NoWarn>在 .csproj 文件中

  2. 添加 .globalconfig 文件和 .editorconfig

    is_global = true
    
    dotnet_diagnostic.CA1304.severity = none
    dotnet_diagnostic.CA1305.severity = none
    dotnet_diagnostic.CA1307.severity = none
    dotnet_diagnostic.CA1309.severity = none
    dotnet_diagnostic.CA1311.severity = none
    

这两个选项的坏处是它们在 VS 中工作得很好,但 Rider 不支持这两个选项并不断突出显示这些警告(他们对此有待处理的票证,但仍未修复)。因此,因为我们共享这些设置,并且开发人员同时使用 Rider 和 VS,所以我最终选择了选项

  • GlobalSuppressions.cs 文件。它必须放置在项目的根目录中,如下所示:

    using System.Diagnostics.CodeAnalysis;
    
    [assembly: SuppressMessage("Globalization", "CA1304:Specify CultureInfo", Justification = "Not needed in context of our projects", Scope = "module")]
    [assembly: SuppressMessage("Globalization", "CA1311:Specify a culture or use an invariant version", Justification = "Not needed in context of our projects", Scope = "module")]
    [assembly: SuppressMessage("Globalization", "CA1305:Specify IFormatProvider", Justification = "Not needed in context of our projects", Scope = "module")]
    [assembly: SuppressMessage("Globalization", "CA1307:Specify StringComparison", Justification = "Not needed in context of our projects", Scope = "module")]
    [assembly: SuppressMessage("Globalization", "CA1309:Use ordinal StringComparison", Justification = "Not needed in context of our projects", Scope = "module")]
    
  • 这样在 Rider 和 VS 中都可以工作。希望它能对某人有所帮助。

    关于c# - 无法抑制 Rider 中的 CA1307 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76367941/

    相关文章:

    c# - 将自签名证书与 .NET 的 HttpWebRequest/Response 结合使用

    entity-framework - 骑士。 EF Code First 迁移

    android - Rider "Android Sdk not found"即使它在那里

    roslyn - 在 Visual Studio 2019 Preview 4 中使用通用 .editorconfig 文件(在 csproj 中导入)的问题

    c# - 如何 "Pass configuration data to your plug-in"以及相同的目的是什么?有实时场景的例子吗?

    c# - 我是否需要序列化抽象基类以使派生类可序列化

    roslyn - 自最近升级以来新生成的 GeneratedMSBuildEditorConfig 文件

    c# - ReSharper 似乎不尊重 .editorconfig

    c# - 如何将 AutoFixture 与 NSubstitute 结合使用的示例

    git - 将 .gitignore 与 Rider IDE 结合使用