RefactoringEssentials icon indicating copy to clipboard operation
RefactoringEssentials copied to clipboard

"Add null check" results in incorrect null check

Open mjcheetham opened this issue 8 years ago • 0 comments

Version information

Product Version
Refactoring Essentials VSIX 4.8.0.0
Visual Studio 2015 Community (Update 3)

Description

The "Add null check" refactoring applied to member access does not ensure the added null check is performed in the correct order in an existing if condition expression. The x.y.z != null expression should occur after both x and x.y have been null-checked.

Example code

Press Ctrl + . with cursor at the member access on foo and apply the "Add null check" refactoring..

if (foo != null)
{
    returnValue = foo.Bar.Value;
}

..becomes..

// If foo is null then the expression "foo.Bar != null" will cause a NullReferenceException
if ((foo.Bar != null) && (foo != null))
{
    returnValue = foo.Bar.Value;
}

..whereas it should be..

// foo should be null-checked before an expression containing a member access of foo
if ((foo != null) && (foo.Bar != null))
{
    returnValue = foo.Bar.Value;
}

badcodefix1

mjcheetham avatar Mar 09 '17 22:03 mjcheetham