VS codeanalysis CA1062 fires even after null check

378 views Asked by At

I have a Sitecore project in which I am using visualstudio code analysis. I am using Sitecore method "Assert.ArgumentNotNull" to check for null arguments, However visualstudio code analysis engine doesn't recognize it and show "CA1062 Validate arguments of public methods" message.

Instead of creating a custom rule, Is there a easier way to tell analysis engine that "Assert.ArgumentNotNull" performs null check and message is invalid.

I don't want to suppress the message or disable it.

1

There are 1 answers

0
Vyacheslav Pritykin On BEST ANSWER

You can't use Sitecore's Assert class that way and that's why:

Sitecore Assert class as well as NotNullAttribute and CanBeNullAttribute were made the way ReSharper can understand when it performs its own analysis.

Definition of Assert.ArgumentNotNull(object, string) method is the following:

[AssertionMethod]
public static void ArgumentNotNull([CanBeNull] [AssertionCondition(AssertionConditionType.IS_NOT_NULL)] object argument, [CanBeNull] [InvokerParameterName] string argumentName)

All those attributes are defined in Sitecore and R# understands them because of naming conventions.

Unfortunately, VS code analysis has another naming conventions. ArgumentNotNull should look like this for you:

public static void ArgumentNotNull([ValidatedNotNull] object argument, string argumentName)

Since you can't modify the Assert class, you can't mark argument parameter with ValidatedNotNullAttribute.