简体   繁体   中英

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. I want to test out using Nullable Reference Types . As part of this, I want to have appropriate compilation errors and warnings.

TLDR; I want to know how to setup the CA1062 code quality settings in .editorconfig correctly.

Library Project

I have added the following nuget packages to the project:

<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.

Nullable Reference Types has been enabled using <Nullable>enable</Nullable> in the project.

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; }
    }
}

Guard Library Project

In the solution I have added a simple Guard library which is another dotnetstandard 2.1 project.

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!

.editorconfig

I have added an .editorconfig to the root of solution with the following diagnostic checks:

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.

Fixing it up - Steve Smith's Guard Library

So lets try and implement the guard clauses from Steve Smiths Guard Library to get rid of the error.

So we add the following to TestEditorConfig.TestMethod :

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

and tweak the .editorconfig with:

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

Excellent, all is good. The error has disappeared. 初始错误消失

Fixing it up - my own guard library

But now I want to use my own guard. So we replace the initial guard check in TestEditorConfig.TestMethod with:

FakeGuard.Validate(input);

and replace the null_check_validation_methods in .editorconfig with:

dotnet_code_quality.CA1062.null_check_validation_methods = FakeGuard.Validate

The error is now back.
在此处输入图片说明

Question(s)

  1. The question is, what do I need in order to use a project with guards from the same solution?
  2. Why am I getting warnings for the other CA1062 in the Error Window?
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:

  • MyGuards
  • MyGuards.FakeGuard
  • FakeGuard
  • MyGuards.FakeGuard.Validate
  • FakeGuard.Validate
  • Validate

Curiously, in a different solution, then I don't get any complaints about the CA1062 editorconfig settings in the Error Window. And in there I have been unable to get the null_check_validation_methods working - hence putting together this solution. This has been bugging me for a month or two, but finally getting the energy to put things together at the moment.

EditorConfig Bug?

If I copy the FakeGuard file to the Library project, then the error message disappears. 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

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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM