Wiki

Clone wiki

Greenkeeper / ParameterNullChecks

Parameter null check

Generate the missing null checks for all parameters in a method or a constructor. The generated code checks all your parameters against null and throw an ArgumentNullException with the parametername.

MethodWithoutNullCheck

MethodWithNullCheck

In ReSharper you can already check parameter for null, but only for one parameter and there is no hint that you missed the check. Greenkeeper provides a ContextAction for all parameters and a Highlighting with a QuickFix if you missed a null check for any parameter.

Null check not needed

There are some cases where a parameter need no nullcheck.

  • parameter is valuetype

  • parameter marked with attribute "CanBeNull". The assembly JetBrains.Annotations includes this attribute.

    public void MethodA([CanBeNull] string parameter1)
    {
    }
    
  • parameter is optional with null

    public void MethodA(string parameter1 = null)
    {
    }
    
  • method is marked with configured attributes. Default is "Test", "TestCase" or "TestCaseSource"

    [TestCase]
    public void MethodA(string parameter1)
    {
    }
    
  • constructor call other constructor with :this or :base and there is a null check

    public A(string a)
        :this(a,string.Empty)
    {
    }
    
    public A(string a, string b)
    {
       if (a == null) throw new ArgumentNullException("a");
       if (b == null) throw new ArgumentNullException("b");
    }
    

Valid null checks

The following statements are interpreted as null checks

  • ==

    public void MethodA(string parameter1)
    {
       if (parameter1 == null) throw new ArgumentNullException("parameter1");
    }
    
  • String.IsNullOrEmpty

    public void MethodA(string parameter1)
    {
       if (String.IsNullOrEmpty(parameter1)) throw new ArgumentNullException("parameter1");
    }
    
  • String.IsNullOrWhiteSpace

    public void MethodA(string parameter1)
    {
       if (String.IsNullOrWhiteSpace(parameter1)) throw new ArgumentNullException("parameter1");
    }
    
  • ReferenceEquals

    public void MethodA(string parameter1)
    {
       if (ReferenceEquals(parameter1,null)) throw new ArgumentNullException("parameter1");
    }
    

Insert NotNull - Attribute

There is the possibility to add the "NotNull" attribute to the parameter while the null check is generated. This attribute offers several advantages. Additional information contained in the ReSharper documentation

You can configure this in the optionsdialog. Navigate in Visual Studio to the ReSharper-Options, menuitem Greenkeeper ParameterNullCheck (ReSharper > Options > Greenkeeper > ParameterNullCheck > Insert Not-Null Attribute to checked parameters). The default is true.

Before you can use this annotations you must include the assembly JetBrains.Annotations. This assembly is located in the ReSharper installation directory, usually C:\Program Files (x86)\JetBrains\ReSharper\v8.0\Bin\JetBrains.Annotations.dll.

Updated