[英]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
添加到解決方案的根目錄:
dotnet_diagnostic.CA1062.severity = error
dotnet_code_quality.CA1062.exclude_extension_method_this_parameter = true
所以到目前為止一切都如我所料。 我還沒有使用任何守衛。
因此,讓我們嘗試實現 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
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 的各種組合,包括:
奇怪的是,在不同的解決方案中,我沒有收到關於錯誤窗口中 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.