C++ Core Check improvements in Visual Studio 2017 15.5

Andrew

November 15th, 2017

点这里看中文版

This post was written by Sergiy Oryekhov.

In Visual Studio 2017 version 15.5 Preview 4 we have refreshed our C++ Core Guidelines Check extension for native code static analysis tools. Most of the work since 15.3 has been focused on new rules that will help developers starting new projects write safer C++ and those with legacy projects move toward safer, modern C++. For those who are new to the core guidelines checks, we recommend to review the latest version of the working draft: “C++ Core Guidelines“.

This article provides an overview of the changes in VS2017 15.5. For more information about the current release, please see the list of supported rule sets on docs.microsoft.com.

Note that some of the links to docs.microsoft.com do not yet resolve. Not all of the official documentation has yet been written for this topic.

New rule sets

In the previous release we introduced several rule sets to allow customers to narrow code analysis results. The rules implemented for 15.5 extended some of the existing rule sets (Raw Pointer, Owner Pointer, Unique Pointer, Type), and also introduced a few new categories:

Class rules : This section includes a few rules mostly focused on proper use of special methods and virtual specifications. This is a subset of checks recommended for classes and class hierarchies.

: This section includes a few rules mostly focused on proper use of special methods and virtual specifications. This is a subset of checks recommended for classes and class hierarchies. Concurrency rules : This currently includes a single rule which catches badly declared guard objects. For more information see Core Guidelines related to concurrency.

: This currently includes a single rule which catches badly declared guard objects. For more information see Core Guidelines related to concurrency. Declaration rules : Here are a couple of rules from the interfaces guidelines which focus on how global variables are declared.

: Here are a couple of rules from the interfaces guidelines which focus on how global variables are declared. Function rules : These are two checks that help with adoption of the noexcept specifier. This is a part of the guidelines for clear function design and implementation.

: These are two checks that help with adoption of the specifier. This is a part of the guidelines for clear function design and implementation. Shared pointer rules : As a part of resource management guidelines enforcement, we added a few rules specific to how shared pointers are passed into functions or used locally.

: As a part of resource management guidelines enforcement, we added a few rules specific to how shared pointers are passed into functions or used locally. Style rules: In this release we have one simple but important check which bans use of goto . This is the first step in improving of coding style and use of expressions and statements in C++. While there are exceptions in the C++ Core Guideline about goto , proper use of the construct is rare enough that it deserves review.

New rules in each set

Warnings that were rearranged

Some warning numbers found in the VS2017 version 15.3 release are no longer available in VS2017 version 15.5. These warnings did not disappear, but were rather replaced with more specific checks. The primary goal was to separate particularly common patterns within a warning into separate warnings.

In closing

Good tools can help you to maintain and upgrade your code. The C++ Core Guidelines are a great place to start, and the C++ Core Guidelines Checker can help you to clean up your code and keep it clean. Try out the expanded C++ Core Guidelines Checker in Visual Studio 2017 version 15.5 and let us know what you think.

If you have any feedback or suggestions for us, let us know. We can be reached via the comments below, via email (visualcpp@microsoft.com) and you can provide feedback via Help > Report A Problem in the product, or via Developer Community. You can also find us on Twitter (@VisualC) and Facebook (msftvisualcpp).