Snippets to Fix FxCop CA1062

3 minute read

If you run FxCop you may frequently run into CA1062: Validate arguments of public methods. Below are three snippets to make writing this boiler plate a bit easier.

Before we get to the snippets, if you are in a position to disable CA1062 and still want thorough null checks, you might consider using IL weaving with Fody to automate the addition of null checks.

Example

Here is a quick example. This public method uses input before validating that it is not null. This results in the error shown below.

1
2
3
4
5
6
7
8
9
10
public class FxCopTest
{
    public static void MissingValidation(string input)
    {
        if (input.Length != 0)
        {
            Console.WriteLine(input);
        }
    }
}

Snippets

The snippets below all provide the same functionality, but differ in their style.

To install the snippet in Visual Studio.

  1. Press Ctrl+K, Ctrl+B to open the Code Snippet Manager, where you can get the path to the Visual C#\My Code Snippets.
  2. Save the selected snippet with any name, and the .snippet file extension.
  3. You may need to restart Visual Studio for the snippet to become available.

Usage for each snippet is the same:

  1. Type nullguard or enough of the name that IntelliSense selects nullguard.
  2. Hit tab twice.
  3. Type the name of the argument to check and press enter/return.

Each option below shows example output from the snippet, along with the snippet itself.

Default Option

Note, this snippet is not safe to use with StyleCop. See the next option.

1
if (input == null) throw new ArgumentNullException(nameof(input));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>nullguard</Title>
            <Shortcut>nullguard</Shortcut>
            <Description>Code snippet for performing null check.</Description>
            <Author>Mike Conrad - https://www.mjconrad.com/</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>argument</ID>
                    <ToolTip>Argument to check for null</ToolTip>
                    <Default>argument</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[if ($argument$ == null) throw new ArgumentNullException(nameof($argument$));]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

StyleCop Safe Option

This snippet avoids avoids violating these two StyleCop rules:

  • SA1503 - Braces should not be omitted
  • SA1501 - Statement should not be on a single line
1
input = input ?? throw new ArgumentNullException(nameof(input));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>nullguard</Title>
            <Shortcut>nullguard</Shortcut>
            <Description>Code snippet for performing null check.</Description>
            <Author>Mike Conrad - https://www.mjconrad.com/</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>argument</ID>
                    <ToolTip>Argument to check for null</ToolTip>
                    <Default>argument</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[$argument$ = $argument$ ?? throw new ArgumentNullException(nameof($argument$));]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Helper Class Option

The last option uses a helper class

1
NullGuard.Check(things, nameof(things));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static class NullGuard
{
    /// <summary>
    /// Throws an <see cref="ArgumentNullException"/> if the provided argument is null.
    /// </summary>
    /// <param name="argument">Argument to check.</param>
    /// <param name="name">Original name of the argument.</param>
    public static void Check(object argument, string name)
    {
        if (argument == null)
        {
            throw new ArgumentNullException(name);
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets  xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>nullguard</Title>
            <Shortcut>nullguard</Shortcut>
            <Description>Code snippet for performing null check.</Description>
            <Author>Mike Conrad - https://www.mjconrad.com/</Author>
            <SnippetTypes>
                <SnippetType>Expansion</SnippetType>
            </SnippetTypes>
        </Header>
        <Snippet>
            <Declarations>
                <Literal>
                    <ID>argument</ID>
                    <ToolTip>Argument to check for null</ToolTip>
                    <Default>argument</Default>
                </Literal>
            </Declarations>
            <Code Language="csharp"><![CDATA[if ($argument$ == null) throw new ArgumentNullException(nameof($argument$));]]>
            </Code>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>

Updated:

Leave a Comment

Your email address will not be published. Required fields are marked *

Loading...