ReSharper 2017.1 Help

Code Cleanup

ReSharper | Edit | Cleanup Code
Ctrl+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.

In this topic:

Code cleanup profiles

Code cleanup can have various configurations (profiles) to apply in different cases according to your needs. There are two default profiles available out of the box:

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.

To create a new custom cleanup profile

  1. Open the Code Cleanup options: ReSharper | Options | Code Editing | Code Cleanup.
  2. Select one of the existing profiles on the left and click Copy on the top of the page. ReSharper will create a new profile with the same set of tasks as in original profile.
  3. Make sure that your new profile is selected in the list of profiles in the Code Cleanup options page.
  4. Click Configure on the top of the page.
  5. Preferences of the selected profile become editable on the right. Configure them ad desired.
  6. 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.
  7. Click Save 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 drop-down list. For more information, see Managing and Sharing 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.

Running code cleanup

ReSharper | Edit | Cleanup Code
Ctrl+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.

To clean up the selection or the whole file

  1. Select the scope where you want to cleanup code:
    • Make a selection in the editor to cleanup code in the selection.
    • Set the caret anywhere in the file to cleanup code in the file.
    • Select one or more items in the Solution Explorer to cleanup code in files under these items and their child items.
  2. Do one of the following:
    • Press Ctrl+Alt+F or choose ReSharper | Edit | Cleanup Code in the main menu.
    • Right-click anywhere in the text editor or right-click the selection and choose Cleanup Code in the context menu.
  3. 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.
  4. 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 Code Editing | Code Cleanup page of ReSharper options, click Save To and then choose the desired settings layer.
  5. Click Run as soon as the desired cleanup profile is selected.

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.

To clean up multiple files, projects, or entire solution

  1. Select one or more items (files, directories, projects, or the solution) in the Solution Explorer.
  2. Do one of the following:
    • Press Ctrl+Alt+F or choose ReSharper | Edit | Cleanup Code in the main menu.
    • Right-click the selection and choose Cleanup Code in the context menu.
  3. Choose a cleanup profile as described above.
  4. Click Run.

Silent cleanup

ReSharper | Edit | Silent Cleanup Code
Ctrl+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.

To assign a profile for silent code cleanup

  1. Open the Code Cleanup options: ReSharper | Options | Code Editing | Code Cleanup.
  2. Select a cleanup profile on the left and click Set as default on the top of the options page.
  3. Click Save 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 drop-down list. For more information, see Managing and Sharing ReSharper Settings.

To run code cleanup in silent mode

  1. 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.
  2. Press Ctrl+Shift+Alt+F or choose ReSharper | Edit | Silent Cleanup Code in the main menu.

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 taskDescription
C#
Apply file layout Reorders type members in files according to the rules configurable on the Code Editing | C# | File Layout page of ReSharper options. For more information, see File and Type Layout.
Update file header Inserts or updates the file header comment that you can configure on the Code Editing | File Header Text 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 Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: 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 Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: Modifiers.
Sort modifiers Applies the preferred order of type/member modifiers. The rule is configurable on the Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: Modifiers.
Apply arguments style (named | positional) Applies the preferences for named/positional arguments for specific types of parameters. The preferences are configurable on the Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: Named/Positional Arguments.
Remove redundant parentheses Removes redundant optional parentheses in cases specified on the Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: Optional Parentheses.
Add parentheses to explicitly specify operation precedence Adds optional parentheses to clarify precedence of operations in cases specified on the Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: Optional Parentheses.
Add/remove braces around single statements Adds or removes optional braces around single nested statements as specified on the Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: Braces for Single Nested Statements.
Arrange attributes Applies the rules for multiple attributes as specified on the Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: Multiple Attributes.
Enforce 'var' keyword usage settings Applies the rules for 'var' usage during the cleanup. The rules are configurable in the Code Editing | C# | Code Style page of ReSharper options.
Remove code redundancies On the Code Inspection | Inspection Severity 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:
  • Removes:
    • Redundant empty constructors.
    • Explicit integer type specifiers in enumerations.
    • true specifiers in for statements.
    • Redundant type casts.
    • Redundant parentheses from attributes without arguments.
    • Explicit calls to base class constructors without arguments.
    • Redundant boolean comparisons.
    • Redundant braces from collection initializers with a single expression.
    • Redundant explicit delegate creation expressions.
    • Empty argument lists from object/collection initializers.
    • Redundant new keywords.
    • Redundant partial modifiers on type declarations where a type has a single part.
    • Redundant unsafe contexts and statements.
    • Redundant object.ToString() calls (for example, in a concatenation with a string or as an argument of a string.Format() call).
    • Redundant string.ToCharArray() calls.
    • Redundant specifications of argument types.
    • Signatures in anonymous methods where its parameters are not used in the method body.
    • Non-referenced labels.
    • Redundant field initializers.
    • Redundant explicit names in anonymous type properties.
    • Redundant nullable types where value type is implicitly convertible to nullable.
  • Converts:
    • Nullable types to short form Nullable<T> to T?).
    • Static method invocations to extension method calls where extension methods are invoked as static methods.
    • To implicitly typed array creation expressions when array type can be inferred from initializer.
  • Fixes:
    • Occurrences where static members of a type are accessed via derived members of the type.
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 View | Properties Window in the menu).
Arrange qualifiers Applies the rules for qualifying type members. The rules are configurable on the Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: 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 Code Editing | C# | Code Style page of ReSharper options. For more information, see Code Syntax Style: Built-In Type References.
Optimize 'using' directives Provides the following configurable options:
  • Optimize 'using' directives
    Removes unused namespace import directives by implementing rules configured on the Code Editing | C# | Namespace Imports and Code Editing | C# | Code Style options pages and sorts the namespace import directives: all System.* namespaces go first, sorted alphabetically by the second word after dot; all the rest namespaces go next, in alphabetical order.. For more information, see Code Syntax Style: Namespace Imports.
  • Embrace 'using' directives in region
    If this check box is selected, a new region for wrapping all namespace import directives is created during cleanup.
  • Region name
    Allows specifying a name for the region wrapping namespace import directives.
Shorten qualified references Replaces fully qualified names with short names when possible by importing namespaces, implementing settings configured on the Code Editing | C# | Code Style page of ReSharper options.
Reformat code Reformats your code according to options configurable in the Formatting Style. For more information, see Managing and Applying Code Formatting Rules.
Reformat embedded XML doc comments Reformats XML doc comments according to options configurable on the Code Editing | XML Doc Comments | Formatting Style page of ReSharper options.

Note that ReSharper does not reformat plain comments like:
// A plain single-line comment
or
/* A plain multi-line comment */

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 Managing and Applying Code Formatting Rules.
Normalize quotes around attribute value Applies the preferred quote style: single quote (') or double quote ("), configurable on the Code Editing | HTML | Code Style 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 Code Editing | TypeScript | Code Style 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:
  • In JavaScript code, if the target JavaScript language level is ECMAScropt 6 (you can choose it on the Code Editing | JavaScript | Inspections page of ReSharper options)
  • In TypeScript 1.4, if the target ECMAScript 6 is selected in the project properties.
  • In TypeScript 1.5 or above.
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 Code Editing | JavaScript | Inspections 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";
will be converted to
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 Managing and Applying Code Formatting Rules.
Reformat embedded XML doc comments Reformats XML doc comments according to options configurable on the Code Editing | XML Doc Comments | Formatting Style page of ReSharper options.

Note that ReSharper does not reformat plain comments like:
// A plain single-line comment
or
/* A plain multi-line comment */

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 Code Editing | TypeScript | Code Style 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 Code Editing | TypeScript | Code Style 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 Code Editing | TypeScript | Code Style 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 Code Editing | TypeScript | Code Style 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;
with
var bar = foo as string;
which is a recommended way to avoid ambiguity with JSX syntax.
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 Managing and Applying Code Formatting Rules.
C++
Code redundancies
  • Add 'override' specifier to overriding functions
  • Fix slashes in include file paths
  • Replace constants in boolean context with boolean literals
  • Replace zero valued expressions in pointer context with nullptr
  • Replace smart pointer constructors with make functions
  • Remove redundant 'else' keywords
  • Remove redundant qualifiers
  • Remove redundant statements
  • Remove redundant 'typename' and 'template' keywords
  • Sort member initializers by the order of initialization
  • Remove unreachable code
  • Remove unused #include directives
  • Make member functions 'const' when possible
  • Make member functions 'static' when possible
Reformat code Reformats your code according to options configurable in the Code Style Assistance in C++. For more information, see Managing and Applying Code Formatting Rules.
Update file header Inserts or updates the file header comment that you can configure on the Code Editing | File Header Text page of ReSharper options. For more information, see File Header Style.
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 Code Editing | Visual Basic.NET | Namespace Imports page of ReSharper options.
Shorten qualified references Replaces fully qualified names with short names when possible by importing namespaces, implementing settings configured on the Code Editing | Visual Basic.NET | Code Style page of ReSharper options.
Reformat code Reformats your code according to options configurable in the Formatting Style. For more information, see Managing and Applying Code Formatting Rules.
Reformat embedded XML doc comments Reformats XML doc comments according to options configurable on the Code Editing | XML Doc Comments | Formatting Style page of ReSharper options.

Note that ReSharper does not reformat plain comments like:
// A plain single-line comment
or
/* A plain multi-line comment */

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 Managing and Applying Code Formatting Rules.
Last modified: 12 October 2017

See Also