Remove unused directives (or "usings") with Code Cleanup on save

Answered

Hey there,

I'd like to let Resharper cleanup unused usings/directives from files, during Code cleanup, on file saving.

Unfortunately I wasn't able to find a option in C# Syntax style or other styling options.

I can manually execute this, but not on save? Am I missing something?

To be more clear, here is a screenshot:

Thanks

Fred

1
7 comments

@..., thank you for your question! Please try enabling the following option: "ReSharper | Options | Code Editing | Code Cleanup | General | Automatically run cleanup when saving a file". Does it work for you?

0

It works, yes. Thanks!

Another related question: which style is responsible for cleaning up the unused directives shown in the screenshot? Is it a Naming, Formatting or Syntax style?

0

@..., thanks for the reply. I'm not sure I fully understood the question. Could you please explain your case in more detail? 

You can turn the inspection off here: "ReSharper | Options | Code Inspection | Inspection Severity | C# | Redundancies in Code | Redundant using directives".

Also, there is an option that allows disabling the removal of unused directories: ReSharper | Options | Code Editing | Code Cleanup | Profiles | Create new Profile | Open "Optimize imports | C#" and uncheck "Optimize 'using' directives".

Please let me know if you have any questions.

1

Thanks again for your answer, Maria Pleskunina.

The second hint was I was looking for, sorry for being to unclear.

 

If checked, the "Optimize 'using' directives" does not only remove redundant/obsolete "using's" from within the C# class file, but also clears not used references (e.g. a project or a NuGet package?) from the project, the class file is located in, am I right?

0

@..., please take a look at what happens when you apply the "Optimize 'using' directives" action here: https://www.jetbrains.com/help/resharper/Code_Cleanup__Index.html#optimize-imports.

Removing unused references is a separate action - https://www.jetbrains.com/help/resharper/Refactorings__Remove_Unused_References.html

0

Alright, now everything comes together. Thanks a lot for your support, Maria Pleskunina!

0

Please sign in to leave a comment.