Suppress CA1062 with fluent validation
Asked Answered
K

1

5

I have a fluent, extensible validation helper like:

Assert.That(aParameter).IsNotNull();

It is extensible because the That method is actually generic (That<T>) and uses implicit typing to return a generic IAssertCondition<T> object. IsNotNull is actually an extension method.

Anyway, the problem using this approach to validate the parameters passed into a method is that I get CA1062 warnings instructing me to validate the arguments before using them which, of course, I am already doing.

I read Eric Smith's post (here) about using a ValidatedNotNullAttribute to inform FxCop that the argument is being validated but I don't see how I can accomplish this using the fluent interface I've described.

What are my options so that Code Analysis will recognize that the above statement satisfies the requirements and the warning will not appear?

Kate answered 23/11, 2011 at 15:39 Comment(0)
I
4

The only place you could add the attribute in this case is on the parameter of the That<T> method. Unfortunately, while that would prevent CA1062 from firing, it could lead to false negatives since you need to call more than just That<T> to actually implement a "not null" verification. If you want to use Code Analysis to properly check for parameter validation in a manner that recognizes your validation helper, you're pretty much going to have to write your own rule to replace CA1062.

Infeasible answered 23/11, 2011 at 16:5 Comment(6)
Can you point me to any resources explaining how to write my own rule so I can see what's involved? Thx!Kate
There is no official rule authoring SDK. The single most complete resource is binarycoder.net/fxcop/index.html.Infeasible
I've done a cursory read of the docs at the link and like the idea of writing some custom rules to enforce other standards but I don't see how I would be able to replace the existing CA1062 rule. Can you provide more detail?Kate
Replace is the key word. I can see from the link how I can ADD custom rules but you are suggesting that I can REPLACE an existing, built-in rule with an implementation of my own. I'm not seeing how to accomplish that.Kate
Or are you suggesting that I create my implementation and effectively replace CA1062 by turning CA1062 off?Kate
The latter. Replace your use of CA1062 by use of a custom rule that does what you want.Infeasible

© 2022 - 2024 — McMap. All rights reserved.