C++ analyzer helps developers to write C++17 compliant code

Hello C++ developers,

C++17 standard is out for a while now and supported by the C++ analyzer running on SonarCloud. We did an effort in the past years to fix false-positives/false-negatives related to the new constructs/features of C++17. That was great but not enough to help you be more familiar with the standard and take the best out of it.
This is why we are happy to introduce a first set of rules dedicated to C++17. With these rules, we hope you will take advantage of the new features of C++17 and write more reliable and maintainable C++17 code.

Here is the list of the 20 new rules added:

Code Smell Detections:

  • S6005: Structured binding should be used
  • S6003: Emplacement should be prefered when insertion creates a temporary with sequence containers
  • S6004: “if” and “switch” initializer should be used
  • S6007: [[nodiscard]] should be used when the return value of a function should not be ignored
  • S6015: “std::uncaught_exception” should not be used
  • S6009: “std::string_view” should be used to pass a read-only string to a function
  • S6022: “std::byte” should be used when you need byte-oriented memory access
  • S6018: Inline variables should be used to declare global variables in header files
  • S6016: “[*this]” should be used to capture the current object by copy
  • S6006: “as_const” should be used
  • S6023: “std::optional” member function “value_or” should be used
  • S5997: “scoped_lock” should be preferred over “lock_guard”
  • S6030: “try_emplace” should be used with “std::map” and “std::unordered_map”
  • S6033: “emplace” should be prefered over “insert” with “std::set” and “std::unordered_set”
  • S6013: “static_assert” with no message should be used over “static_assert” with empty or redundant message
  • S6011: Objects should not be created solely to be passed as arguments to functions that perform delegated object creation
  • S6031: The right template argument should be specified for std::forward
  • S6032: Unnecessary expensive copy should be avoided when using auto as a placeholder type
  • S6045: Transparent comparator should be used with associative "std::string"containers

Bug Detection:

  • S5999: a “scoped_lock” should not be created without arguments

That’s it? No, as I said, this is just the first set of rules dedicated to C++17. A second one is coming soon and you can follow its progress here.

These C++17 rules are available now on SonarCloud and SonarQube 8.6 Developer Edition.

Alex

1 Like

A post was split to a new topic: What’s the plan for C++20 support

A post was split to a new topic: Streaming bytes