ReSharper Web Help

Code Cleanup provides a number of settings designed to perform various tasks. You can create your own custom Code Cleanup profiles with different combinations of these settings.

To create a custom Code Cleanup profile
  1. Choose ReSharper | Options | Tools | Code Cleanup, or click Ctrl+Alt+F to open the Code Cleanup dialog box.
  2. Click Edit Profiles.
  3. In the Code Cleanup tab in the Options dialog box, click Add to create a new custom profile.
  4. Specify a name for the new profile in the Profile Name dialog box, and click OK.
  5. Make sure that your new profile is selected in the list of profiles in the Code Cleanup tab.
  6. Configure profile settings in the Selected profile settings area. See table below for details on each profile setting.
  7. Click OK.

Whenever you choose to edit a profile or create a new one in ReSharper | Options | Tools | Code Cleanup, ReSharper displays a list of available cleanup settings in the Selected profile settings area:
Code Cleanup profile settings
The settings are listed and explained in the following table.

Code Cleanup profile settings
Profile setting Description

C#

Arrange 'this' qualifier

Applies a value selected in the ReSharper | Options | Languages | C# | Formatting Style | Other | Other | Force "this." qualifier for instance member drop-down list and removes redundant this. qualifiers.

Remove code redundancies

Overall, ReSharper lists over 70 redundancies in ReSharper | Options | Code Inspection | Inspection Severity that have associated code inspection items. Some of them require special attention, and you simply can't 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.

Make field read-only, if possible

Takes action upon "Field can be made readonly" code inspection item.

Use 'var' in declaration

Provides three options: Replace direction, 'foreach' iterator declaration style, and Local variable declaration style (see below).

Replace direction

One of three items in the Use 'var' in declaration group of settings.

Provides four options:

  • Do not change (if this value is selected, ReSharper neither converts explicit to implicit types, nor it does the opposite, even if the values of the following two settings imply such conversion).
  • Can change type usage to 'var' (only allows converting explicit to implicit types).
  • Can change 'var' to type usage (only allows converting implicit to explicit types).
  • Can change explicit to 'var' and vice versa (allows conversion in both directions - for example, you can opt to use explicit types in 'foreach' iterators and implicit types in local variables).

'foreach' iterator declaration style

One of three items in the Use 'var' in declaration group of settings.

Provides three options:

  • Always use explicit type
  • Always use 'var'
  • Use 'var' except for built-in types (implements implicit typing for all types except built-in .NET Framework data types).
  • Use 'var' except for simple types (see C# Language Specification: 4.1.4. Simple types for details on simple types).

Local variable declaration style

One of three items in the Use 'var' in declaration group of settings.

Provides five options:

  • Always use explicit type
  • Always use 'var'
  • Use 'var' except for built-in types (implements implicit typing for all types except built-in .NET Framework data types).
  • Use 'var' except for simple types (see C# Language Specification: 4.1.4. Simple types for details on simple types).
  • Use 'var' only when initializer has type usage (var p = new CodeCleanupProfile(false);
    but string n = c.Name;).

Update file header

Inserts or updates file header that you can specify in the ReSharper | Options | Languages | Common | File Header Text tab.

Optimize 'using' directives

One of three items in the Optimize 'using' directives group of settings.

Removes unused namespace import directives; implements settings configured in the ReSharper | Options | Languages | C# | Namespace Imports tab.

Embrace 'using' directives in region

One of three items in the Optimize 'using' directives group of settings.

In every file that a particular Cleanup action covers, this setting creates a new region for holding all namespace import directives.

Region name

One of three items in the Optimize 'using' directives group of settings.

Allows to specify a name for the namespace import region.

Shorten qualified references

Replaces fully qualified names with short names where possible by importing namespaces, implementing settings configured in the ReSharper | Options | Languages | C# | Namespace Imports tab.

Reformat code

Reformats you code according to options configurable in ReSharper | Options | Languages | C# | Formatting Style for C# code.

Reformat embedded XML doc comments

Reformats XML doc comments according to options configurable in ReSharper | Options | Languages | XML | Formatting Style.

Reorder type members

Reorders type members according to a pattern configurable in ReSharper | Options | Languages | C# | Type Members Layout.

VB.NET

Optimize 'import' directives

Removes unused namespace import directives; implements settings configured in the ReSharper | Options | Languages | Visual Basic .NET | Namespace Imports tab.

Shorten qualified references

Replaces fully qualified names with short names where possible by importing namespaces, implementing settings configured in the ReSharper | Options | Languages | Visual Basic .NET | Namespace Imports tab.

Reformat code

Reformats your code according to options configurable in ReSharper | Options | Languages | Visual Basic .NET | Formatting Style for VB.NET code.

XML

Reformat code

Reformats your code according to options configurable in ReSharper | Options | Languages | XML | Formatting Style for XML code.

See Also

Procedures

Reference

External Links