Code Cleanup
Control+Alt+F
ReSharper_CleanupCode
ReSharper allows you to apply formatting and other code style preferences in a bulk mode to instantly eliminate code style violations in one or more files, in a project or in the entire solution.
Code cleanup profiles
Code cleanup can have various configurations (profiles) to apply in different cases according to your needs. There are three default profiles available out of the box:
Built-in: Reformat Code that only applies code formatting preferences
Built-in: Reformat & Apply Syntax Style that applies code formatting preferences and code syntax styles.
Built-in: Full Cleanup that applies all available cleanup tasks except updating file header.
These profiles help in most cases but you cannot configure them. If you want to run code cleanup with the custom set of cleanup tasks, you need to create a custom profile and specify the set of desired tasks there.
Create a new custom cleanup profile
Select Alt+R O.
from the main menu or pressGo to the cleanup profiles settings page:
.Select one of the existing profiles on the left and click Duplicate on the top of the page. ReSharper will create a new cleanup profile with the same set of tasks as in original profile.
Select the newly created profile in the list of profiles.
Click Configure on the top of the page.
The tasks of the selected profile become editable on the right. Configure them as desired.
If you want to create or edit other profiles before saving your modifications, click Done on the top of the page to temporarily remember modifications to the current profile.
Click Save in the Options dialog to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see Manage and Share ReSharper Settings.
Your newly created cleanup profile will be available in the Code Cleanup dialog when you are running code cleanup. You can also set this profile to be used for the silent cleanup.
Run code cleanup
Control+Alt+F
ReSharper_CleanupCode
ReSharper can apply code cleanup to an arbitrarily selected block of code, all code in the current file, all files in a directory, the current project, or even the current solution. Make sure that you select the right set of cleanup tasks by choosing the right cleanup profile.
Clean up the selection or the whole file
Select the scope where you want to clean up code:
Make a selection in the editor to clean up code in the selection.
Set the caret anywhere in the file to clean up code in the file.
Select one or more items in the Solution Explorer to clean up code in files under these items and their child items.
Do one of the following:
Press Control+Alt+F or choose
from the main menu.Right-click anywhere in the text editor or right-click the selection and choose Cleanup Code in the context menu.
In the Code Cleanup dialog that opens, select a cleanup profile on the left. Use the area on the right to check the tasks included in the selected profile.
If none of the available profiles suits you, choose one of the existing profiles and click Copy to create a new custom profile.
Note that if you create or modify cleanup profiles in this dialog, your changes will be saved using the smart save logic. If you need to save your modifications to a shared settings layer, make your modifications on the page of ReSharper options, click Save To and then choose the desired settings layer.Click Run to clean code in the selected scope.
By default, files that are opened in the editor are not saved automatically after cleanup. If you want to enable auto-saving after cleanup, select Save after cleanup is applied to the whole file on the page of ReSharper options(Alt+R, O).
You can run any cleanup tasks — for example, reformat code — every time you save a file, to make sure that your edits always comply with your code style.
Run code cleanup automatically on save
Select Alt+R O.
from the main menu or pressGo to the cleanup profiles settings page:
.Select a cleanup profile that you want to apply on save and click Set as default. The default profile is also used for silent cleanup.
Go to the Automatically run cleanup when saving a file.
options page and selectOptionally, you can restrict automatic cleanup to specific files by a file mask. You can also select Only changed parts of file to make sure that cleanup is applied to the code affected by your changes, and the rest of the code in the file is not modified.
Click Save in the Options dialog to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see Manage and Share ReSharper Settings.
The next time you finish editing and save the file (Ctrl+S) or all files (Ctrl+Shift+S), ReSharper will clean up the affected files using the selected profile. If the default cleanup profile is not selected, ReSharper will prompt you choose one of the profiles.
You can clean up not only an individual file, but also multiple files in a directory, project or entire solution. To do so, you should run code cleanup from the Solution Explorer.
Clean up multiple files, projects, or entire solution
Select one or more items (files, directories, projects, or the solution) in the Solution Explorer.
Do one of the following:
Press Control+Alt+F or choose
from the main menu.Right-click the selection and choose Cleanup Code in the context menu.
Choose a cleanup profile as described above.
Click Run to clean code in the selected scope.
You can also run code cleanup in files that you recently opened or modified.
Clean up recently modified files
Expand the submenu at ReSharper | Edit | Cleanup Recent Files.
Choose whether to run cleanup for all opened files or for all recently modified files.
Cleanup... actions wil prompt you to choose a cleanup profile, Silent Cleanup... actions will use the default cleanup profile selected on the page of ReSharper options(Alt+R, O).If you choose to clean up recently modified files, ReSharper will apply the selected cleanup profile to all files in the solution that were edited during the last 7 days.
You can change the number of days during which changes should be considered recent on the options page.You can also select Only changed parts of file to make sure that cleanup is applied to the code affected by your changes, and the rest of the code in the file is not modified.
Silent cleanup
Control+Shift+Alt+F
ReSharper_SilentCleanupCode
You can also run code cleanup in the silent mode - that is, without displaying the Code Cleanup dialog. Since in this case you will not be able to choose a profile explicitly, you have to assign some cleanup profile to use for the silent code cleanup.
Assign a profile for silent code cleanup
Select Alt+R O.
from the main menu or pressGo to the cleanup profiles settings page:
.Select a cleanup profile on the left and click Set as default at the top of the options page.
Click Save in the Options dialog to apply the modifications and let ReSharper choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, see Manage and Share ReSharper Settings.
Run code cleanup in silent mode
Do one of the following:
To run silent code cleanup in a specific code file, open it in the text editor or select it in the Solution Explorer.
To apply silent code cleanup to a set of files (directory, project, or solution), select the corresponding item in the Solution Explorer.
Press Control+Shift+Alt+F or choose
from the main menu. Alternatively, you can press Control+Shift+A, start typing the command name in the popup, and then choose it there.
Available code cleanup tasks
The tables below list available cleanup tasks for different languages. If you need the cleanup to perform only a part of these tasks, you can create a custom cleanup profile for that.
C#
Apply file layout | Reorders type members in files according to the rules configurable on the Alt+R, O). For more information, see Rearrange Members with File and Type Layout Patterns. | page of ReSharper options(
Update file header | Inserts or updates the file header comment that you can configure on the File Header Style. | page of ReSharper options. For more information, see
Syntax Styles | |
Apply 'var' style | Applies the rules for 'var' usage during the cleanup. The rules are configurable on the page of ReSharper options(Alt+R, O). |
Use explicit or implicit modifier definition for types | Applies the rule for type modifiers - whether on not to use the internal modifier explicitly. The rule is configurable on the page of ReSharper options(Alt+R, O). For more information, see Code Syntax Style: Modifiers. |
Use explicit or implicit modifier definition for type members | Applies the rule for type member modifiers - whether on not to use the private modifier explicitly. The rule is configurable on the page of ReSharper options(Alt+R, O). For more information, see Code Syntax Style: Modifiers. |
Sort modifiers | Applies the preferred order of type/member modifiers. The rule is configurable on the Alt+R, O). For more information, see Code Syntax Style: Modifiers. | page of ReSharper options(
Apply arguments style (named | positional) | Applies the preferences for named/positional arguments for specific types of parameters. The preferences are configurable on the Alt+R, O). For more information, see Code Syntax Style: Named/Positional Arguments. | page of ReSharper options(
Remove redundant parentheses | Removes redundant optional parentheses in cases specified on the Alt+R, O). For more information, see Code Syntax Style: Optional Parentheses. | page of ReSharper options(
Add parentheses to explicitly specify operation precedence | Adds optional parentheses to clarify precedence of operations in cases specified on the Alt+R, O). For more information, see Code Syntax Style: Optional Parentheses. | page of ReSharper options(
Add/remove braces around single statements | Adds or removes optional braces around single nested statements as specified on the Alt+R, O). For more information, see Code Syntax Style: Braces for Single Nested Statements. | page of ReSharper options(
Arrange attributes | Applies the rules for multiple attributes as specified on the Alt+R, O). For more information, see Code Syntax Style: Multiple Attributes. | page of ReSharper options(
Apply code body style | Applies preferences for bodies of type members (expression body | statement body) as specified on the Alt+R, O). For more information, see Code Syntax Style: Bodies of Type Members. | page of ReSharper options(
Arrange trailing commas | Applies preferences for trailing commas in declarations with multiple items and similar constructs (object, array, and collection initializers, as well as enums and switch expressions) as specified on the Alt+R, O). For more information, see Code Syntax Style: Trailing Commas. | page of ReSharper options(
Apply object creation style ('new()' vs 'new T()') | Applies preferences for using the target-typed new operator in C# 9.0 and later as specified on the page of ReSharper options(Alt+R, O). For more information, see Code Syntax Style: Object Creation ('new()' vs 'new T()'). |
Apply default value style ('default' vs 'default(T)') | Applies preferences for using optional type specification in default value expressions as specified on the Alt+R, O). For more information, see Code Syntax Style: Default Value Expressions ('default' vs 'default(T)'). | page of ReSharper options(
Remove code redundancies | On the Alt+R, O), in the Redundancies in Code categories for most languages, ReSharper lists over 50 redundancies that have associated code inspections. Some of them require special attention, and you cannot process them in the batch mode. That leaves Code Cleanup with about 30 redundancies that it is able to remove automatically whenever you select Remove code redundancies in your cleanup profile.
|
Use auto-property, if possible | Replaces simple properties and corresponding backing fields with auto-properties. For more information, see Use Auto-Properties. |
Make field read-only, if possible | Adds the 'readonly' keyword to fields that are assigned in the declaration or in a constructor and only have read usages. For more information, see Make Fields Readonly. |
Make auto-property get-only, if possible | Removes the redundant set accessor on auto-properties that are initialized from the constructor/initializer and have no write usages. Note that this task only works for C# 6.0. By default, ReSharper automatically detects C# version based on the associated compiler. However, you can specify the target C# version explicitly — right-click the project in the Solution Explorer, choose Edit project item properties from the context menu and use the C# Language Level selector. |
Arrange qualifiers | Applies the rules for qualifying type members. The rules are configurable on the Alt+R, O). For more information, see Code Syntax Style: Optional Member Qualifiers. | page of ReSharper options(
Fix built-in type references | Applies the rule for built-in type names (C# keyword or CLR type name). The rule is configurable on the Alt+R, O). For more information, see Code Syntax Style: Built-In Type References. | page of ReSharper options(
Usings | Includes the following configurable options:
|
Shorten qualified references | Replaces fully qualified names with short names when possible by importing namespaces, implementing settings configured on the Alt+R, O). | page of ReSharper options(
Reformat code | Reformats your code according to the preferences that you can configure on the Alt+R, O). For more information, see Manage and Apply Code Formatting Rules. | page of ReSharper options(
Reformat embedded XML doc comments | Reformats XML doc comments according to options configurable on the Alt+R, O). Note that ReSharper does not reformat plain comments like: | page of ReSharper options(
XAML
Collapse empty tags | Removes the closing tag if the tag body is empty. |
Remove redundant attributes | Removes attributes, such as Freeze , modifiers , name , span , UpdateSourceTrigger , Mode if they are redundant in the context |
Remove redundant resource | Removes resource references that are not used |
Remove redundant property setters | Removes unused setters for the following properties: property , attached , style . |
Remove redundant namespace alias | Remove all redundant namespace aliases in the scope of cleanup. Redundant aliases are also detected with the code inspection and can be removed with a quick-fix. |
Remove redundant definitions | Removes definitions if they are redundant in the context |
Remove forbidden attributes | Removes attributes, such as name , span if they are forbidden in the context |
XML
Reformat code | Reformats your code according to the preferences that you can configure on the Alt+R, O). For more information, see Manage and Apply Code Formatting Rules. | page of ReSharper options(
C++
C++ code styles and code redundancies |
|
Apply Clang-tidy fix-its | In this node, you can choose all or specific fixes that will be applied according to the Clang-Tidy checks. The selected fixes will be applied even if the corresponding inspections are turned off on the Alt+R, O). Be careful when enabling too many checks at once — code cleanup runs Clang-Tidy only once for a given file, so if multiple fixes touch the same block of code, the changes might conflict with each other. page of ReSharper options( |
Reformat code | Reformats your code according to the preferences that you can configure on the Alt+R, O). For more information, see Manage and Apply Code Formatting Rules. | page of ReSharper options(
Update file header | Inserts or updates the file header comment that you can configure on the File Header Style. | page of ReSharper options. For more information, see
VB.NET
Remove code redundancies | Removes redundant code the same way as for C# Language |
Optimize 'import' directives | Removes unused namespace import directives implementing rules configured on the | page of ReSharper options.
Shorten qualified references | Replaces fully qualified names with short names when possible by importing namespaces, implementing settings configured on the Alt+R, O). | page of ReSharper options(
Reformat code | Reformats your code according to the preferences that you can configure on the Alt+R, O). For more information, see Manage and Apply Code Formatting Rules. | page of ReSharper options(
Reformat embedded XML doc comments | Reformats XML doc comments according to options configurable on the Alt+R, O). Note that ReSharper does not reformat plain comments like: | page of ReSharper options(
CSS
Alphabetize properties | Rearranges CSS properties in the alphabetical order. |
Reformat code | Reformats your code according to the preferences that you can configure on the Alt+R, O). For more information, see Manage and Apply Code Formatting Rules. | page of ReSharper options(
JavaScript-like languages
Apply statement termination | Adds the optional semicolon ; at the end of statements in JavaScript, TypeScript, and JSON. |
JavaScript
Normalize quotes around string literal | Applies the preferred quote style: single quote (') or double quote ("). The preference can be configured on the Alt+R, O). For more information, see Quotes style. | page of ReSharper options(
Correct 'var' to 'let'/'const' where possible |
|
Move let/const to most possible inner scopes | Moves variables defined outside of a function/compound statement, and so on closer to their usages if there are no other usages in outer scopes. |
Convert string concatenations to template strings | In JavaScript code, if the target JavaScript language level is ECMAScript 6 (you can choose it on the Alt+R, O)), as well as in TypeScript 1.4 or later, this option will convert string concatenations to template strings. For example:
var str1 = "Hello,";
var str2 = "Test: " + str1 + " World1";
will be converted to
const str1 = "Hello, ";
const str2 = `Test: ${str1} World1`;
| page of ReSharper options(
Reformat code | Reformats your code according to the preferences that you can configure on the Alt+R, O). For more information, see Manage and Apply Code Formatting Rules. | page of ReSharper options(
Reformat embedded XML doc comments | Reformats XML doc comments according to options configurable on the Alt+R, O). Note that ReSharper does not reformat plain comments like: | page of ReSharper options(
TypeScript
Remove redundant qualifiers | Use this option to remove redundant type name qualifiers. |
Optimize 'import' statements | Use this option to remove redundant 'import' statements or fully-qualified names according to the settings configurable on the Alt+R, O). | page of ReSharper options(
Optimize reference comments | Use this option to remove redundant reference comments. |
Synchronize 'public' presence with code style | This option lets you add or remove explicit 'public' modifier according to settings configurable on the Alt+R, O). | page of ReSharper options(
Add explicit 'any' type annotation if needed | This option lets you add explicit 'any' annotation to types according to settings configurable on the Alt+R, O). | page of ReSharper options(
Synchronize type annotation presence with code style | This option lets you enforce explicit/implicit typing according to settings configurable on the Alt+R, O). | page of ReSharper options(
Fix relative path style in 'require' if needed | Applies the Relative file reference style in 'require' code style preference for relative paths in the require keyword. For example, import X = require("file1") or import X = require("./file1") . You can configure this preference on the page of ReSharper options(Alt+R, O). |
Use 'as' instead of type assertions | Replaces var bar = <string> foo; with var bar = foo as string; which is a recommended way to avoid ambiguity with JSX syntax. |
HTML
Reformat code | Reformats your code according to the preferences that you can configure on the Alt+R, O). For more information, see Manage and Apply Code Formatting Rules. | page of ReSharper options(
Normalize quotes around attribute value | Applies the preferred quote style: single quote (') or double quote ("), configurable on the | page of ReSharper options.
ASP.NET
Optimize @Register directives | Removes unused @Register directives. |