ReSharper C++
The Visual Studio Extension for C++ Development
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:
- Type conversion hints: a new type of inlay hints for implicit conversions.
- New immutability inspections for function parameters and
constexpr
variables. - Context actions for working with raw string literals.
- New typing assist feature: unindent code lines with Backspace.
- Embedded links to cppreference.com.
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:
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.
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.
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.
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.
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:
- Local variable can be made const and Parameter can be made const inspections notify you about objects in local scopes that can be made immutable. They cover the Con.1: By default, make objects immutable and Con.4: Use const to define objects with values that do not change after construction recommendations from the C++ Core Guidelines.
- Member function can be made const highlights member functions that do not change the state of the containing object. As Con.2: By default, make member functions const explains, marking such functions
const
improves the API of the class and lets the compiler catch more errors. - Function returns by const value checks for functions with a const-qualified return type. This is usually not beneficial and can suppress important optimizations related to move semantics.
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.
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.
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.
This transformation works both ways, so you can convert a raw string literal into a regular string literal, too.
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.
You can configure Unindent on Backspace to move the caret to the nearest or proper indent position (ReSharper | Options | Environment | Editor | Behavior).
Embedded links to cppreference.com
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.
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