RefactorMCP catalog

Cleanup Usings

Removes the using directives in a file that nothing in it needs.

Precondition

  • None. A file whose directives are all used is left unchanged.

Transformation

  • Each directive the compiler reports as unnecessary is removed: ordinary, static and alias directives alike, at the top of the file or inside a namespace.
  • A directive needed only for extension methods, such as using System.Linq; for Where, is used and stays.
  • A header comment above the first directive stays at the top of the file.
  • When every directive of a file or namespace goes, so does the blank line that followed them.

Preserved

  • Everything else in the file exactly as written: nothing is reformatted.
  • The order of the directives that remain.

Limitations

  • Directives are not sorted, and missing ones are not added.
  • Only the one file is cleaned; other files keep their unnecessary directives.

Error codes

None.

Cases

·

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

#aliases-and-static

Treats alias and static directives like the others: kept when used, removed when not

success
TargetSample.cs
Sample.csmodified
11 using static System.Math;
2−using static System.Console;
3−using Text = System.Text.StringBuilder;
42 using Names = System.Collections.Generic.List<string>;
53
64 namespace Shop
75 {
86 public class Sample
97 {
108 public double Root(double value) => Sqrt(value);
119
1210 public Names Empty() => new Names();
1311 }
1412 }

#all-unused

Removes every using directive and the blank line that followed them

success
TargetSample.cs
Sample.csmodified
1−using System;
2−using System.Text;
3−
41 namespace Shop
52 {
63 public class Sample
74 {
85 public int Answer() => 42;
96 }
107 }

#inside-namespace

Removes unused directives declared inside a namespace

success
TargetSample.cs
Sample.csmodified
11 namespace Shop
22 {
33 using System;
4− using System.IO;
54
65 public class Sample
76 {
87 public void Say() => Console.WriteLine("Hi");
98 }
109 }

#keeps-header-comment

Keeps the file's header comment when the first using directive is removed

success
TargetSample.cs
Sample.csmodified
11 // Copyright Shop Ltd.
2−using System.Text;
32 using System;
43
54 namespace Shop
65 {
76 public class Sample
87 {
98 public void Say() => Console.WriteLine("Hi");
109 }
1110 }

#nothing-to-remove

Leaves a file whose directives are all used unchanged

success
TargetSample.cs
Sample.csunchanged
11 using System;
22
33 namespace Shop
44 {
55 public class Sample
66 {
77 public void Say() => Console.WriteLine("Hi");
88 }
99 }

#removes-unused

Removes the using directives nothing in the file needs, keeping the one an extension method call needs and leaving the rest of the file as written

success
TargetSample.cs
Sample.csmodified
11 using System;
2−using System.Collections.Generic;
32 using System.Linq;
4−using System.Text;
53
64 namespace Shop
75 {
86 public class Sample
97 {
108 public int Count(int[] values) => values.Where(v => v>0).Count();
119
1210 public void Say() => Console.WriteLine("Hi");
1311 }
1412 }