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: 4 rules found
design
    Impact
      Clean code attribute
        1. "using" should be preferred for type aliasing

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

           Code Smell
        3. Two branches in a conditional structure should not have exactly the same implementation

           Code Smell
        4. Inheritance tree of classes should not be too deep

           Code Smell

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

        intentionality - clear
        maintainability
        Code Smell
        • cppcoreguidelines
        • design
        • bad-practice
        • since-c++11
        • clumsy

        Why is this an issue?

        More Info

        If you use std::unique_ptr<T> const & for a function parameter type, it means that the function will not be able to alter the ownership of the pointed-to object by the unique_ptr:

        • It cannot acquire ownership of the pointed-to object (this would require a parameter of type std::unique_ptr<T>)
        • It cannot transfer the object ownership to someone else (this would require a std::unique_ptr<T> &).

        That means the function can only observe the pointed-to object, and in this case, passing a T* (if the unique_ptr can be null) or a T& (if it cannot) provides the same features, while also allowing the function to work with objects that are not handled by a unique_ptr (e.g., objects on the stack, in a vector, or in another kind of smart pointer), thus making the function more general-purpose.

        Noncompliant code example

        void draw(std::unique_ptr<Shape> const &shape); // Noncompliant
        
        void drawAll(std::vector<std::unique_ptr<Shape>> v)
        {
          for (auto &shape : v) {
              if (shape) {
                draw(shape);
              }
          }
        }
        

        Compliant solution

        void draw(Shape const &shape); // Compliant
        
        void drawAll(std::vector<std::unique_ptr<Shape>> v)
        {
          for (auto &shape : v) {
              if (shape) {
                draw(*shape);
              }
          }
        }
        
          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