4

I'm unable to get CA1062 (validate arguments of public methods) to evaluate.

I've created a .Net Standard 2.0 C# class library and installed Microsoft.CodeQuality.Analyzers and several others nuget packages as per this screenshot: Analyzers for the project

I've also enabled CA1062 in the ruleset for the project, as per the image:

CA1062 is enabled in the ruleset

I've created this class to test if CA1062 (validate arguments of public methods) does evaluate:

public class Person
{
    public String Name { get; }
    public Person(String name)
    {
        this.Name = name;
    }
    public void DeclareWarOn(Person enemy)
    {
        String enemyName = enemy.Name;
        Console.WriteLine($"{this.Name} declared war on {enemyName}");
    }
}

In the DeclareWarOn method the parameter enemy is never checked for a null value but is used in the line String enemyName = enemy.Name;

This is suppose to trigger CA1062, but it is not triggered.

Other rules do evaluate. As example, I have created an internal Exception class as such:

internal class MyException : Exception 
{ 
    ... 
}

And this did indeed trigger the relevant code analysis warning for it (CA1064). However I can't get CA1062 to evaluate.

Cornelius
  • 3,526
  • 7
  • 37
  • 49
  • 1
    at first I though along the line of "could it be smart enough to detect that there is no default constructor, and no instance of `Person` could ever be constructed without a name", but you can still call `DeclareWarOn(null);` so that would be a bug of the analyzer. – Cee McSharpface Nov 23 '18 at 14:28
  • 1
    Found any solution yet ? got the same problem here. The same goes for CA1031 and CA1047. But others like CA1063, CA1716, CA1816 are detected correctly. I don't understand why some rules work and others don't. – Jonathan Jan 21 '19 at 14:38
  • Unfortunately not. I also have a lot of other CA rules not evaluating correctly, even despite setting each rule to evaluate as an error. – Cornelius Jan 21 '19 at 15:45
  • I'm seeing the same thing for CA1063 with .NET Core 2.0, though bizarrely it's inconsistent. `foreach (var e in listParam)` evaluates but not `foreach (var e in listParam.Select(x => x)` – makhdumi Apr 10 '19 at 18:08
  • @Jonathan see my answer below. – Cornelius Aug 19 '19 at 16:30

1 Answers1

0

I've recently removed all Analyzers and installed a higher version of the FxCop package on this project, and it solved the issue.

(I've added this in case any developer discovers this post and is still searching for a solution)

Cornelius
  • 3,526
  • 7
  • 37
  • 49