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: 2 rules found
user-experience
    Impact
      Clean code attribute
        1. Facilities in <random> should be used instead of "srand", "rand" and "random_shuffle"

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

           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