RefactorMCP catalog

Remove Unused Parameter

Removes a parameter that no body reads, together with the argument every call passes for it.

Arguments

ArgumentMeaning
parameterthe name of the parameter to remove

The target is the method or constructor, by symbol.

Precondition

  • No member of the method's family uses the parameter: not the method, not an override, not an implementation of the same interface member.
  • No call passes an argument for it that may have side effects: a call, an object creation, an assignment, an increment or an await. Dropping such an argument would drop the work it does.
  • The method is only called, never used as a method group.
  • Every member of the family is declared in the solution.
  • The result compiles.

Transformation

The parameter is removed as by Change Signature: from the method, every override and interface member related to it, and every call. Named arguments keep their names.

Preserved

  • The behaviour of every call.
  • Layout of the remaining parameters and arguments. A line comment after the comma of the element that becomes last moves after the closing parenthesis.

Limitations

  • Reading a property is assumed to have no side effects, so an argument that only reads properties is dropped.
  • <param> documentation for the parameter is left in place.

Error codes

CodeMeaning
parameter-in-usethe method or another member of its family uses the parameter
argument-has-side-effectsa call's argument may have side effects
method-group-referencethe method is used as a method group
external-memberthe method overrides or implements a member outside the solution

Cases

·

Diffs show each file from before to after. /*[*/ … /*]*/ marks a selection and /*^*/ a caret; the runner removes them before the refactoring runs.

#argument-has-side-effects-rejected

Refuses when a call's argument does work that removing it would lose

refusal
Targetsymbol M:Shop.Report.Title(System.String,System.Int32)
Arguments
parameter"width"
Refusesargument-has-side-effects mentioning “NextWidth()”; every file is left unchanged
Report.csinput
1 namespace Shop;
2
3 public class Report
4 {
5 private int _calls;
6
7 public string Title(string name, int width)
8 {
9 return name.ToUpperInvariant();
10 }
11
12 public string Header()
13 {
14 return Title("sales", NextWidth());
15 }
16
17 private int NextWidth()
18 {
19 _calls++;
20 return 80;
21 }
22 }

#calls-in-another-file

Removes the first parameter, updating positional and named calls in another file

success
Targetsymbol M:Shop.Invoice.Line(System.Int32,System.String,System.Decimal)
Arguments
parameter"index"
Invoice.csmodified
11 namespace Shop;
22
33 public class Invoice
44 {
5− public string Line(int index, string product, decimal price)
5+ public string Line(string product, decimal price)
66 {
77 return product + " " + price;
88 }
99 }
Printer.csmodified
11 namespace Shop;
22
33 public class Printer
44 {
55 public string Print(Invoice invoice)
66 {
7− return invoice.Line(1, "pen", 1.5m) + invoice.Line(price: 2m, index: 2, product: "ink");
7+ return invoice.Line("pen", 1.5m) + invoice.Line(product: "ink", price: 2m);
88 }
99 }

#generic-method

Removes an unused parameter from a generic method called with inferred and explicit type arguments

success
Targetsymbol M:Shop.Lists.First``1(System.Collections.Generic.List{``0},System.Boolean)
Arguments
parameter"cached"
Lists.csmodified
11 using System.Collections.Generic;
22
33 namespace Shop;
44
55 public static class Lists
66 {
7− public static T First<T>(List<T> items, bool cached)
7+ public static T First<T>(List<T> items)
88 {
99 return items[0];
1010 }
1111
1212 public static int Use(List<int> numbers)
1313 {
14− return First(numbers, true) + First<int>(numbers, false);
14+ return First(numbers) + First<int>(numbers);
1515 }
1616 }

#interface-implementations

Removes a parameter no implementation reads from the interface, every implementation and every call

success
Targetsymbol M:Shop.IAudit.Record(System.String,System.Int32)
Arguments
parameter"severity"
Audit.csmodified
11 using System.Collections.Generic;
22
33 namespace Shop;
44
55 public interface IAudit
66 {
7− void Record(string message, int severity);
7+ void Record(string message);
88 }
99
1010 public class MemoryAudit : IAudit
1111 {
1212 public List<string> Entries { get; } = new List<string>();
1313
14− public void Record(string message, int severity)
14+ public void Record(string message)
1515 {
1616 Entries.Add(message);
1717 }
1818 }
1919
2020 public class NullAudit : IAudit
2121 {
22− void IAudit.Record(string message, int severity)
22+ void IAudit.Record(string message)
2323 {
2424 }
2525 }
2626
2727 public class Checkout
2828 {
2929 public void Pay(IAudit audit)
3030 {
31− audit.Record("paid", 1);
31+ audit.Record("paid");
3232 }
3333 }

#parameter-in-use-rejected

Refuses to remove a parameter the body reads

refusal
Targetsymbol M:Shop.Report.Title(System.String,System.Int32)
Arguments
parameter"name"
Refusesparameter-in-use; every file is left unchanged
Report.csinput
1 namespace Shop;
2
3 public class Report
4 {
5 public string Title(string name, int width)
6 {
7 return name.ToUpperInvariant();
8 }
9
10 public string Header()
11 {
12 return Title("sales", 80);
13 }
14 }

#preserves-trivia

Removes the last parameter of a list laid out one per line; the comment after the new last element moves past the parenthesis

success
Targetsymbol M:Shop.Mailer.Send(System.String,System.String,System.Boolean)
Arguments
parameter"legacy"
Mailer.csmodified
11 namespace Shop;
22
33 public class Mailer
44 {
55 public string Send(
66 string to,
7− string body, // plain text
8− bool legacy)
7+ string body) // plain text
98 {
109 return to + ": " + body;
1110 }
1211
1312 public string Welcome()
1413 {
1514 // greet a new customer
1615 return Send(
1716 "ann@example.com",
18− "Welcome", // body
19− false);
17+ "Welcome"); // body
2018 }
2119 }

#unused-parameter

Removes a parameter the body never reads and the argument each call passes for it

success
Targetsymbol M:Shop.Report.Title(System.String,System.Int32)
Arguments
parameter"width"
Report.csmodified
11 namespace Shop;
22
33 public class Report
44 {
5− public string Title(string name, int width)
5+ public string Title(string name)
66 {
77 return name.ToUpperInvariant();
88 }
99
1010 public string Header()
1111 {
12− return Title("sales", 80);
12+ return Title("sales");
1313 }
1414 }

#used-by-override-rejected

Refuses when the method does not read the parameter but an override does

refusal
Targetsymbol M:Shop.Notifier.Format(System.String,System.Int32)
Arguments
parameter"level"
Refusesparameter-in-use mentioning “LoudNotifier”; every file is left unchanged
Notifier.csinput
1 namespace Shop;
2
3 public class Notifier
4 {
5 public virtual string Format(string message, int level)
6 {
7 return message;
8 }
9 }
10
11 public class LoudNotifier : Notifier
12 {
13 public override string Format(string message, int level)
14 {
15 return message + new string('!', level);
16 }
17 }