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: 36 rules found
convention
    Impact
      Clean code attribute
        1. Literal suffix "L" for long integers shall be upper case

           Code Smell
        2. Source code should only use /* ... */ style comments

           Code Smell
        3. Concept names should comply with a naming convention

           Code Smell
        4. Coroutine names should comply with a naming convention

           Code Smell
        5. Format strings should comply with ISO standards

           Code Smell
        6. Method overloads should be grouped together in the interface

           Code Smell
        7. Label names should comply with a naming convention

           Code Smell
        8. Enumeration values should comply with a naming convention

           Code Smell
        9. Enumeration names should comply with a naming convention

           Code Smell
        10. Namespace names should comply with a naming convention

           Code Smell
        11. "final" should not be used redundantly

           Code Smell
        12. Comment styles "//" and "/* ... */" should not be mixed within a file

           Code Smell
        13. Preprocessor directives should not be indented

           Code Smell
        14. "union" names should comply with a naming convention

           Code Smell
        15. "public", "protected" and "private" sections of a class should be declared in that order

           Code Smell
        16. C++ comments should be used

           Code Smell
        17. Track "TODO" and "FIXME" comments that do not contain a reference to a person

           Code Smell
        18. Multiple variables should not be declared on the same line

           Code Smell
        19. "struct" names should comply with a naming convention

           Code Smell
        20. File names should comply with a naming convention

           Code Smell
        21. Macro names should comply with a naming convention

           Code Smell
        22. Track lack of copyright and license headers

           Code Smell
        23. Comments should not be located at the end of lines of code

           Code Smell
        24. Functions without parameters should not use "(void)"

           Code Smell
        25. Assignment operators should return non-"const" reference to the assigned object

           Code Smell
        26. Statements should be on separate lines

           Code Smell
        27. Local variable and function parameter names should comply with a naming convention

           Code Smell
        28. Field names should comply with a naming convention

           Code Smell
        29. Lines should not end with trailing whitespaces

           Code Smell
        30. Files should end with a newline

           Code Smell
        31. Tabulation characters should not be used

           Code Smell
        32. Lines should not be too long

           Code Smell
        33. Class names should comply with a naming convention

           Code Smell
        34. Function names should comply with a naming convention

           Code Smell
        35. The "#include" directive shall be followed by either a "<filename>" or ""filename"" sequence

           Bug
        36. A "declaration" should not declare more than one variable or member variable

           Code Smell

        Assignment operators should return non-"const" reference to the assigned object

        consistency - conventional
        maintainability
        Code Smell
        • convention
        • cppcoreguidelines
        • user-experience

        Why is this an issue?

        More Info

        Copy assignment operators and move assignment operators can return anything, including void.

        However, if you decide to declare them yourself (don’t forget the "Rule-of-Zero" described in S3624), it is a recommended practice to return a non-const reference to the assigned object (left-operand). It allows the developer to chain the assignment operations, increasing consistency with what other types do and, in some cases, enabling the writing of concise code.

        This rule will raise for assignment operators that deviate from the above expectation.

        Using an unconventional return type

        This rule will raise an issue if the return type of the copy or move assignment operator, is different from mutable reference to the class type.

        Noncompliant code example

        class Clazz {
        public:
          const Clazz& operator=(const Clazz& other); // Noncompliant, returns const reference
          Clazz operator=(Clazz&& other) noexcept;    // Noncompliant, returns by value
        };
        

        Compliant solution

        class Clazz {
        public:
          Clazz& operator=(const Clazz& other);      // Compliant
          Clazz& operator=(Clazz&& other) noexcept;  // Compliant
        };
        

        Returning an object different from *this

        An assignment operator should return a reference to the assigned object. Conventionally, such return is expressed as return *this, and the rule will mark any return statement as deviating from this convention.

        Noncompliant code example

        class Clazz {
        public:
          Clazz& set(Clazz& other);
          Clazz& operator=(Clazz const& other) {
            return set(other); // Noncompliant: depends on return of `set` member function
          }
        
          Clazz&& operator=(Clazz&& other) noexcept {
            return other;      // Noncompliant, also return type is non-compliant
          }
        };
        

        Compliant solution

        class Clazz {
        public:
          Clazz& set(Clazz& other);
          Clazz& operator=(Clazz const& other) {
            set(other);
            return *this;      // Compliant
          }
        
          Clazz& operator=(Clazz&& other) noexcept {
            return *this;      // Compliant
          }
        };
        

        In C++23, if the assignment operator is declared using an explicit object argument, the rule will mark any return statement that does not return the object parameter directly.

        Noncompliant code example

        class Clazz {
        public:
          Clazz& set(Clazz& other);
          Clazz& operator=(this Clazz& self, Clazz const& other) {
            return self.set(other); // Noncompliant: depends on the return of `set` member function
          }
        };
        

        Compliant solution

        class Clazz {
        public:
          Clazz& set(Clazz& other);
          Clazz& operator=(this Clazz& self, Clazz const& other) {
            self.set(other);
            return self;      // Compliant
          }
        };
        

        Declaring assignment operation as non-mutating

        The assignment operation is designed to change the value of the target object, to the same one as the source. Such operation is mutating and thus should not be declared with a const qualifier.

        Noncompliant code example

        class Clazz {
        public:
          Clazz& operator=(Clazz const& other) const { // Noncompliant: also leads to noncompliant return statement
            return const_cast<Clazz&>(*this);
          }
          Class& operator=(Clazz&& other) const; // Noncompliant
        };
        

        Compliant solution

        class Clazz {
        public:
          Clazz& operator=(Clazz const& other) { // Compliant
            return *this;
          }
          Clazz& operator=(Clazz&& other); // Compliant
        };
        

        When declaring an assignment operator with C++23 explicit object argument, the object argument should not be passed:

        • by const reference - this is equivalent to declaring the implicit object parameter method as const, as described above;
        • by value - in this case a temporary object will be created, and modified by the assignment operator, instead of the left-hand side of the assignment operator

        Noncompliant code example

        class Clazz {
          int val;
        public:
          Clazz& operator=(this Clazz const& self, Clazz const& other) const { // Noncompliant: also leads to non-compliant return
            return const_cast<Clazz&>(self);
          }
          void operator=(this Clazz self, Clazz&& other) { // Noncompliant
            self.val = other.val; // Modifies temporary object
          }
        };
        

        Compliant solution

        class Clazz {
          int val;
        public:
          Clazz& operator=(this Clazz& self, Clazz const& other) { // Compliant
            self.val = other.val;
            return self;
          }
          Clazz& operator=(this Clazz& self, Clazz&& other) { // Compliant
            self.val = other.val; // Modifies referenced object
            return self;
          }
        };
        

        Exceptions

        This rule will not raise an issue when the assignment operator’s return type is declared void. That syntax is commonly used when assignment operator chaining is not desired. The issue will still be raised if such an assignment operator is declared as non-mutating.

        class Clazz {
          int val;
        public:
          void operator=(Clazz const& other) { // Compliant
            self.val = other.val;
            return self;
          }
          void operator=(Clazz&& other) const; // Noncompliant: declared as const
        };
        
          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