簡體   English   中英

c# editorconfig CA1062 null 檢查驗證方法(用於保護子句)具有可為空引用類型

[英]c# editorconfig CA1062 null check validation methods (for guard clauses) with nullable reference types

我在解決方案中創建了一個小的dotnetstandard 2.1庫項目。 我想使用Nullable Reference Types進行測試。 作為其中的一部分,我希望有適當的編譯錯誤和警告。

TLDR; 我想知道如何在.editorconfig正確設置 CA1062 代碼質量設置。

圖書館計划

我已將以下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 的漂亮的Guard Clauses庫。

已在項目中使用<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

所以有了這個,當我編譯時,我得到以下信息: 錯誤

所以到目前為止一切都如我所料。 我還沒有使用任何守衛。

修復它 - Steve Smith 的 Guard Library

因此,讓我們嘗試實現 Steve Smiths Guard Library 中的保護子句以消除錯誤。

因此,我們將以下內容添加到TestEditorConfig.TestMethod

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

並使用以下方法調整.editorconfig

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

太好了,一切都很好。 錯誤已經消失。 初始錯誤消失

修復它 - 我自己的守衛圖書館

但現在我想用我自己的守衛。 因此,我們將TestEditorConfig.TestMethod的初始保護檢查替換為:

FakeGuard.Validate(input);

並將 .editorconfig 中的.editorconfig替換為:

dotnet_code_quality.CA1062.null_check_validation_methods = FakeGuard.Validate

現在錯誤又回來了。
在此處輸入圖片說明

問題)

  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 Docs Code Quality並嘗試了 FakeGuard 的各種組合,包括:

  • 我的衛士
  • MyGuards.FakeGuard
  • 防偽衛士
  • MyGuards.FakeGuard.Validate
  • FakeGuard.Validate
  • 證實

奇怪的是,在不同的解決方案中,我沒有收到關於錯誤窗口中 CA1062 editorconfig 設置的任何投訴。 在那里我一直無法讓null_check_validation_methods工作 - 因此將這個解決方案放在一起。 這已經困擾了我一兩個月,但現在終於有精力把事情放在一起。

編輯器配置錯誤?

如果我將 FakeGuard 文件復制到 Library 項目,那么錯誤消息就會消失。 但是為什么這在解決方案中的不同項目中不起作用。

好的...我在roslyn-analyzers問題上roslyn-analyzers - 在這里 - 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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM