Skip to content

Latest commit

 

History

History
118 lines (91 loc) · 2.42 KB

GU0001.md

File metadata and controls

118 lines (91 loc) · 2.42 KB

GU0001

Name the argument

Topic Value
Id GU0001
Severity Hidden
Enabled True
Category Gu.Analyzers.Correctness
Code ArgumentListAnalyzer

Description

Name the arguments of calls to methods that have more than 3 arguments and are placed on separate lines.

The warning is shown when the following criteria are met:

  • More than 3 arguments.
  • Each argument on their own row.
  • At least two adjacent arguments are of the same type.
  • Not in an expression tree.

Motivation

Naming the arguments can help in keeping things clear & correct. This comes up most often when calling constructors for immutable types:

public class Foo
{
    public Foo(int a, int b, int c, int d)
    {
        this.A = a;
        this.B = b;
        this.C = c;
        this.D = d;
    }

    public int A { get; }

    public int B { get; }

    public int C { get; }

    public int D { get; }

    private Foo Create(int a, int b, int c, int d)
    {
        return new Foo(
            a,
            b,
            c, 
            d);
    }
}

How to fix violations

Use the code fix. Or manually Name the parameter to match the assigned member

public class Foo
{
    public Foo(int a, int b, int c, int d)
    {
        this.A = a;
        this.B = b;
        this.C = c;
        this.D = d;
    }

    public int A { get; }

    public int B { get; }

    public int C { get; }

    public int D { get; }
	
    private Foo Create(int a, int b, int c, int d)
    {
        return new Foo(
            a: a,
            b: b,
            c: c,
            d: d);
    }
}

Configure severity

Via ruleset file.

Configure the severity per project, for more info see MSDN.

Via #pragma directive.

#pragma warning disable GU0001 // Name the argument
Code violating the rule here
#pragma warning restore GU0001 // Name the argument

Or put this at the top of the file to disable all instances.

#pragma warning disable GU0001 // Name the argument

Via attribute [SuppressMessage].

[System.Diagnostics.CodeAnalysis.SuppressMessage("Gu.Analyzers.Correctness", 
    "GU0001:Name the argument", 
    Justification = "Reason...")]