Visual Studio Team System
Review unused parameters

TypeName

AvoidUnusedParameters

CheckId

CA1801

Category

Microsoft.Performance

Breaking Change

Breaking,NonBreaking

Cause

A method signature includes a parameter that is not used in the method body. The following methods are not examined by this rule:

  • Methods referenced by a delegate.

  • Methods used as event handlers.

  • 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.

Rule Description

Review parameters in non-virtual methods that are not used in the method body to insure no correctness 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 (i.e. the parameter should actually have been used in the method body). Exclude warnings of this rule if the parameter has to 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). If the this parameter is reported, change the method declaration to include the static (Shared in Visual Basic) modifier (a breaking change).

When to Exclude Warnings

It is safe to exclude a warning from this rule for previously shipped code for which the fix would be a breaking change.

Example

The following shows two methods, one that violates the rule and one that satisfies the rule.

Related Rules

Avoid uncalled private code

Avoid uninstantiated internal classes

Remove unused locals

Tags :


Community Content

David M. Kean - MSFT
Example

The following shows two methods, one that violates the rule and one that satisfies the rule.

 using System;
using System.Globalization;
  
 namespace Samples
{
    public static class Foo
    {
         // This method violates the rule.

        public static string Bar(int first, int second)
        {
            return first.ToString(CultureInfo.InvariantCulture);
        }
  
         // This method satisfies the rule.
         public static string Bar(int first)
        {
            return first.ToString(CultureInfo.InvariantCulture);
        }
    }
}
Tags :

David M. Kean - MSFT
Does not fire on the 'this' parameter
Although the documentation states that 'this' parameter can be reported as unused, this is incorrect and this violation is actually found by MarkMembersAsStatic (http://msdnwiki.microsoft.com/en-us/mtpswiki/ms245046(VS.80).aspx).
Tags :

DominicZ
Variables that are not assigned inside the method are not taken into account

What about:

public static StreamWriter GetTemporaryFileForCustomer(string customerId)
{
     string tempFileName = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName + "\\" + customerId + ".tmp";
     if(File.Exists(tempFileName))
          return File.AppendText(tempFileName,true);
     else
          return File.CreateText(tempFileName);
}

customerId is said to violate the rule, although the variable is needed to carry out the logic. What alternative could be provided to satisfy the rule?

Tags :

Page view tracker