RefactorMCP catalog

Safe Delete Member

Deletes a method, property, indexer, field or event that nothing uses.

Precondition

  • The target is a method, property, field or event. A type is deleted with Safe Delete Type.
  • Nothing refers to the member outside its own declaration, in any project of the solution. Recursive calls from inside the member do not count.
  • The member does not override another: deleting an override changes what calls through the base class run.
  • No other member overrides or implements it, and it does not implement an interface member: calls reach such members without naming them.

Transformation

  • The member is removed with its documentation comment and the comments directly above it.
  • A field or event declared alongside others loses just its declarator: private int _count, _spare; becomes private int _count;.
  • The blank line that separated the member goes with it, so the members either side stay one blank line apart, and a member that becomes first in its type follows the opening brace directly.

Preserved

  • Every other member, including other overloads of a method.
  • Preprocessor directives around the member, such as #region and #if.

Limitations

  • A field or property initializer with side effects is deleted with the member, so the side effect no longer happens when the type is constructed.
  • Uses by reflection, serialization or string names are not found.
  • Constructors, operators and finalizers are not offered.

Error codes

CodeMeaning
member-referencedcode outside the member refers to it
overrides-memberthe member overrides a base class member
has-overridesa subclass overrides the member
has-implementationsthe member is an interface member that a type implements
implements-interfacethe member implements an interface member
not-a-memberthe target is a type

Cases

·

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

#field-among-declarators

Deletes one unused field from a declaration of several

success
Targetsymbol F:Shop.Counter._spare
Counter.csmodified
11 namespace Shop
22 {
33 public class Counter
44 {
5− private int _count, _spare, _limit = 10;
5+ private int _count, _limit = 10;
66
77 public bool Next() => ++_count < _limit;
88 }
99 }

#first-member-with-comments

Deletes the first member of a class with its documentation comment, keeping the region around it

success
Targetsymbol P:Shop.Customer.Nickname
Customer.csmodified
11 namespace Shop
22 {
33 public class Customer
44 {
55 #region Names
6− /// <summary>What friends call the customer.</summary>
7− public string Nickname { get; set; } = "";
8−
96 // The name on invoices.
107 public string Name { get; set; } = "";
118 #endregion
129 }
1310 }

#implemented-interface-member-refused

Refuses to delete an interface member that a class implements

refusal
Targetsymbol M:Shop.INotifier.Send(System.String)
Refuseshas-implementations; every file is left unchanged
Notifiers.csinput
1 namespace Shop
2 {
3 public interface INotifier
4 {
5 void Send(string message);
6 }
7
8 public class EmailNotifier : INotifier
9 {
10 public void Send(string message)
11 {
12 }
13 }
14 }

#interface-implementation-refused

Refuses to delete a method that implements an interface member

refusal
Targetsymbol M:Shop.EmailNotifier.Send(System.String)
Refusesimplements-interface; every file is left unchanged
Notifiers.csinput
1 namespace Shop
2 {
3 public interface INotifier
4 {
5 void Send(string message);
6 }
7
8 public class EmailNotifier : INotifier
9 {
10 public void Send(string message)
11 {
12 }
13 }
14 }

#not-a-member-refused

Refuses a type, which safe-delete-type deletes

refusal
Targetsymbol T:Shop.Order
Refusesnot-a-member; every file is left unchanged
Order.csinput
1 namespace Shop
2 {
3 public class Order
4 {
5 }
6 }

#overload-kept

Deletes an unused generic overload, leaving the overload that is called

success
Targetsymbol M:Shop.Formatter.Format``1(``0)
Formatter.csmodified
11 namespace Shop
22 {
33 public class Formatter
44 {
55 public string Format(string value) => value.Trim();
66
7− public string Format<T>(T value) => value?.ToString() ?? "";
8−
97 public string Run() => Format(" a ");
108 }
119 }

#overridden-refused

Refuses to delete a virtual method that a subclass overrides

refusal
Targetsymbol M:Shop.Shape.Area
Refuseshas-overrides; every file is left unchanged
Shapes.csinput
1 namespace Shop
2 {
3 public class Shape
4 {
5 public virtual double Area() => 0;
6 }
7
8 public class Square : Shape
9 {
10 public override double Area() => 4;
11 }
12 }

#override-refused

Refuses to delete an override, which would change what calls through the base class run

refusal
Targetsymbol M:Shop.Square.Area
Refusesoverrides-member; every file is left unchanged
Shapes.csinput
1 namespace Shop
2 {
3 public class Shape
4 {
5 public virtual double Area() => 0;
6 }
7
8 public class Square : Shape
9 {
10 public override double Area() => 4;
11 }
12 }

#recursive-method

Deletes a method whose only calls are its own recursive ones

success
Targetsymbol M:Shop.Maths.Factorial(System.Int32)
Maths.csmodified
11 namespace Shop
22 {
33 public static class Maths
44 {
55 public static int Square(int n) => n * n;
6−
7− public static int Factorial(int n) => n <= 1 ? 1 : n * Factorial(n - 1);
86 }
97 }

#referenced-from-another-project-refused

Refuses to delete a property read by a project that references the declaring one

refusal
Targetsymbol P:Core.Product.Sku
ProjectsCore; App → Core
Refusesmember-referenced; every file is left unchanged
App/Label.csinput
1 using Core;
2
3 namespace App
4 {
5 public class Label
6 {
7 public string Print(Product product) => product.Sku;
8 }
9 }
Core/Product.csinput
1 namespace Core
2 {
3 public class Product
4 {
5 public string Sku { get; set; } = "";
6 }
7 }

#referenced-in-another-file-refused

Refuses to delete a method called from another file

refusal
Targetsymbol M:Shop.Order.Total
Refusesmember-referenced mentioning “Checkout.cs”; every file is left unchanged
Checkout.csinput
1 namespace Shop
2 {
3 public class Checkout
4 {
5 public int Pay(Order order) => order.Total();
6 }
7 }
Order.csinput
1 namespace Shop
2 {
3 public class Order
4 {
5 public int Total() => 10;
6 }
7 }

#unused-event

Deletes an event nothing raises or subscribes to

success
Targetsymbol E:Shop.Door.Opened
Projectnullable enable
Door.csmodified
11 using System;
22
33 namespace Shop
44 {
55 public class Door
66 {
77 public event EventHandler? Closed;
88
9− public event EventHandler? Opened;
10−
119 public void Close() => Closed?.Invoke(this, EventArgs.Empty);
1210 }
1311 }

#unused-method

Deletes a method nothing calls, with the blank line that separated it

success
Targetsymbol M:Shop.Order.Legacy
Order.csmodified
11 namespace Shop
22 {
33 public class Order
44 {
55 public int Total() => 10;
66
7− public int Legacy() => Total() * 2;
8−
97 public int Tax() => Total() / 5;
108 }
119 }