C++ Core Checks in Visual Studio 2017 15.7 Preview 2

This post was written by Sergiy Oryekhov.

The C++ Core Guidelines Check extension received several new rules in Visual Studio 2017 15.7 Preview 2. The primary focus in this iteration was on the checks that would make it easier to adopt utilities from the Guidelines Support Library.

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

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

New rule sets

There is one new rule category added in this release with a rule set file which can be selected in the project settings dialog.

  • GSL rules

    Several new rules try to help catching subtle issues related to how span and view types are used. Catching such issues becomes more important once modern practices of safe memory handling get adopted. In addition, a couple useful utilities from the Guidelines Support Library are promoted so that user code can be made safer and more uniform.

New rules

Bounds rules

  • C26446 USE_GSL_AT
    This rule suggests using a safer version of an indexing function whenever a subscript operator which doesn’t perform range checks gets called.
    This rule is also a part of the GSL rule set.

Function rules

  • C26447 DONT_THROW_IN_NOEXCEPT
    This check is applicable to functions marked as ‘noexcept’ and points to the places where such functions invoke code that can potentially throw exceptions.

GSL rules

  • C26445 NO_SPAN_REF
    Some subtle issues may occur when legacy code gets migrated to new types that point to memory buffers. One of such issue is unintended referencing of spans or views which may happen if legacy code used to reference containers like vectors or strings etc.
  • C26448 USE_GSL_FINALLY
    The Guidelines Support Library has a useful utility which helps to add the “final action” functionality in a structural and uniform way. This rule helps to find places that use ‘goto’ and may be good candidates for adoption of gsl::finally.
  • C26449 NO_SPAN_FROM_TEMPORARY
    Spans and views are efficient and safe in dealing with memory buffers, but they never own data they point to. This must be taken into account especially when dealing with legacy code. One dangerous mistake that users can make is to create a span over a temporary object. It is a special case of a lifetime issue similar to referencing local data.

    This rule is enabled by default in the “Native Recommended” rule set.

    Example: Subtle difference in result types.

    // Returns a predefined collection. Keeps data alive.
    gsl::span get_seed_sequence() noexcept;
    
    // Returns a generated collection. Doesn’t own new data.
    const std::vector get_next_sequence(gsl::span);
    
    void run_batch()
    {
    auto sequence = get_seed_sequence();
    while (send(sequence))
    {
    sequence = get_next_sequence(sequence); // C26449
    // ...
    }
    }
    
  • C26446 USE_GSL_AT
    See “Bounds rules” above.

Feedback

As always, we welcome your feedback. Feel free to send any comments through e-mail at visualcpp@microsoft.com, through Twitter @visualc, or Facebook at Microsoft Visual Cpp.

If you encounter other problems with MSVC in VS 2017, please let us know via the Report a Problem option, either from the installer or the Visual Studio IDE itself. For suggestions, let us know through UserVoice. Thank you!