How to make Code Analysis understand that an argument has been confirmed in a called method

Usually, if I had this:

public string Foo(string text) { return text.Substring(3); } 

I would get CA1062: Validate arguments of public methods from code analysis. This would be fixed by modifying the code as such:

 public string Foo(string text) { if (text == null) throw new ArgumentNullException("text"); else if (string.IsNullEmptyOrWhiteSpace(text) throw new ArgumentException("May not be empty or white space", "text") else if (text.Length < 3) throw new ArgumentException("Must be at least 3 characters long", "text"); return text.Substring(3); } 

But now I want to use another tool to perform this check:

 public string Foo(string text) { Validator.WithArgument(text, "text").NotNullEmptyOrWhitespace().OfMinLength(3); return text.Substring(3); } 

since the method checks the argument, the code analysis rule is executed, but you still get warning CA1062 . Is there a way to suppress the code analysis rule for such cases without manually suppressing them each time or disabling this particular code analysis rule?

+6
source share
1 answer

The ValidatedNotNullAttribute attribute can be used to indicate that a parameter has been verified in a helper method. However, this is not necessarily an excellent choice for a free verification API, since you need to add it to the parameter of the wrong method (your WithArgument method, as opposed to your NotNullEmptyOrWhitespace method).

+2
source

All Articles