Skip to content

Pass correct parameterName to ArgumentException #33763

@terrajobst

Description

@terrajobst

Find places where nameof(...) is passed as the first (rather than second) argument to ArgumentException. Also places where nameof is used as the argument name in an ArgumentException (or derived type) but where it's not referring to a parameter.

Category: Reliability Usage

Examples to detect:

Single-argument nameof (or a literal string that matches the name of a parameter). Fixer: change to call the two argument constructor, pass null for the first parameter.

public void SomeMethod(string formatted)
{
    if (!Helper.TryParse(arg, out Parsed parsed))
    {
        throw new ArgumentException(nameof(formatted));
    }
}

Two argument call, first one looks like the parameter. Flip the argument order.

public void SomeMethod(string formatted)
{
    if (!Helper.TryParse(arg, out Parsed parsed))
    {
        throw new ArgumentException(
            nameof(formatted),
            string.Format(Resources.DidNotParse, formatted));
    }
}

Two argument call, paramName is a literal, but not a parameter name. (No fixer, just squiggle the argument.)

public void SomeMethod(string formatted)
{
    if (!Helper.TryParse(arg, out Parsed parsed))
    {
        throw new ArgumentException(
            string.Format(Resources.DidNotParse, formatted),
            "input");
    }
}

Two argument call, paramName is a literal, but not nameof. (Fixer: use nameof)

public void SomeMethod(string formatted)
{
    if (!Helper.TryParse(arg, out Parsed parsed))
    {
        throw new ArgumentException(
            string.Format(Resources.DidNotParse, formatted),
            "formatted");
    }
}

Examples to not detect:

Probably wrong (based on the parameter names), but probably shouldn't warn; since we're kinda guessing.

public static void ThrowArgumentException(string param, string msg)
{
    throw new ArgumentException(param, msg);
}

When the parameter name comes from a variable rather than a literal, don't squiggle.

public static void ThrowArgumentException(string msg, string param)
{
    throw new ArgumentException(msg, param);
}
public void SomeMethod(string formatted, int idx)
{
    string argFail = null;
    string msg = null;

    if (!Helper.TryParse(arg, out Parsed parsed))
    {
        argFail = "formatted";
        msg = string.Format(Resources.DidNotParse, formatted);
    }
    else if (parsed.Length < idx)
    {
        // Disregard that ArgumentOutOfRangeException is better here.
        argFail = "idx";
        msg = string.Format(Resources.OutOfRange, idx, parsed.Length);
    }

    if (argFail != null)
    {
        throw new ArgumentException(msg, argFail);
    }
}

Metadata

Metadata

Assignees

Labels

api-approvedAPI was approved in API review, it can be implementedarea-System.Runtimecode-analyzerMarks an issue that suggests a Roslyn analyzercode-fixerMarks an issue that suggests a Roslyn code fixer

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions