CLion 2023.3 Help

New refactorings

CLion Nova adds the following new refactorings to the CLion refactoring set:

Introduce Field

This refactoring creates a new field based on the selected expression, initializes it with the expression or from the constructor, and replaces the occurrences of the expression with references to the newly introduced field.

To invoke this refactoring, press Ctrl+Alt+F.

In the example below, we use this refactoring to replace two occurrences of the same string with a new private field and initialize it from the existing constructor:

#include <exception> #include <iostream> class ErrorHandler { ErrorHandler() {} public: void logError(std::exception&amp; e) { auto errorLogFIle = fopen("log.txt", "w"); fprintf(errorLogFIle, "Something has failed: %s", e.what()); fclose(errorLogFIle); } void printError(std::exception&amp; e) { printf("Something has failed: %s", e.what()); } };
#include <exception> #include <iostream> class ErrorHandler { ErrorHandler(): error_message("Something has failed: %s") {} public: void logError(std::exception&amp; e) { auto errorLogFIle = fopen("log.txt", "w"); fprintf(errorLogFIle, error_message, e.what()); fclose(errorLogFIle); } void printError(std::exception&amp; e) { printf(error_message, e.what()); } private: const char* error_message; };

Introduce Namespace Alias

This refactoring helps you create a namespace alias for a namespace usage and replace the currently selected usage or all usages in the current file with the alias. Depending on the selected usages, the namespace alias is declared in the closest possible scope to the usages.

Invoke Introduce Namespace Alias

  1. Place the caret at a namespace usage, press Ctrl+Alt+Shift+T, and then select Introduce namespace alias in the Refactor This popup.

    If there are multiple occurrences of the namespace usage in the document, you will be able to choose whether to replace the current usage or all usages.

In the example below, we use this refactoring to add a namespace alias for the SpaceOne::SpaceTwo namespace.

namespace SpaceOne { namespace SpaceTwo { int ten = 10; inline void foo() { // do something } } } inline int test() { SpaceOne::SpaceTwo::foo(); return SpaceOne::SpaceTwo::ten; }
namespace SpaceOne { namespace SpaceTwo { int ten = 10; inline void foo() { // do something } } } inline int test() { namespace s_two_alias = SpaceOne::SpaceTwo; s_two_alias::foo(); return s_two_alias::ten; }

Introduce Using Enum

The C++20 using enum syntax allows you to add all the enumerators from the target enumeration. As a result, you can omit repetitions of the enumeration name when using its member enumerators.

Invoke Introduce Using Enum

  1. Place the caret at a namespace usage, press Ctrl+Alt+Shift+T, and then select Introduce Using Enum in the Refactor This popup.

    CLion Nova also detects the code pieces where using enum is applicable and suggests the refactoring in the Alt+Enter menu:

    Introduce Using Enum intention action

    If there are multiple occurrences of the enumeration usage in the document, choose whether to replace the current usage or all usages:

    Introduce Using Enum multiple occurrences
  2. CLion Nova will perform the refactoring in-place:

    Introduce Using Enum result

Convert to Scoped Enum

The Convert to Scoped Enum refactoring helps you convert a C-style enumeration declaration into a C++11 scoped enumeration.

To invoke it, place the caret at an enumerator, press Ctrl+Alt+Shift+T and select Convert to Scoped Enum.

Last modified: 23 February 2024