ReSharper Help

Code Cleanup

ReSharper | Options | Code Editing | Code Cleanup

This page of ReSharper options allows you to manage and configure custom Code Cleanup profiles, as well as to assign a cleanup profile for the silent code cleanup.

ReSharper also provides two default cleanup profiles, (Full Cleanup and Reformat Code) that you can use but cannot modify. These two profiles are not displayed on this page.

ItemDescription
Profiles List of available custom code cleanup profiles. See Configuring Code Clenup for more information.
Add Adds a new empty profile, prompting you to specify a name for it.
Rename Renames the selected profile.
Remove Deletes the selected profile without prompting for confirmation.
Profile to use with silent cleanup Allows choosing one of the default cleanup profiles or a custom cleanup profile that will be used for silent code cleanup. ReSharper does not make a default selection of the silent cleanup profile. It means that until you select a profile by yourself, ReSharper will open the Code Cleanup dialog box whenever you launch silent code cleanup.

Selected profile settings

Use this properties to edit the profile selected in the list of custom profiles above. Initially, there are no custom profiles. You can click Add to create one.

Profile settingDescription
C#
Apply file layout Reorders type members in files according to the rules configurable in the Code Editing | C# | File Layout options page. For more information, see File and Type Layout.
Update file header Inserts or updates file header that you can configure in the Code Editing | File Header Text options page. For more information, see File Header Style.
Remove code redundancies

In the Code Inspection | Inspection Severity options page, in the Redundancies in Code category, 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.

While a code cleanup profile provides a single check box that allows you remove all kinds of code redundancies listed below, you can still configure ReSharper to ignore some kinds of redundancies. To ignore specific redundancies, set the severity level of the corresponding code inspections to Do not show

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 from properties that are initialized from the constructor 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 project settings. 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. ).
Arrange qualifiers Applies the rules for qualifying type members. The rules are configurable in the Code Editing | C# | Code Style options page. 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 in the Code Editing | C# | Code Style options page. For more information, see Built-in Type References Style.
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 options page.
Optimize 'using' directives Provides the following configurable options:
  • Optimize 'using' directives
    Removes unused namespace import directives by implementing rules configured in 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 Namespace Imports Assistance.
  • 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 where possible by importing namespaces, implementing settings configured in the Code Editing | C# | Code Style options page.
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 in the Code Editing | XML Doc Comments | Formatting Style options page.

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 Code Formatting Rules.
Normalize quotes around attribute value Applies the preferred quote style: single quote (') or double quote ("), configurable in the Code Editing | HTML | Code Style options page.
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 options page)
  • 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 options page) , 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 Code Formatting Rules.
Reformat embedded XML doc comments Reformats XML doc comments according to options configurable in the Code Editing | XML Doc Comments | Formatting Style options page.

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 in the Code Editing | TypeScript | Code Style options page.
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 in the Code Editing | TypeScript | Code Style options page.
Add explicit 'any' type annotation if needed This options lets you add explicit 'any' annotation to types according to settings configurable in the Code Editing | TypeScript | Code Style options page.
Synchronize type annotation presence with code style This options lets you enforce explicit/implicit typing according to settings configurable in the Code Editing | TypeScript | Code Style options page.
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").
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.
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.
Visual Basic .NET
Optimize 'import' directives Removes unused namespace import directives implementing rules configured in the Code Editing | Visual Basic .NET | Namespace Imports options page.
Shorten qualified references Replaces fully qualified names with short names where possible by importing namespaces, implementing settings configured in the Code Editing | Visual Basic .NET | Code Style options page.
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 in the Code Editing | XML Doc Comments | Formatting Style options page.

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

See Also

Last modified: 3 September 2015