Code Cleanup
Ctrl+Alt+F
ReSharper_CleanupCode
Code cleanup allows you to apply a number of ReSharper's code style features (see the table below) in a bulk mode to instantly eliminate code style violations in one or more files, a project or in the entire solution.
In this topic:
Also in this section:
Code cleanup profiles
Code cleanup can have various configurations (profiles) to apply in different cases according to your needs. There are two default profiles that can be applied out-of-the-box:
- Reformat Code that only applies code formatting rules
- Full Cleanup that applies all available cleanup tasks except updating file header.
Available code cleanup tasks
The table below lists available cleanup tasks. If you need the cleanup to perform only a part of these tasks, you can create a custom cleanup profile for that.
Cleanup task | Description |
---|---|
C# | |
Apply file layout | Reorders type members in files according to the rules configurable on the page of ReSharper options. For more information, see File and Type Layout. |
Update file header | Inserts or updates file header that you can configure on the page of ReSharper options. For more information, see File Header Style. |
Code Styles | |
Use explicit or implicit modifier definitions 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. For more information, see Arranging Modifiers. |
Use explicit or implicit modifier definitions 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. For more information, see Arranging Modifiers. |
Sort modifiers | Applies the preferred order of type/member modifiers. The rule is configurable on the page of ReSharper options. For more information, see Arranging Modifiers. |
Apply arguments style (named | positional) | Applies the preferences for named/positional arguments for specific types of parameters. The preferences are configurable on the page of ReSharper options. For more information, see Using Named/Positional Arguments. |
Remove redundant parentheses | Removes redundant optional parentheses in cases specified on the page of ReSharper options. For more information, see Arranging Optional Parentheses. |
Add parentheses to explicitly specify operation precedence | Adds optional parentheses to clarify precedence of operations in cases specified on the page of ReSharper options. For more information, see Arranging Optional Parentheses. |
Add/remove braces around single statements | Adds or removes optional braces around single nested statements as specified on the page of ReSharper options. For more information, see Using Braces for Single Nested Statements. |
Arrange attributes | Applies the rules for multiple attributes as specified on the page of ReSharper options. For more information, see Attributes Style. |
Enforce 'var' keyword usage settings | Applies the rules for 'var' usage during the cleanup. The rules are configurable in the page of ReSharper options. |
Remove code redundancies | On the page of ReSharper options, 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 simply cannot process them in 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. Specifically, code cleanup:
|
Use auto-property, if possible | Replaces simple properties and corresponding backing fields with auto-properties. For more information, see Using 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 Making 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 by selecting the project in the Solution Explorer and using the C# Language Level property in the Visual Studio's Properties window (choose in the menu). |
Arrange qualifiers | Applies the rules for qualifying type members. The rules are configurable on the page of ReSharper options. For more information, see Arranging Optional Member Qualifiers. |
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 page of ReSharper options. For more information, see Built-In Type References Style. |
Optimize 'using' directives | Provides the following configurable options:
|
Shorten qualified references | Replaces fully qualified names with short names when possible by importing namespaces, implementing settings configured on the page of ReSharper options. |
Reformat code | Reformats your code according to options configurable in the Formatting Style. For more information, see Code Formatting Rules. |
Reformat embedded XML doc comments | Reformats XML doc comments according to options configurable on the page of ReSharper options. Note that ReSharper does not reformat plain comments like: |
ASP.NET | |
Optimize @Register directives | Removes unused @Register directives. |
HTML | |
Reformat code | Reformats your code according to options configurable in the Formatting Style. For more information, see Code Formatting Rules. |
Normalize quotes around attribute value | Applies the preferred quote style: single quote (') or double quote ("), configurable on the page of ReSharper options. |
JavaScript/TypeScript | |
Terminates statements | Terminates JavaScript statements with the semicolon. |
Normalize quotes around string literal | Applies the preferred quote style: single quote (') or double quote ("). The preference can be configured on the page of ReSharper options. For more information, see Code Style Assistance in JavaScript. |
Correct 'var' to 'let'/'const' where possible | let and const are supported in the following cases: |
Move block-scope variables to most possible inner scopes | Moves variables defined outside of a function/compound statement, etc. 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 ECMAScropt 6 (you can choose it on the page of ReSharper options), as well ad in TypeScript 1.4 or above, this option will convert string concatenations to template strings. For example: var str1 = "Hello,";
var str2 = "Test: " + str1 + " World1"; const str1 = "Hello, ";
const str2 = `Test: ${str1} World1`; |
Reformat code | Reformats your code according to options configurable in the Formatting Style. For more information, see Code Formatting Rules. |
Reformat embedded XML doc comments | Reformats XML doc comments according to options configurable on the page of ReSharper options. Note that ReSharper does not reformat plain comments like: |
TypeScript specific | |
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 page of ReSharper options. |
Optimize reference comments | Use this option to remove redundant reference comments. |
Synchronize 'public' presence with code style | This options lets you add or remove explicit 'public' modifier according to settings configurable on the page of ReSharper options. |
Add explicit 'any' type annotation if needed | This options lets you add explicit 'any' annotation to types according to settings configurable on the page of ReSharper options. |
Synchronize type annotation presence with code style | This options lets you enforce explicit/implicit typing according to settings configurable on the 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") . |
Use 'as' instead of type assertions | Replaces var bar = <string> foo; var bar = foo as string; |
XAML | |
Collapse empty tags | Removes the closing tag if the tag is empty. |
XML | |
Reformat code | Reformats your code according to options configurable in the Formatting Style. For more information, see Code Formatting Rules. |
C++ | |
Reformat code | Reformats your code according to options configurable in the Code Style Assistance in C++. For more information, see Code Formatting Rules. |
Visual Basic.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 page of ReSharper options. |
Reformat code | Reformats your code according to options configurable in the Formatting Style. For more information, see Code Formatting Rules. |
Reformat embedded XML doc comments | Reformats XML doc comments according to options configurable on the page of ReSharper options. Note that ReSharper does not reformat plain comments like: |
CSS | |
Alphabetize properties | Rearranges CSS properties in the alphabetical order. |
Reformat code | Reformats your code according to options configurable in the Formatting Style. For more information, see Code Formatting Rules. |
Fixing inconsistent naming is not included in code cleanup because when batch-applied it can lead to naming conflicts. To learn how to deal with inconsistent naming, see Naming Style.