How do I get ReSharper Code Inspection to recognize our assert method is preventing a possible System.NullReferenceException?


We are inundated with false "possible System.NullReferenceException" warnings
because ReSharper code inspection has no way to determine that our assert
method is preventing invalid use of null.


Here's a sample assert we use to simplify and clarify method parameter checking.
There are other overloads as you'd expect:

static class Guard
{
    public static void AgainstNull(T
value)" methods to return "value",  and then do

    arg1 = Guard.AgainstNull(arg1, "arg1")

but that has too much code smell.

Thanks,
EdHow do I get ReSharper Code Inspection to recognize our assert method is
preventing a possible System.NullReferenceException?


1 comment

Hello Ed,

You can mark your method with AssertionMethod attribute from external annotations.
For more information please see http://www.jetbrains.com/resharper/webhelp/Code_Analysis__External_Annotations.html.
Thank you!

Andrey Serebryansky
Senior Support Engineer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

We are inundated with false "possible System.NullReferenceException"
warnings because ReSharper code inspection has no way to determine
that our assert method is preventing invalid use of null.

Here's a sample assert we use to simplify and clarify method parameter
checking. There are other overloads as you'd expect:

static class Guard
{
public static void AgainstNull<T>(T value, string parameterName)
where
T : class
{
if(value == null)
throw new ArgumentNullException(parameterName);
}
}
Here's the typical call site usage, and where the issue occurs.

class SomeClass
{
void SomeMethod(int[] arg1, string arg2)
{
Guard.AgainstNull(arg1, "arg1");
Guard.AgainstNull(arg2, "arg2");
int count = arg2.Length; // <---warning "possible
System.NullReferenceException",
and quick fix action list appears
}
}
I realize we could appease the code inspector by refactoring our "void
AgainstNull<T>(T value)" methods to return "value",  and then do

arg1 = Guard.AgainstNull(arg1, "arg1")

but that has too much code smell.

Thanks,
EdHow do I get ReSharper Code Inspection to recognize our assert
method is
preventing a possible System.NullReferenceException?



0

Please sign in to leave a comment.