CA1801: Review unused parameters

Value
Rule ID CA1801
Category Microsoft.Usage
Fix is breaking or non-breaking Non-breaking - If the member is not visible outside the assembly, regardless of the change you make.

Non-breaking - If you change the member to use the parameter within its body.

Breaking - If you remove the parameter and it is visible outside the assembly.

Cause

A method signature includes a parameter that's not used in the method body.

This rule does not examine the following kinds of methods:

  • Methods referenced by a delegate.

  • Methods used as event handlers.

  • Serialization constructors (see guidelines).

  • Serialization GetObjectData methods.

  • Methods declared with the abstract (MustOverride in Visual Basic) modifier.

  • Methods declared with the virtual (Overridable in Visual Basic) modifier.

  • Methods declared with the override (Overrides in Visual Basic) modifier.

  • Methods declared with the extern (Declare statement in Visual Basic) modifier.

This rule does not flag parameters that are named with the discard symbol, for example, _, _1, and _2. This reduces warning noise on parameters that are needed for signature requirements, for example, a method used as a delegate, a parameter with special attributes, or a parameter whose value is implicitly accessed at run time by a framework but is not referenced in code.

Rule description

Review parameters in non-virtual methods that are not used in the method body to make sure no incorrectness exists around failure to access them. Unused parameters incur maintenance and performance costs.

Sometimes, a violation of this rule can point to an implementation bug in the method. For example, the parameter should have been used in the method body. Suppress warnings of this rule if the parameter must exist because of backward compatibility.

How to fix violations

To fix a violation of this rule, remove the unused parameter (a breaking change), or use the parameter in the method body (a non-breaking change).

When to suppress warnings

It is safe to suppress a warning from this rule:

  • In previously shipped code for which the fix would be a breaking change.

  • For the this parameter in a custom extension method for Microsoft.VisualStudio.TestTools.UnitTesting.Assert. The functions in the Assert class are static, so there's no need to access the this parameter in the method body.

Configurability

You can configure which parts of your codebase to run this rule on, based on their accessibility. For example, to specify that the rule should run only against the non-public API surface, add the following key-value pair to an .editorconfig file in your project:

dotnet_code_quality.ca1801.api_surface = private, internal

You can configure this option for just this rule, for all rules, or for all rules in this category (Performance). For more information, see Code quality rule configuration options.

Example

The following example shows two methods. One method violates the rule and the other method satisfies the rule.

// This method violates the rule.
public static string GetSomething(int first, int second)
{
    return first.ToString(CultureInfo.InvariantCulture);
}

// This method satisfies the rule.
public static string GetSomethingElse(int first)
{
    return first.ToString(CultureInfo.InvariantCulture);
}