Roslyn-analyzers: CA1062 editorconfig issue with null_check_validation_methods in other project in solution

Created on 29 Mar 2020  路  9Comments  路  Source: dotnet/roslyn-analyzers

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:
Errors

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.
Initial error gone

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.
enter image description here

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.

Most helpful comment

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

All 9 comments

Checking whether this is a duplicate of https://github.com/dotnet/roslyn-analyzers/issues/2578 which has own unit test to cover this scenario.

@andez2000

dotnet_code_quality.CA1062.null_check_validation_methods = FakeGuard.Validate

Can you instead try specifying the option value as a full documentation comment ID, as explained here: https://github.com/dotnet/roslyn-analyzers/blob/master/docs/Analyzer%20Configuration.md#null-check-validation-methods?

  1. Enable XML doc comments for the project containing FakeGuard
  2. Build project and open the XML doc file to find the documentation comment ID for FakeGuard.Validate method, it should start with M: in the doc file.
  3. Supply the option as dotnet_code_quality.CA1062.null_check_validation_methods = <%doc_comment_id%>

If the issue still repros, can you provide a small snippet of your FakeGuard definition?

I tried the above and the error still appears. I tried copying various fragments of the doc comment id.

xml doc snippet:

<member name="M:MyGuards.FakeGuard.Validate(System.Object)">

FakeGuard

using System;

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

.editorconfig

dotnet_diagnostic.CA1062.severity = error
dotnet_code_quality.CA1062.exclude_extension_method_this_parameter = true
dotnet_code_quality.CA1062.null_check_validation_methods = M:MyGuards.FakeGuard.Validate(System.Object)

usage

using System;
using MyGuards;

namespace EditorConfigIssues
{
    public static class TestEditorConfig
    {
        public static void TestMethod(MyParam input)
        {
            FakeGuard.Validate(input);

            string x = input.Check;
            Console.WriteLine(x);
        }
    }

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

        public string Check { get; }
    }
}

NOTE: It did seem to work when I copied the FakeGuard class to the project that uses the FakeGuard class.

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

OK thanks... The ValidatedNotNullAttribute seems to work. The error has now gone thanks.

@mavasani Reading this thread I think that everything is now handled and so the ticket can be closed.

@Evangelink The workaround of using ValidatedNotNullAttribute works, but we need to ensure the editorconfig based configuration also works.

I still have the problem using VS 16.8.3 with Microsoft.CodeAnalysis.NetAnalyzers 5.0.1.
The build error list does not list this error but if I open a file that contains such a null-guard test method the error is still reported as an error.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sharwell picture sharwell  路  4Comments

fschlaef picture fschlaef  路  4Comments

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments

paulomorgado picture paulomorgado  路  3Comments