title: c# editorconfig CA1062 null check validation methods (for guard clauses) with nullable reference types
body: <p>I have created a small <code>dotnetstandard 2.1</code> library project in a solution.  I want to test out using <a href="https://docs.microsoft.com/en-us/dotnet/csharp/nullable-references" rel="nofollow noreferrer">Nullable Reference Types</a>.  As part of this, I want to have appropriate compilation errors and warnings.</p>
  
  <p>TLDR;
  I want to know how to setup the CA1062 code quality settings in <code>.editorconfig</code> correctly.</p>
  
  <h2>Library Project</h2>
  
  <p>I have added the following <code>nuget</code> packages to the project:</p>
  
  <pre><code>&lt;ItemGroup&gt;
        &lt;PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="2.9.8"&gt;
            &lt;PrivateAssets&gt;all&lt;/PrivateAssets&gt;
            &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
        &lt;/PackageReference&gt;
        &lt;PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8"&gt;
            &lt;PrivateAssets&gt;all&lt;/PrivateAssets&gt;
            &lt;IncludeAssets&gt;runtime; build; native; contentfiles; analyzers; buildtransitive&lt;/IncludeAssets&gt;
        &lt;/PackageReference&gt;
        &lt;PackageReference Include="Microsoft.CSharp" Version="4.7.0" /&gt;
          &lt;PackageReference Include="Ardalis.GuardClauses" Version="1.4.2" /&gt;
    &lt;/ItemGroup&gt;
  </code></pre>
  
  <p>This includes the various code analysis packages and also Steve Smith's nice <a href="https://github.com/ardalis/GuardClauses" rel="nofollow noreferrer">Guard Clauses</a> library.</p>
  
  <p>Nullable Reference Types has been enabled using <code>&lt;Nullable&gt;enable&lt;/Nullable&gt;</code> in the project.  </p>
  
  <p>And I have a class which in the real world would be a nice implementation that actually did something:</p>
  
  <pre><code>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) =&gt; Check = check;
  
          public string Check { get; }
      }
  }
  </code></pre>
  
  <h2>Guard Library Project</h2>
  
  <p>In the solution I have added a simple Guard library which is another <code>dotnetstandard 2.1</code> project.</p>
  
  <pre><code>using System;
  
  namespace MyGuards
  {
      public static class FakeGuard
      {
          public static void Validate(object obj)
          {
              if (obj == null)
                  throw new ArgumentNullException();
          }
      }
  }
  </code></pre>
  
  <p>NOTE: This is not in competition of the GuardClauses library - just using to contrast editorconfig with!</p>
  
  <h2>.editorconfig</h2>
  
  <p>I have added an <code>.editorconfig</code> to the root of solution with the following diagnostic checks:</p>
  
  <pre><code>dotnet_diagnostic.CA1062.severity = error
  dotnet_code_quality.CA1062.exclude_extension_method_this_parameter = true
  </code></pre>
  
  <p>So with this in place, when I compile I get the following:
  <a href="https://i.stack.imgur.com/TkoYK.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TkoYK.png" alt="Errors"></a></p>
  
  <p>So everything is as I expect so far.  I am not using any guards yet.</p>
  
  <h2>Fixing it up - Steve Smith's Guard Library</h2>
  
  <p>So lets try and implement the guard clauses from Steve Smiths Guard Library to get rid of the error.</p>
  
  <p>So we add the following to <code>TestEditorConfig.TestMethod</code>:</p>
  
  <pre><code>Guard.Against.Null(input, nameof(input));
  </code></pre>
  
  <p>and tweak the <code>.editorconfig</code> with:</p>
  
  <pre><code>dotnet_code_quality.CA1062.null_check_validation_methods = Ardalis.GuardClauses.Guard.Against.Null
  </code></pre>
  
  <p>Excellent, all is good.  The error has disappeared.
  <a href="https://i.stack.imgur.com/kgXSC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kgXSC.png" alt="Initial error gone"></a></p>
  
  <h2>Fixing it up - my own guard library</h2>
  
  <p>But now I want to use my own guard.  So we replace the initial guard check in <code>TestEditorConfig.TestMethod</code> with:</p>
  
  <pre><code>FakeGuard.Validate(input);
  </code></pre>
  
  <p>and replace the null_check_validation_methods in <code>.editorconfig</code> with:</p>
  
  <pre><code>dotnet_code_quality.CA1062.null_check_validation_methods = FakeGuard.Validate
  </code></pre>
  
  <p>The error is now back.<br>
  <a href="https://i.stack.imgur.com/LPUHB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LPUHB.png" alt="enter image description here"></a></p>
  
  <h2>Question(s)</h2>
  
  <ol>
  <li>The question is, what do I need in order to use a project with guards from the same solution?</li>
  <li>Why am I getting warnings for the other CA1062 in the Error Window?</li>
  </ol>
  
  <pre><code>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
  </code></pre>
  
  <p>I have been checking out this link <a href="https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1062?view=vs-2019" rel="nofollow noreferrer">MS Docs Code Quality</a> and tried various combinations for the FakeGuard, including: </p>
  
  <ul>
  <li>MyGuards</li>
  <li>MyGuards.FakeGuard</li>
  <li>FakeGuard</li>
  <li>MyGuards.FakeGuard.Validate</li>
  <li>FakeGuard.Validate</li>
  <li>Validate</li>
  </ul>
  
  <p>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 <code>null_check_validation_methods</code> 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.</p>
  
  <h2>EditorConfig Bug?</h2>
  
  <p>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.</p>