I have following extension method for strings:
public static bool IsNullOrEmpty(this string target)
{
return string.IsNullOrEmpty(target);
}
... and in the code I use it as follows:
public static string DoSomethingOnString(this string target)
{
if (target.IsNullOrEmpty())
return target;
target = target.Trim(); //This line causes CA1062 violation
return target;
}
Now, if I run code analysis on this, I get a violation of rule CA1062. But if I change the code to:
public static string DoSomethingOnString(this string target)
{
if (string.IsNullOrEmpty(target)) //CHANGED LINE
return target;
target = target.Trim(); //This line DOES NOT cause CA1062 violation anymore
return target;
}
... then it is fine.
Why it thinks that I'm not checking for null condition in the first example? Does it check only for string.IsNullOrEmpty or string.IsNullOrWhiteSpace? Is there a way to make CA recognize my extension method, or I'll need to suppress this rule?
UPDATE: If you have the same issue you can vote on feedback item I submitted on MS Connect: Code Analysis rule CA1062 raises false alarm