JetBrains Rider 2023.2 Help

Code cleanup

JetBrains Rider 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:

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

  1. Press Control+Alt+S or choose File | Settings (Windows and Linux) or JetBrains Rider | Preferences (macOS) from the menu .

  2. Go to the cleanup profiles settings page: Editor | Code Cleanup.

  3. Click Add on the top of the page. JetBrains Rider will create a new cleanup profile .

  4. Select the newly created profile in the list of profiles.

  5. Use the right pane to configure the list of tasks in the selected profile.

  6. Click Save in the Settings dialog to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, refer to layer-based settings.

Your newly created cleanup profile will be available in the Reformat and Cleanup Code dialog when you are running code cleanup. You can also set this profile to be used for the silent cleanup.

Run code cleanup

JetBrains Rider can apply code cleanup to an arbitrarily selected block of code, all code in the current file, all files in a directory, files in the current VCS changelist, 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 selection or file

  1. Select the scope where you want to clean up code:

    • Make a selection in the editor to clean up code in the selection.

    • Place the caret anywhere in the file to clean up code in the file.

  2. If you are cleaning up a selection, press Alt+Enter and choose Reformat and cleanup | [cleanup profile].

    Alternatively you can follow the below steps, which work for both selection and file.

  3. Press Control+R, C or choose Code | Reformat and Cleanup… from the main menu .

  4. In the Reformat and Cleanup Code dialog that opens, you can check and optionally change the scope for cleaning code and choose a cleanup profile to apply. Use the area to the right of the selected profile to check the tasks included in that profile.

  5. If none of the available profiles suits you, click Edit App resharper common edit, then choose one of the existing profiles and click Clone to create a new custom profile and save the changes.

  6. Click Run to clean up code in the selected scope.

You can clean up not only an individual file, but also multiple files in a directory, project or entire solution. To do so, run code cleanup from the Solution Explorer.

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. Press Control+R, C or choose Code | Reformat and Cleanup… from the main menu .

  3. Choose a cleanup profile as described above.

  4. Click Run to clean up code in the selected scope.

You can run Code Cleanup in code that you have recently modified and are going to commit to Git. JetBrains Rider will run the selected cleanup profile before committing.

Clean up code before committing it to Git

  1. Press Control+K or select Git | Commit from the main menu.

  2. In the Commit tool window, click Show Commit Options and in the Commit Checks area, select the Cleanup with... checkbox.

  3. Optionally, you can click Choose profile to specify the cleanup profile that should be used.

  4. Click Commit or Commit and Push. JetBrains Rider will run code cleanup in files staged for the commit, and then commit the changes.

You can run Code Cleanup every time you save changes in a file to make sure that your edits always comply with your code style. Note that this will only happen when you save changes explicitly with Control+S or Control+S and will not be triggered by auto-saving. However, all auto-saved files are placed to the 'reformat and cleanup' queue and will be processed on the next explicit save.

Automatically run Code Cleanup on saving changes

  1. Press Control+Alt+S to open the IDE settings and then select Tools | Actions on Save.

  2. Select Reformat and Cleanup Code, choose which code cleanup profile should be applied and whether to apply it to the whole file or only to the changed lines.

  3. The next time you finish editing and save the file or all files , JetBrains Rider will clean up the affected files using the selected profile.

Silent cleanup

You can also run code cleanup in the silent mode — that is, without displaying the Reformat and Cleanup Code 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

  1. Press Control+Alt+S or choose File | Settings (Windows and Linux) or JetBrains Rider | Preferences (macOS) from the menu .

  2. Go to the cleanup profiles settings page: Editor | Code Cleanup.

  3. Select a cleanup profile on the left and click Set as default for silent cleanup at the top of the options page.

  4. Click Save in the Settings dialog to apply the modifications and let JetBrains Rider choose where to save them, or save the modifications to a specific settings layer using the Save To list. For more information, refer to layer-based settings.

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 Control+R, G or choose Code | Silent Reformat and Cleanup 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.

Reformat code

C#

Reformat code

Reformats your code according to the preferences that you can configure on the Editor | Code Style | C# page of JetBrains Rider settings . For more information, refer to Apply formatting rules.

Reformat embedded XML doc comments

Reformats XML doc comments according to options configurable on the Editor | Code Style | C# | XML Doc Comments page of JetBrains Rider settings .

Note that JetBrains Rider does not reformat plain comments like:

// A plain single-line comment

or

/* A plain multi-line comment */

C++

Reformat code

Reformats your code according to the preferences that you can configure on the Editor | Code Style | C++ page of JetBrains Rider settings . For more information, refer to Apply formatting rules.

Visual Basic

Reformat code

Reformats your code according to the preferences that you can configure on the Editor | Code Style | Visual Basic page of JetBrains Rider settings . For more information, refer to Apply formatting rules.

Reformat embedded XML doc comments

Reformats XML doc comments according to options configurable on the Editor | Code Style | Visual Basic | XML Doc Comments page of JetBrains Rider settings .

Note that JetBrains Rider does not reformat plain comments like:

// A plain single-line comment

or

/* A plain multi-line comment */

XML

Reformat code

Reformats your code according to the preferences that you can configure on the Editor | Code Style | XML page of JetBrains Rider settings . For more information, refer to Apply formatting rules.

HTML

Reformat code

Reformats your code according to the preferences that you can configure on the Editor | Code Style | HTML page of JetBrains Rider settings . For more information, refer to Apply formatting rules.

CSS

Reformat code

Reformats your code according to the preferences that you can configure on the Editor | Code Style | CSS page of JetBrains Rider settings . For more information, refer to Apply formatting rules.

JavaScript/TypeScript

Reformat code

Reformats your code according to the preferences that you can configure on the Editor | Code Style | JavaScript/TypeScript page of JetBrains Rider settings . For more information, refer to Apply formatting rules.

Reformat embedded XML doc comments

Reformats XML doc comments according to options configurable on the Editor | Code Style | JavaScript/TypeScript | XML Doc Comments page of JetBrains Rider settings .

Note that JetBrains Rider does not reformat plain comments like:

// A plain single-line comment

or

/* A plain multi-line comment */

Syntax styles

C#

Apply 'var' style

Applies the rules for 'var' usage during the cleanup. The rules are configurable on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S.

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 Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to 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 Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Modifiers.

Sort modifiers

Applies the preferred order of type/member modifiers. The rule is configurable on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Modifiers.

Apply arguments style (named vs. positional)

Applies the preferences for named/positional arguments for specific types of parameters. The preferences are configurable on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Named/Positional Arguments.

Remove redundant parentheses

Removes redundant optional parentheses in cases specified on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to 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 Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Optional Parentheses.

Add/remove braces around single statements

Adds or removes optional braces around single nested statements as specified on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Braces for Single Nested Statements.

Add/remove brackets for attributes

Applies the rules for multiple attributes as specified on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Multiple Attributes.

Apply code body style (expression body vs. block body)

Applies preferences for bodies of type members (expression body or block body) as specified on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code syntax style: bodies of type members.

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 Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Trailing Commas.

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 Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to 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 Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Default Value Expressions ('default' vs 'default(T)').

Arrange namespaces (file-scoped vs. block-scoped)

Applies preferences for using file-scoped namespace declarations in files containing a single namespace as specified on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code syntax style: file-scoped namespaces.

Arrange null checking pattern style

Applies the preferred way of checking an expression for null with the type-testing 'is' operator (with not null or with { }) as specified on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Null checking pattern.

Arrange qualifiers

Applies the rules for qualifying type members. The rules are configurable on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to 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 Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S. For more information, refer to Code Syntax Style: Built-In Type References.

C/C++

Add/remove braces for single statements

Applies preferences for braces around single nested statements under the if statement and different kinds of loop statements. The rules are configurable on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Apply 'auto' style

Applies preferences for using the auto specifier in variable types as specified on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Apply function declaration style (regular vs. trailing return types)

Applies the preferred style of trailing or regular return types in function declarations as specified on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Apply nested namespaces style

Applies the rule for enforcing the C++17 syntax for nested namespaces, which allows you to declare multi-level namespaces in a more concise manner. The rule is configurable on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Apply type alias style (typedefs vs. type aliases)

Applies the preferred way to define type synonyms as specified on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Arrange cv-qualifiers

Applies the preferred order of cv-qualifiers - whether the const keyword should be placed before or after the type it applies to, and which of the const and volatile qualifiers should go first. The rules are configurable on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Arrange slashes in include directives (forward slashes vs. backslashes)

Applies the preferred style of path separator for #include directives as specified on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Arrange specifiers ('virtual' and/or 'override') on overriding functions

Applies the preferred style for overriding functions - which specifiers should be required on overriding functions and destructors. The rules are configurable on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Sort include directives

Applies the rules for sorting and regrouping #include directives. The desired order is configurable on the Languages & Frameworks | C++ | Order of #includes page of JetBrains Rider settings Control+Alt+S. For more information, refer to Sorting of includes.

Sort member initializers

Applies the order of member initializers according to constructor initializer lists as specified on the Editor | Code Style | C++ | Syntax Style page of JetBrains Rider settings Control+Alt+S.

XAML

Collapse empty tags

Removes the closing tag if the tag body is empty.

ASP.NET/Razor

Normalize quotes around attribute value

Applies the preferred quote style: single quote ' or double quote ", configurable on the Editor | Code Style | ASP.NET (Razor) | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Optimize imports

C#

Optimize 'using' directives

  • Removes unused namespace import directives

  • Applies preferences for arranging namespace import directives and fully-qualified names when importing types, as specified on the Editor | General | Code Style | C# | Syntax Style and Editor | General | Auto Import settings pages.

  • Sorts the namespace import directives: all System.* namespaces go first, sorted alphabetically by the second word after the dot; all other namespaces go next, in alphabetical order. .

For more information, refer to Code Syntax Style: Namespace Imports.

Embrace 'using' directives in region

If this checkbox 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, according to the preferences configured on the Editor | Code Style | C# | Syntax Style page of JetBrains Rider settings Control+Alt+S

Visual Basic

Optimize 'import' directives

  • Removes unused namespace import directives

  • Applies preferences for arranging namespace import directives and fully-qualified names when importing types, as specified on the Editor | General | Code Style | Visual Basic | Syntax Style and Editor | General | Auto Import settings pages.

  • Sorts the namespace import directives: all System.* namespaces go first, sorted alphabetically by the second word after the dot; all other namespaces go next, in alphabetical order. .

For more information, refer to Code Syntax Style: Namespace Imports.

Embrace 'using' directives in region

If this checkbox 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, according to the preferences configured on the Editor | Code Style | Visual Basic | Syntax Style page of JetBrains Rider settings Control+Alt+S

XAML

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.

TypeScript specific

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 | Syntax Style page of JetBrains Rider settings Control+Alt+S.

Optimize reference comments

Use this option to remove redundant reference comments.

ASP.NET/Razor

Optimize @Register directives

Removes unused @Register directives.

Rearrange code

C#

Apply file layout

Reorders type members in files according to the rules configurable on the Editor | Code Style | C# | File Layout page of JetBrains Rider settings Control+Alt+S. For more information, refer to Rearrange members with file and type layout patterns.

CSS

Alphabetize properties

Rearranges CSS properties in the alphabetical order.

Remove redundancies & apply optimizations

C#

Remove code redundancies

On the Editor | Inspection Severity page of JetBrains Rider settings Control+Alt+S, in the Redundancies in Code categories of most languages, JetBrains Rider lists dozens of cases that have associated code inspections. Some of them require special attention, and you cannot process them in the batch mode, but most of them can be fixed 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, refer to 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, refer to 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.

By default, JetBrains Rider automatically detects C# version based on the associated compiler. However, you can specify the target C# version explicitly for a project — press Alt+Enter on the project in the Solution Explorer and use the Language version selector on the Application page of the Project Properties dialog .

To set the C# version for all projects in your solution, specify it in a Directory.Build.props file in your solution directory as described here.

C/C++

Add required 'typename' and 'template' keywords

Adds missing typename and template keywords.

Join declarations and assignments

Joins declaration and assignment for cases when a local variable is reassigned in all paths before being read.

Make local variables const

Adds const for objects in local scopes that can be made immutable.

Make member functions 'const' when possible

Adds const for member functions that do not change the state of the containing object.

Make member functions 'static' when possible

Makes member functions static.

Remove redundant casts

Removes redundant cast expressions.

Remove redundant 'else' keywords

Removes else keywords that are redundant in their context.

Remove redundant elaborated type specifiers

Removes redundant elaborated type specifier when it's not used to declare a new class or to refer to a previously declared struct hidden by a more local non-type declaration.

Remove redundant lambda parameter lists

Removes redundant () in lambdas with no parameters.

Remove redundant member initializers

Removes member initializers in constructors that duplicate the corresponding default member initializers.

Remove redundant parentheses

Removes parentheses that are redundant in their context.

Remove redundant qualifiers

Removes qualifiers that are redundant in their context.

Remove redundant specifiers

Removes specifiers that are redundant in their context.

Remove redundant statements

Removes statements that are redundant in their context.

Remove redundant template arguments

Removes redundant template arguments that can be deduced by the language according to the C++17 class template argument deduction rules.

Remove redundant 'typename' and 'template' keywords

Removes typename and template keywords that are redundant in their context.

Remove unreachable code

Removes unnecessary code that will never be executed.

Remove unused #include directives

Removes never used #include directives.

Remove unused lambda captures

Removes unused capture if a local variable is captured by a lambda but not used inside the lambda body.

Replace C-style and functional-style casts with static casts

Replaces C-style, reinterpret_cast, and also functional-style casts with static_cast.

Replace constants in boolean context with boolean literals

Replaces integral constants with true/false in boolean contexts.

Replace if statements with constant conditions with 'if constexpr'

Replaces if statements with if constexpr when the condition is a compile-time constant expression.

Replace postfix operators with prefix variants

Replaces postfix operators with their prefix variants.

Replace smart pointer constructors with make functions

Replaces smart pointer constructors with standard (make_unique/make_shared) or Unreal-specific (MakeShared/MakeUnique) smart pointer creation functions.

Replace throw expressions with rethrow where possible

Updates the throw expression to rethrow the exception that cannot be handled by a catch block when caught.

Replace type traits with corresponding template aliases or variable templates

Shortens C++11 type trait expressions with C++14 alias templates.

Replace zero valued expressions in pointer context with nullptr

Replaces zero constants with nullptr in pointer contexts.

Replace 'tie(..)' with structured binding declarations

Replaces std::tie() or boost::tie() for unpacking a tuple or a pair with structured binding declarations.

Use 'contains' for associative containers wherer possible

Replaces find or count with the C++20 contains function.

Use 'std::erase'/'std::eraseif' instead of remove-erase idiom

Replaces the old erase-remove idiom with C++20 std::erase or std::erase_if library functions for erasing elements from any STL container.

Visual Basic

Remove code redundancies

On the Editor | Inspection Severity page of JetBrains Rider settings Control+Alt+S, in the Redundancies in Code categories of most languages, JetBrains Rider lists dozens of cases that have associated code inspections. Some of them require special attention, and you cannot process them in the batch mode, but most of them can be fixed automatically whenever you select Remove code redundancies in your cleanup profile.

XAML

Remove redundant 'Freeze' attribute

Removes Freeze attributes that are redundant in their context.

Remove redundant modifiers attribute

Removes modifiers attributes that are redundant in their context.

Remove redundant name attribute

Removes name attributes that are redundant in their context.

Remove redundant resource

Removes resource references that are not used

Remove redundant empty collection property setter

Remove redundant attached property setter

Removes unused setters for the attached properties.

Remove redundant style property setter

Removes unused setters for the style properties.

Remove forbidden name attribute

Removes name attributes that are not allowed in their context.

Remove redundant definitions

Removes definitions if they are redundant in the context.

Remove redundant UpdateSourceTrigger attribute

Removes UpdateSourceTrigger attributes that are redundant in their context.

Remove redundant Mode attribute

Removes Mode attributes that are redundant in their context.

Remove redundant span attribute

Removes span attributes that are redundant in their context.

C#

Update file header

Inserts or updates the file header comment that you can configure on the Editor | File Header Template page of JetBrains Rider settings Control+Alt+S or using the file_header_template EditorConfig property. For more information, refer to File header style.

C/C++

Update file header

Inserts or updates the file header comment that you can configure on the Editor | File Header Template page of JetBrains Rider settings Control+Alt+S or using the file_header_template EditorConfig property. For more information, refer to File header style.

Run external cleanup tools

C/C++

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 Editor | Inspection Settings| Inspection Severity page of JetBrains Rider settings Control+Alt+S. 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.

Last modified: 05 September 2023