[英]c# editorconfig CA1062 null check validation methods (for guard clauses) with nullable reference types
I have created a small dotnetstandard 2.1
library project in a solution.我在解决方案中创建了一个小的dotnetstandard 2.1
库项目。 I want to test out using Nullable Reference Types .我想使用Nullable Reference Types进行测试。 As part of this, I want to have appropriate compilation errors and warnings.作为其中的一部分,我希望有适当的编译错误和警告。
TLDR; TLDR; I want to know how to setup the CA1062 code quality settings in .editorconfig
correctly.我想知道如何在.editorconfig
正确设置 CA1062 代码质量设置。
I have added the following nuget
packages to the project:我已将以下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>
This includes the various code analysis packages and also Steve Smith's nice Guard Clauses library.这包括各种代码分析包以及 Steve Smith 的漂亮的Guard Clauses库。
Nullable Reference Types has been enabled using <Nullable>enable</Nullable>
in the project.已在项目中使用<Nullable>enable</Nullable>
启用可空引用类型。
And I have a class which in the real world would be a nice implementation that actually did something:我有一个在现实世界中是一个很好的实现的类,它实际上做了一些事情:
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; }
}
}
In the solution I have added a simple Guard library which is another dotnetstandard 2.1
project.在解决方案中,我添加了一个简单的 Guard 库,它是另一个dotnetstandard 2.1
项目。
using System;
namespace MyGuards
{
public static class FakeGuard
{
public static void Validate(object obj)
{
if (obj == null)
throw new ArgumentNullException();
}
}
}
NOTE: This is not in competition of the GuardClauses library - just using to contrast editorconfig with!注意:这不与 GuardClauses 库竞争 - 只是用来对比 editorconfig !
I have added an .editorconfig
to the root of solution with the following diagnostic checks:我已通过以下诊断检查将.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:所以有了这个,当我编译时,我得到以下信息:
So everything is as I expect so far.所以到目前为止一切都如我所料。 I am not using any guards yet.我还没有使用任何守卫。
So lets try and implement the guard clauses from Steve Smiths Guard Library to get rid of the error.因此,让我们尝试实现 Steve Smiths Guard Library 中的保护子句以消除错误。
So we add the following to TestEditorConfig.TestMethod
:因此,我们将以下内容添加到TestEditorConfig.TestMethod
:
Guard.Against.Null(input, nameof(input));
and tweak the .editorconfig
with:并使用以下方法调整.editorconfig
:
dotnet_code_quality.CA1062.null_check_validation_methods = Ardalis.GuardClauses.Guard.Against.Null
Excellent, all is good.太好了,一切都很好。 The error has disappeared.错误已经消失。
But now I want to use my own guard.但现在我想用我自己的守卫。 So we replace the initial guard check in TestEditorConfig.TestMethod
with:因此,我们将TestEditorConfig.TestMethod
的初始保护检查替换为:
FakeGuard.Validate(input);
and replace the null_check_validation_methods in .editorconfig
with:并将 .editorconfig 中的.editorconfig
替换为:
dotnet_code_quality.CA1062.null_check_validation_methods = FakeGuard.Validate
The error is now back.现在错误又回来了。
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
I have been checking out this link MS Docs Code Quality and tried various combinations for the FakeGuard, including:我一直在查看此链接MS Docs Code Quality并尝试了 FakeGuard 的各种组合,包括:
Curiously, in a different solution, then I don't get any complaints about the CA1062 editorconfig settings in the Error Window.奇怪的是,在不同的解决方案中,我没有收到关于错误窗口中 CA1062 editorconfig 设置的任何投诉。 And in there I have been unable to get the null_check_validation_methods
working - hence putting together this solution.在那里我一直无法让null_check_validation_methods
工作 - 因此将这个解决方案放在一起。 This has been bugging me for a month or two, but finally getting the energy to put things together at the moment.这已经困扰了我一两个月,但现在终于有精力把事情放在一起。
If I copy the FakeGuard file to the Library project, then the error message disappears.如果我将 FakeGuard 文件复制到 Library 项目,那么错误消息就会消失。 But why does this not work in a different project in the solution.但是为什么这在解决方案中的不同项目中不起作用。
OK... I posted on the roslyn-analyzers
issues - here - https://github.com/dotnet/roslyn-analyzers/issues/3451好的...我在roslyn-analyzers
问题上roslyn-analyzers
- 在这里 - https://github.com/dotnet/roslyn-analyzers/issues/3451
As it turns out this is a bug.事实证明这是一个错误。 For now here is the suggested workaround:现在这里是建议的解决方法:
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.