New C++ Core Check Rules

Avatar

Jordan

The C++ Core Guidelines Checker receives three new rules with the release of Visual Studio version 16.3 Preview 2. In addition, some warnings published in the warnings.h that ships with Visual Studio have been moved or renamed.

Below is a quick summary of these additions. For more detailed information, please see the C++ Core Guidelines Checker Reference documentation.

If you’re just getting started with native code analysis tools, take a look at our introductory quick start for Code Analysis for C/C++.

New rule set

The “Enum Rules” set has been added in this release. It can be enabled by selecting “C++ Core Check Enum Rules” in the Project Settings dialog. This rule set can be used to detect common errors when using enums as specified in the Core Guidelines Enum section.

New rules

Const rules

  • C26814 – “USE_CONSTEXPR_RATHER_THAN_CONST”

C26814 is a more aggressive implementation of Con.5. Our previous warning, C26498 (“USE_CONSTEXPR_FOR_FUNCTIONCALL”), checks for constexpr conversion candidates by evaluating all const variables whose values are derived from constexpr functions. This new rule evaluates all const variables to determine whether their values can be determined at compile time.NOTE: This rule is not included in the “Microsoft Native Recommended Rules” rule set by default and will need to be added or run via the “C++ Core Check Const Rules” rule set.

Enum rules

  • C26812 – “USE_ENUM_CLASS_INSTEAD_OF_ENUM”

C26812 implements Enum.3. It recommends declaring all enums as a scoped enums; that is, declaring “enum” as “enum class”. This is largely to prevent unintended errors when using enums as they are too readily converted to int.

Type rules

  • C26478 – “NO_MOVE_OP_ON_CONST”

C26478 is designed to prevent unnecessary calls to “std::move”. Specifically, this rule hopes to curb the usage of “std::move” on constant objects. When calling “std::move” on a const object, the move operation performs a copy rather than moving the ownership of the object, which is likely not what the developer intended. For further reading, refer to ES.56.

Warnings.h changes

  • The warning C26477 “USE_NULLPTR” was renamed to “USE_NULLPTR_NOT_CONSTANT”.
  • The rule category “CPPCORECHECK_EXPERIMENTAL_WARNINGS” was removed from this release. The warning it contained, C26800 (“USE_OF_A_MOVED_FROM_OBJECT”), was added to the “CPPCORECHECK_LIFETIME_WARNINGS” rules.
  • The warnings C26810 and C26811, (“COROUTINES_USE_AFTER_FREE_CAPTURE” and “COROUTINES_USE_AFTER_FREE_PARAM” respectively, were removed from the “CPPCORECHECK_CONCURRENCY_WARNINGS” category and added to “CPPCORECHECK_LIFETIME_WARNINGS”.

Feedback

We would appreciate it if you tried these new rules out and gave us feedback on them. We can be reached via the comments below, via email (visualcpp@microsoft.com), or on Twitter @VisualC. If you encounter any problems, please report them via the Report A Problem tool in Visual Studio or on the Visual Studio Developer Community.

 

Avatar
Jordan Maples

Follow Jordan   

2 comments

  • Avatar
    Victor Derks

    Hi Jordan,
    I tried VS 16.3 P2. Here some feedback on the new warnings
    USE_CONSTEXPR_RATHER_THAN_CONST: can generate a false warning for const variables in templates. I had to disable this warning for those cases (see open source project, https://github.com/team-charls/charls/tree/ooapi, file scan.h, line 363 and 400.

    USE_ENUM_CLASS_INSTEAD_OF_ENUM
    I am getting this warning reported when I use an enum from the Windows SDK, for example COINIT from objbase.h and enums from msxml6.tli. I have been unable with 16.3 P2 to suppress these warnings by wrapping the #include statement in a #pragma push/disable/pop. As I cannot “repair” the Windows SDK and I cannot suppress these warnings in a single location, I have improved my own code and then disabled this warning. 

    • Avatar
      Alex Ivanoff

      I agree. As long as USE_ENUM_CLASS_INSTEAD_OF_ENUM reports errors from Windows SDK headers it will not be very useful.

Leave a comment