Skip to main content

Code Cleanup

  • 3 minutes to read

Code Cleanup allows you to specify which Refactorings and Code Formatters CodeRush should apply to a document during cleanup. This feature helps you refactor a large amount of code.

Configure Cleanup

  1. Open the Editor | C# (Visual Basic) | Code Cleanup options page.

  2. Choose rules CodeRush should apply when you call code cleanup and enable “Apply In Action”.

  3. Choose rules CodeRush should apply in code cleanup before a file is saved and enable “Apply on Save”.

    Note

    CodeRush does not clean up a file when you close the unsaved file or solution.

  4. Enable the Apply Code Cleanup when saving a document option to allow CodeRush to clean up a file before it is saved.

  5. Click OK to save and apply the settings.

    CRR_CodeCleanUp

Exclude Generated Code from Code Cleanup

You can specify files and regions that contain generated code on the Editor | All Languages | Generated Code page.

Options

CodeRush skips the specified items in code cleanup and code analysis.

Note

The Generated Code page already includes commonly used file masks and regions for generated code (for example, the “*.Designer.cs” file).

Clean a File Up

Follow the steps below to run code cleanup in an open file:

  1. Place the caret anywhere in your code. Press the Ctrl + . or Ctrl + ~ shortcut to invoke the Code Actions menu.
  2. Choose Code Cleanup from the menu and press Enter.

The screencast below shows the code cleanup with applied Organize members and Format document rules.

CleanUpCall

You can also use the following methods to execute code cleanup:

Visual Studio menu

Open the CodeRush | Edit menu and choose Cleanup Document.

CleanUpCall

Visual Studio toolbar

Click the Run Code Cleanup button on the CodeRush Visualize toolbar.

CleanUpCall

Clean a File Up on Save

Click the Save button or press Ctrl + S in the editor.

The screencast below shows the code cleanup with applied Organize members and Format document rules.

CleanUpCall

Clean a Project Up

Follow the steps below to run code cleanup in a project:

  1. Right-click the project you want to clean in the Solution Explorer.
  2. Select CodeRush Cleanup in the context menu.

    CleanUpProject

CodeRush shows a progress window when it calculates changes that Code Cleanup should apply in code.

Style_CleanUpProject

If you cancel the process, the code remains unchanged.

Note

Use Visual Studio’s Undo action (Edit | Undo menu item) to undo the cleanup after its completion. This action is available when a document is open.

CodeRush applies the changes to documents when the calculation is finished.

ApplyClean

Clean a Folder Up

You can run Code Cleanup for a selected folder (folders) in the Solution Explorer.

Right-click the folder (folders) in which you want to clean files and select CodeRush Cleanup.

Style_CleanUpProject

You can cancel code cleanup while it is still in progress, all code remains unchanged.

Style_CleanUpProject

See Also