Early Access Program

ReSharper C++ 2021.2 EAP: Type Conversion Hints, Immutability Inspections, and More

This build concludes the 2021.2 development cycle and brings the remaining features that we outlined in our roadmap:

As usual, the EAP build is free to use and available for download from our website or via the Toolbox App.

DOWNLOAD RESHARPER C++ 2021.2 EAP

Type conversion hints

Implicit conversions in C++ are performed whenever a value of one type needs to be converted to a value of another type. Compared to explicit conversions like a cast expression or a constructor call, implicit conversions don’t need to be spelled out by the programmer – instead, they are automatically applied by the compiler according to the language rules.

Even though implicit conversions can help you make your code more concise and expressive, they can also have surprising results:

  • An implicit conversion can silently call a constructor or a conversion operator, with the associated performance cost of a function call.
  • Conversions between numeric types may silently change the values, leading to potential loss of precision and correctness issues.

ReSharper C++ 2021.2 introduces a new kind of inlay hints – type conversion hints. Type conversion hints help make the implicit conversions that can cause the mentioned issues visible, so that you are aware of potential performance and correctness implications. Similarly to other inlay hints, you can configure type conversion hints on a dedicated settings page:
ReSharper | Options | Environment | Inlay Hints | C++ | Type Conversion Hints

By default, you will see special icons for implicit conversions. Alternatively, you can switch to the more verbose, but also more readable, display mode where implicit conversions are shown as code.
Special icons or text for implicit conversions

You can enable or disable type conversion hints separately for several different kinds of implicit conversions. There is a set of hints that have to do with class type conversions:

  • Hints for copy constructor calls notify you about object copying and object slicing, which occurs when an object of a derived type is copied to an object of a base type. ReSharper C++ will highlight only calls to non-trivial copy constructors (constructors that can’t simply copy the underlying bytes, but have to also perform some other action).
  • Hints for implicit conversions that involve a conversion operator.
  • Hints for object construction, which can be performed through aggregate initialization or a call to a conversion constructor.

Type conversion hints for aggregate initialization

Another set of hints covers standard language conversions to or from built-in types:

  • Implicit conversions between numeric types that might lose precision or convert a signed type to an unsigned type or vice versa.
  • Boolean conversions where a value of a numeric type is converted to a boolean value.
  • C++/CLI boxing conversions, which result in an allocation on the managed heap.
  • HLSL conversions between scalar and vector types.

Type conversion hints for HLSL

Hints for standard conversions are disabled by default, but you can enable them in settings if you find them useful.

An exclusion list for type conversion hints lets you hide implicit conversions for a specific class in your codebase. Use the inlay hints context menu (go to Alt+Enter | Configure Inlay Hints or right-click on an inlay hint) to quickly add a class to the exclusion list or to change other inlay hint settings.
An exclusion list for type conversion hints

Finally, because of the issues discussed above, it’s better to avoid defining your own implicit conversions. ReSharper C++ is, as always, ready to assist you, offering suggestions to declare converting constructors and conversion operators explicit.

Immutability inspections

Code style guides such as the Google C++ Style Guide and the C++ Core Guidelines include in-depth recommendations about the correct and consistent use of const and constexpr. Following these rules lets you create better APIs with compiler-enforced contracts on what objects an operation is allowed to mutate, helps you reason about concurrent programs, and generally leads to more maintainable and readable code.

To recap, ReSharper C++ already included several suggestions related to the use of the const keyword:

In ReSharper C++ 2021.2, we’ve continued our work on immutability-related analyses and introduced two new inspections. The first inspection extends the immutability analysis for function parameters of reference and pointer types and covers Con.3: By default, pass pointers and references to consts. Following this rule guarantees that the function will not modify an argument passed by reference or by pointer.
By default, pass pointers and references to consts

The second inspection suggests marking variables that can be evaluated at compile-time with constexpr. As Con.5: Use constexpr for values that can be computed at compile time mentions, this can lead to better performance and better compile-time checking.
Use constexpr for values that can be computed at compile time

With the new inspections in place, ReSharper C++ now covers all the rules from the Constants and immutability section of the C++ Core Guidelines. We hope that ReSharper’s immutability suggestions will help you follow best practices and improve code quality in your project.

Raw string literals

When working with regular expressions that use the same backslash escape sequence as C++, or markup languages with many quotation marks and newlines, you often have to deal with string literals full of backslashes and overcomplicated escape sequences. You can now use a new context action to convert any string into a C++11 raw string literal, which is much easier to read.
Convert to raw string literal

This transformation works both ways, so you can convert a raw string literal into a regular string literal, too.
Convert to non-raw string literal

Unindent on Backspace

When you press Backspace on an empty line or with whitespaces and tabs to the left of the caret, ReSharper C++ can now place the caret at the proper indent position instead of moving it back one position at a time.
Unindent on Backspace

You can configure Unindent on Backspace to move the caret to the nearest or proper indent position (ReSharper | Options | Environment | Editor | Behavior).

As a C++ programmer, you have probably heard about cppreference.com before. This site, driven by a dedicated community of C++ enthusiasts since 2000, has become the most popular and up-to-date online C++ reference.

If you want to quickly consult cppreference.com about the details of a standard library class or function, you can now use the Read more link in the Quick Documentation pop-up (Ctrl+Shift+F1) to open the corresponding page in your browser.
Embedded links to cppreference.com

That’s all for today! Check out the new EAP build and share your feedback with us using our issue tracker or in the comments below. Thank you!

DOWNLOAD RESHARPER C++ 2021.2 EAP

Your ReSharper C++ team
JetBrains
The Drive to Develop

image description