ReSharper 2024.1 Help

Code analysis in C++

The key features of ReSharper's code analysis are also supported in C++. You can find the detailed information on these features in the corresponding topics of the Code analysis section. In the main topic of the section, you can also find the feature matrix and check what exactly is supported in C++.

In this topic, you can find some examples of using code analysis features in C++.

By default, code inspection, quick-fixes, and context actions are available in all solution files. If necessary, you can enable these features in external files referenced from the solution with the Enable inspections, quick-fixes, and context actions in files external to the solution checkbox on the Code Editing | C++ | Inspections page of ReSharper options Alt+R, O.

Code Inspections

ReSharper's static code analysis detects most of compiler errors and some compiler warnings in C++ files.

Besides, it finds other code issues, which otherwise would be found in runtime. For example, using a virtual method in constructor leads to unexpected behavior as mentioned in this StackOverflow question. ReSharper points at this problem before you even compile:

ReSharper warning. virtual method in constructor

The analysis is performed by applying code inspections to the current document or in any specified scope.

Code inspection results in a C++ project

To find out what kind of code inspections ReSharper provides, check out the full list of ReSharper C++ code inspections.

For more information about the code inspection, refer to Configure code inspection settings.

To quickly turn off a particular inspection or suppress all the inspections, use the inspection context menu:

Disable inspections in C++

You can also suppress some inspections with the ReSharper-specific attributes. Check out the details in the Use attributes to refine inspections section.

Quick-fixes

Quick-fixes let you automatically resolve most of code issues found with code inspection. If one or more quick-fixes are available for a code issue, you will see the corresponding action indicator when your caret is on the highlighted code. Press Alt+Enter to see and apply the desired fix.

ReSharper provides over 290 quick-fixes for C++. Here are some examples:

Add missing #include directive

If you use a symbol that is defined in the standard libraries or elsewhere in your solution, ReSharper helps you add the missing #include directives automatically:

ReSharper: helps adding missing C++ includes automatically

You can use the Show popups for import actions checkbox on the Code Editing | C++ | Inspections page of ReSharper options Alt+R, O to disable the popup.

If the checkbox is not selected, the corresponding action will be available in the action list when you press Alt+Enter.

Add forward declaration

ReSharper can also generate a forward declaration for an unresolved symbol:

ReSharper: Add forward declaration quick-fix

Add [[maybe_unused]]

The [[maybe_unused]] attribute can be added to avoid warnings from the compiler about an unused name or entity. When the caret is on an unused entity, the following quick-fix will be available:

Add [[maybe_unused]]

And another context action will help you to replace usages of UNREFERENCED_PARAMETER and Q_UNUSED with a [[maybe_unused]] attribute:

add [[maybe_unused]]

Add to capture list

If a local variable is used inside a lambda body but not captured by this lambda, ReSharper suggests quick-fixes to update the capture list. You can capture the variable or this by value, by reference, or implicitly capture all the used automatic variables:

ReSharper C++: quick-fix to update the capture list

Change return type

If a function return type does not match the value it actually returns, ReSharper lets you quickly fix the return type:

ReSharper helps fixing types of variables and fields

Change variable type

If the specified type of a variable or a field does not match to the assigned value, ReSharper provides a quick-fix to use the correct type in the declaration:

ReSharper helps fixing types of variables and fields

Create declaration

If a member function in a C++ file does not have declaration in the header file, ReSharper helps you create the missing declaration according to the signature of the implementation:

Generating declaration for C++ class member

Create from usage

If you use an undeclared symbol, ReSharper suggests one or more quick-fixes for creating the symbol based on the usage:

Generating C++ field from usage

Initialize members

If ReSharper detects uninitialized type members, it suggests initializing all members in one fix. You can also use the sub-menu of this fix to generate initializations for specific members.

ReSharper C++: quick-fix to initialize members

Initialize variable

ReSharper detects uninitialized variables and helps initializing them with the default value:

ReSharper helps initialize variables in C++

Remove unused #include directives

All unused #include are highlighted and can be easily removed with a quick-fix:

Removing unused #include directives

Remove unused lambda capture

If a local variable is captured by a lambda but not used inside the lambda body, ReSharper notifies you and suggests removing the unused capture:

Remove unused lambda capture

Replace the dot (.) with arrow (->) and vice versa

If the dot . operator is erroneously applied to a pointer, ReSharper helps you replace it with the arrow -> operator:

ReSharper helps replacing the dot operator with arrow

The reverse quick-fix is also available:

ReSharper helps replacing the arrow operator with the dot

Use explicit template parameters in lambda

ReSharper detects when lambda can be rewritten to use the new C++20 template syntax:

Use explicit template parameters in lambda

If the parameter’s type is an rvalue reference, you may want to pass that type directly to std::forward. In this case, you can use familiar perfect forwarding syntax:

perfect forwarding syntax

Use static_cast

ReSharper detects cases when you should prefer static_cast and helps you update your code:

  • Functional-style cast used instead of a C++ cast:

    Functional-style cast used instead of a C++ cast
  • reinterpret_cast used instead of a static_cast when casting to void*:

    reinterpret_cast used instead of a static_cast when                     casting to void*

Use attributes to refine inspections

You can add ReSharper-specific attributes to symbols of your source code to make ReSharper analyze your solution with greater accuracy and insight. ReSharper allows you to use the following custom attributes: [[jetbrains::format]], [[jetbrains::guard]], [jetbrains::pass_by_value]], and [[jetbrains::has_side_effects]].

The [[jetbrains::format]] attribute allows you to enable format inspections for custom printf-like functions and works similarly to [[gnu::format]].

[[jetbrains::format]]

The [[jetbrains::pass_by_value]] attribute allows you to suppress the "Pass value by const reference" inspection. Why use the suppressing attribute instead of disabling the inspection with a comment? When you don’t want to see a warning for a specific class or struct, you can always select Inspection:… | Disable once with a comment from the Alt+Enter menu to add a comment suppressing the inspection on the current line or in a file. You’ll then have to add the suppressing comment for all class usages as well. In these cases, it’s much easier to specify a suppressing attribute in a class declaration once.

[[jetbrains::pass_by_value]]

The [[jetbrains::guard]] attribute can be used to suppress the "Local variable is never used" inspection. Mark a class with this attribute to let ReSharper know that the class performs important work in its constructor.

template<class Mutex> class [[jetbrains::guard]] my_lock_guard { explicit my_lock_guard(Mutex &); };

The [[jetbrains::has_side_effects]] attribute allows you to mark operator= as having side effects to suppress the "Assigned value is never used" inspection.

struct A { A& operator = (int); [[jetbrains::has_side_effects]] A& operator = (float); }; void foo(A a) { a = 3; // warning "Assigned value is never used" a = 3.f; // no warnings here a = A(); // warning "Assigned value is never used" }

If you need to apply a ReSharper attribute to a library class from an external library, you can use the attribute on a forward class declaration instead: class [[jetbrains::pass_by_value]] my_class;.

If your compiler shows a warning about an unknown attribute, you can guard the declaration with the __JETBRAINS_IDE__ or __RESHARPER__ macro:

#ifdef __JETBRAINS_IDE__ class [[jetbrains::pass_by_value]] my_class; #endif

Make ReSharper ignore specific code

To exclude parts of your solution's code from code analysis, navigation, and other features, ReSharper allows you to ignore specific files, folders and file masks in different ways.

Last modified: 26 May 2024