c# editorconfig CA1062 具有可空引用类型的空检查验证方法(用于保护子句)

2023-12-26

我创建了一个小dotnetstandard 2.1解决方案中的库项目。我想测试一下使用可空引用类型 https://learn.microsoft.com/en-us/dotnet/csharp/nullable-references。作为其中的一部分,我希望有适当的编译错误和警告。

太长了; 我想知道如何设置 CA1062 代码质量设置.editorconfig正确。

图书馆计划

我添加了以下内容nuget打包到项目中:

<ItemGroup>
      <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
      <PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
          <PrivateAssets>all</PrivateAssets>
          <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      </PackageReference>
      <PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
        <PackageReference Include="Ardalis.GuardClauses" Version="1.4.2" />
  </ItemGroup>

这包括各种代码分析包以及 Steve Smith 的好保护条款 https://github.com/ardalis/GuardClauses图书馆。

可空引用类型已启用<Nullable>enable</Nullable>在项目中。

我有一个类,在现实世界中这将是一个很好的实现,它实际上做了一些事情:

using System;
using MyGuards;

namespace EditorConfigIssues
{
    public static class TestEditorConfig
    {
        public static void TestMethod(MyParam input)
        {
            string x = input.Check;
            Console.WriteLine(x);
        }
    }

    public class MyParam
    {
        public MyParam(string check) => Check = check;

        public string Check { get; }
    }
}

警卫图书馆项目

在解决方案中,我添加了一个简单的 Guard 库,这是另一个dotnetstandard 2.1项目。

using System;

namespace MyGuards
{
    public static class FakeGuard
    {
        public static void Validate(object obj)
        {
            if (obj == null)
                throw new ArgumentNullException();
        }
    }
}

注意:这不是 GuardClauses 库的竞争 - 只是用来与 editorconfig 进行对比!

.editorconfig

我添加了一个.editorconfig通过以下诊断检查找到解决方案的根源:

dotnet_diagnostic.CA1062.severity = error
dotnet_code_quality.CA1062.exclude_extension_method_this_parameter = true

So with this in place, when I compile I get the following: Errors

所以到目前为止一切都如我所料。我还没有使用任何守卫。

修复它 - 史蒂夫·史密斯的警卫图书馆

因此,让我们尝试实现 Stevie Smiths Guard Library 中的保护子句来消除错误。

所以我们添加以下内容TestEditorConfig.TestMethod:

Guard.Against.Null(input, nameof(input));

并调整.editorconfig with:

dotnet_code_quality.CA1062.null_check_validation_methods = Ardalis.GuardClauses.Guard.Against.Null

Excellent, all is good. The error has disappeared. Initial error gone

修复它 - 我自己的守卫库

但现在我想用我自己的守卫。所以我们更换了最初的警卫检查TestEditorConfig.TestMethod with:

FakeGuard.Validate(input);

并替换 null_check_validation_methods.editorconfig with:

dotnet_code_quality.CA1062.null_check_validation_methods = FakeGuard.Validate

The error is now back.
enter image description here

问题)

  1. 问题是,为了使用具有来自同一解决方案的守卫的项目,我需要什么?
  2. 为什么我会在错误窗口中收到有关其他 CA1062 的警告?
The keyword "dotnet_code_quality.CA1062.exclude_extension_method_this_parameter" is unknown
The keyword "dotnet_code_quality.CA1062.null_check_validation_methods" is unknown

我一直在查看这个链接MS 文档代码质量 https://learn.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019并尝试了 FakeGuard 的各种组合,包括:

  • MyGuards
  • MyGuards.FakeGuard
  • 假卫士
  • MyGuards.FakeGuard.Validate
  • FakeGuard.验证
  • Validate

奇怪的是,在不同的解决方案中,我没有收到任何关于错误窗口中的 CA1062 editorconfig 设置的投诉。在那里我一直无法得到null_check_validation_methods工作 - 因此整合了这个解决方案。这已经困扰我一两个月了,但现在终于有精力把事情整合起来。

编辑器配置错误?

如果我将 FakeGuard 文件复制到 Library 项目,则错误消息就会消失。但为什么这在解决方案的不同项目中不起作用。


好的...我发布在roslyn-analyzers问题-这里-https://github.com/dotnet/roslyn-analyzers/issues/3451 https://github.com/dotnet/roslyn-analyzers/issues/3451

事实证明这是一个错误。目前,这是建议的解决方法:

using System;

[AttributeUsage(AttributeTargets.Parameter)] 
internal sealed class ValidatedNotNullAttribute : Attribute { } 

namespace MyGuards
{
    /// <summary>
    /// Checks stuff.
    /// </summary>
    public static class FakeGuard
    {
        /// <summary>
        /// No more nulls.
        /// </summary>
        /// <param name="obj"></param>
        public static void Validate([ValidatedNotNull] object obj)
        {
            if (obj == null)
                throw new ArgumentNullException();
        }
    }
}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

c# editorconfig CA1062 具有可空引用类型的空检查验证方法(用于保护子句) 的相关文章

随机推荐