SonarSource Rules
  • Products

    In-IDE

    Code Quality and Security in your IDE with SonarQube Ide

    IDE extension that lets you fix coding issues before they exist!

    Discover SonarQube for IDE

    SaaS

    Code Quality and Security in the cloud with SonarQube Cloud

    Setup is effortless and analysis is automatic for most languages

    Discover SonarQube Cloud

    Self-Hosted

    Code Quality and Security Self-Hosted with SonarQube Server

    Fast, accurate analysis; enterprise scalability

    Discover SonarQube Server
  • SecretsSecrets
  • ABAPABAP
  • AnsibleAnsible
  • ApexApex
  • AzureResourceManagerAzureResourceManager
  • CC
  • C#C#
  • C++C++
  • CloudFormationCloudFormation
  • COBOLCOBOL
  • CSSCSS
  • DartDart
  • DockerDocker
  • FlexFlex
  • GitHub ActionsGitHub Actions
  • GoGo
  • HTMLHTML
  • JavaJava
  • JavaScriptJavaScript
  • JSONJSON
  • JCLJCL
  • KotlinKotlin
  • KubernetesKubernetes
  • Objective CObjective C
  • PHPPHP
  • PL/IPL/I
  • PL/SQLPL/SQL
  • PythonPython
  • RPGRPG
  • RubyRuby
  • RustRust
  • ScalaScala
  • ShellShell
  • SwiftSwift
  • TerraformTerraform
  • TextText
  • TypeScriptTypeScript
  • T-SQLT-SQL
  • VB.NETVB.NET
  • VB6VB6
  • XMLXML
  • YAMLYAML
C++

C++ static code analysis

Unique rules to find Bugs, Vulnerabilities, Security Hotspots, and Code Smells in your C++ code

  • All rules 674
  • Vulnerability13
  • Bug139
  • Security Hotspot19
  • Code Smell503

  • Quick Fix 91
Filtered: 74 rules found
clumsy
    Impact
      Clean code attribute
        1. "contains" should be used to test whether a substring is part of a string

           Code Smell
        2. "if consteval" should be used instead of "if (std::is_constant_evaluated())"

           Code Smell
        3. "static_assert" should be preferred to assert when the argument is a compile-time constant

           Code Smell
        4. "std::format" should be used instead of standard output manipulators

           Code Smell
        5. Use "std::format" rather than "std::vformat" when the format string is known at compile time

           Code Smell
        6. Concatenated "std::format" outputs should be replaced by a single invocation

           Code Smell
        7. Width, alignment, and padding format options should be used consistently

           Code Smell
        8. Explicit argument indexing in "std::format" should be used only for non-trivial ordering

           Code Smell
        9. "std::declval" should not be used within requires-expression

           Code Smell
        10. STL constrained algorithms with range parameter should be used when iterating over the entire range

           Code Smell
        11. "std::enable_if" should not be used

           Code Smell
        12. Function template parameters should be named if reused

           Code Smell
        13. "std::span" should be used for a uniform sequence of elements contiguous in memory

           Code Smell
        14. Operator spaceship "<=>" should be used to define comparable types

           Code Smell
        15. Redundant comparison operators should not be defined

           Code Smell
        16. "std::format" should be used instead of string concatenation and "std::to_string"

           Code Smell
        17. "contains" should be used to check if a key exists in a container

           Code Smell
        18. Elements in a container should be erased with "std::erase" or "std::erase_if"

           Code Smell
        19. Mathematical constants should not be hardcoded

           Code Smell
        20. Use discriminated unions or "std::variant"

           Code Smell
        21. Unnecessary expensive copy should be avoided when using auto as a placeholder type

           Code Smell
        22. The right template argument should be specified for std::forward

           Code Smell
        23. Exception specifications should be treated as part of the type

           Code Smell
        24. "auto" should be used for non-type template parameter

           Code Smell
        25. Use "std::variant" instead of unions with non-trivial types.

           Code Smell
        26. Free functions should be preferred to member functions when accessing a container in a generic context

           Code Smell
        27. "std::optional" member function "value_or" should be used

           Code Smell
        28. "std::byte" should be used when you need byte-oriented memory access

           Code Smell
        29. The "_t" and "_v" version of type traits should be used instead of "::type" and "::value"

           Code Smell
        30. Inline variables should be used to declare global variables in header files

           Code Smell
        31. "[*this]" should be used to capture the current object by copy

           Code Smell
        32. "static_assert" with no message should be used over "static_assert" with empty or redundant message

           Code Smell
        33. Redundant class template arguments should not be used

           Code Smell
        34. Objects should not be created solely to be passed as arguments to functions that perform delegated object creation

           Code Smell
        35. "std::filesystem::path" should be used to represent a file path

           Code Smell
        36. Fold expressions should be used instead of recursive template instantiations

           Code Smell
        37. "as_const" should be used to make a value constant

           Code Smell
        38. Structured binding should be used

           Code Smell
        39. "if" and "switch" initializer should be used to reduce scope of variables

           Code Smell
        40. Emplacement should be preferred when insertion creates a temporary with sequence containers

           Code Smell
        41. "std::visit" should be used to switch on the type of the current value in a "std::variant"

           Code Smell
        42. "std::scoped_lock" should be used instead of "std::lock_guard"

           Code Smell
        43. "bind" should not be used

           Code Smell
        44. Threads should not be detached

           Code Smell
        45. Loop variables should be declared in the minimal possible scope

           Code Smell
        46. "shared_ptr" should not be taken by rvalue reference

           Code Smell
        47. Inheriting constructors should be used

           Code Smell
        48. Return type of functions shouldn't be const qualified value

           Code Smell
        49. "std::endl" should not be used

           Code Smell
        50. C-style array should not be used

           Code Smell
        51. "auto" should be used to avoid repetition of types

           Code Smell
        52. Integer literals should not be cast to bool

           Code Smell
        53. STL algorithms and range-based for loops should be preferred to traditional for loops

           Code Smell
        54. Function pointers should not be used as function parameters

           Code Smell
        55. Macros should not be used to define constants

           Code Smell
        56. Function parameters should not be of type "std::unique_ptr<T> const &"

           Code Smell
        57. "const" and "volatile" should not be used in "enum" declarations

           Code Smell
        58. "static" should not be used in unnamed namespaces

           Code Smell
        59. "inline" should not be used redundantly

           Code Smell
        60. Declarations of functions defined outside of the class should not be marked as "inline"

           Code Smell
        61. Digit separators should be used

           Code Smell
        62. Base class access specifiers should not be redundant

           Code Smell
        63. Access specifiers should not be redundant

           Code Smell
        64. Special member function should not be defined unless a non standard behavior is required

           Code Smell
        65. Empty "case" clauses that fall through to the "default" should be omitted

           Code Smell
        66. "const" references to numbers should not be made

           Bug
        67. "catch" clauses should do more than rethrow

           Code Smell
        68. Exception specifications should not be used

           Code Smell
        69. Redundant casts should not be used

           Code Smell
        70. A "while" loop should be used instead of a "for" loop

           Code Smell
        71. Overriding member functions should do more than simply call the same member in the base class

           Code Smell
        72. "empty()" should be used to test for emptiness

           Code Smell
        73. Mergeable "if" statements should be combined

           Code Smell
        74. Variables of array type should not be declared

           Code Smell

        Redundant comparison operators should not be defined

        intentionality - clear
        maintainability
        Code Smell
        • since-c++20
        • clumsy
        • pitfall

        Why is this an issue?

        C++20 introduces rewriting rules that enable defining only a few operator overloads in a class to be able to compare class instances in many ways:

        • the "spaceship" operator<=> can replace all the other comparison operators in most cases: The code a @ b (where @ is one of the following operators: <, <=, >, or >=) can be implicitly rewritten to use either a<=>b or b<=>a, and its three-way comparison semantics instead.
        • If operator== is defined, a!=b can be implicitly rewritten !(a==b)
        • If an operator<=> is defined as =default, a matching operator== is automatically generated if it does not already exist.

        If you define your own version of any particular comparison operator, e.g., operator< in addition to the operator<=>, it will supersede the compiler-generated version and might result in a surprising behavior with operator< semantics inconsistent with the semantics of other operators defined through operator<=>.

        In most cases, you will only have to define the following set of comparison operators in your class (possibly several of those sets, to allow for mixed-type comparison):

        • No comparison operator, if the class should not be compared, or
        • only operator== for classes that can only be compared for equality (and inequality), or
        • only operator<=>, defined as =default for fully comparable classes that only need to perform comparison member by member, or
        • both operator<=> and operator== when the comparison is more complex.

        This rule will raise an issue when a class is defined:

        • With an operator<=> and any of the four operators <, <=, >, >= defined with the same argument type.
        • With both operator== and operator!= defined for the same types.
        • With a defaulted operator<=> and a defaulted operator== with the same argument types defined.
        • With two operator<=> or two operator== that are declared with the same argument types in reverse order.

        Noncompliant code example

        Example with redundant operations in the same class:

        class A {
          int field;
          public:
            auto operator<=>(const A&) const = default;
            bool operator<(const A& other) const { // Noncompliant: this definition is redundant when operator<=> is present
              return field < other.field;
            }
            bool operator==(const A&) const = default; // Noncompliant: unnecessary, this line is added implicitly
        };
        

        Example with equivalent operations in different order:

        class MyStr {
          friend std::strong_ordering operator<=>(MyStr const &s1, std::string const &s2);
          friend std::strong_ordering operator<=>(std::string const &s1, MyStr const &s2); // Noncompliant, redundant with the previous line
        };
        

        Compliant solution

        The class has been reduced to a minimal set:

        class A {
          int field;
          public:
            auto operator<=>(const A&) const = default; // Compliant: operator== is implicitly generated, and expressions with < can be written
        };
        
        // The following code is valid:
        void f(A const &a1, A const &a2) {
          bool b1 = a1 == a2; // Uses implicitly generated operator==
          bool b2 = a1 != a2; // Uses implicitly generated operator==, rewritten as: !(a1 == a2)
          bool b3 = a1 < a2; // Rewritten as: (a1 <=> a2) < 0
          bool b4 = a1 >= a2; // Uses implicitly generated operator==
          bool b1 = a1 == a2; // Uses implicitly generated operator==
        }
        

        Only one order needs to be written

        class MyStr {
          friend std::strong_ordering operator<=>(MyStr const &s1, std::string const &s2); // Compliant
        };
        
        // The following code is valid
        void f(MyStr const &s1, std::string const &s2) {
          bool b1 = s1 < s2; // Rewritten as: (s1<=>s2) < 0
          bool b2 = s2 >= s1; // Rewritten as 0 >= (s1<=>s2);
        }
        
          Available In:
        • SonarQube IdeCatch issues on the fly,
          in your IDE
        • SonarQube CloudDetect issues in your GitHub, Azure DevOps Services, Bitbucket Cloud, GitLab repositories
        • SonarQube ServerAnalyze code in your
          on-premise CI
          Developer Edition
          Available Since
          9.1

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use