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 798
  • Vulnerability14
  • Bug173
  • Security Hotspot19
  • Code Smell592

  • Quick Fix 99
Filtered: 78 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. There shall be no conversion to type "bool"

           Code Smell
        75. "Symmetrical operators" should only be implemented as non-member functions

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

           Code Smell
        77. The raw pointer constructors of "std::shared_ptr" and "std::unique_ptr" should not be used

           Code Smell
        78. The "assert" macro shall not be used with a "constant-expression"

           Code Smell

        The raw pointer constructors of "std::shared_ptr" and "std::unique_ptr" should not be used

        intentionality - complete
        maintainability
        Code Smell
        Quick FixIDE quick fixes available with SonarQube for IDE
        • clumsy
        • since-c++11
        • misra-c++2023
        • misra-advisory

        Why is this an issue?

        More Info

        This rule is part of MISRA C++:2023.

        Usage of this content is governed by Sonar’s terms and conditions. Redistribution is prohibited.

        Rule 23.11.1 - The raw pointer constructors of std::shared_ptr and std::unique_ptr should not be used

        [unique.ptr]
        [util.smartptr.shared]

        Category: Advisory

        Analysis: Decidable,Single Translation Unit

        Amplification

        This rule applies to the use of the constructors of std::shared_ptr and std::unique_ptr that take ownership of the raw pointer passed as an argument.

        Rationale

        The functions std::make_shared and std::make_unique perform two operations at the same time:

        • Creating an object dynamically (equivalent to new); and
        • Creating a smart pointer that will manage the newly created object’s lifetime.

        Performing both operations in one step ensures that there is no interleaved operation that could throw an exception before the smart pointer has taken ownership of the object. This also prevents two unique_ptr or unrelated shared_ptr instances from "owning" the same object.

        Notes:

        • std::make_shared will allocate a single memory area for both the object and the bookkeeping data required for shared pointers (the reference counts). While this is usually beneficial in terms of performance, it has the drawback that the memory for the object will not be reclaimed when the last shared_ptr pointing to it is destroyed, but only when all weak_ptr references to the object are also destroyed. If this behaviour is undesirable, a custom variant of std::make_shared can be provided that omits this optimisation.
        • Since C++17, the evaluation order of function calls has been made stricter and some of the issues with interleaved calls can no longer happen. However, the use of make_shared or make_unique is still clearer and can result in better performance.

        Example

        struct A { int8_t i; };
        class B { };
        
        void f0()
        {
          auto p = std::make_shared< A > ();      // Compliant
        
          int8_t * pi = &( p->i );
          std::shared_ptr< int8_t > q ( p, pi );  // Does not apply - not taking ownership
        }
        
        auto f1()
        {
          auto * p1 = new A ();
          auto   p2 = std::make_unique< A >();    // make_unique may throw
        
          return std::shared_ptr< A >( p1 );      // Non-compliant - memory leak if
        }                                         //                 make_unique throws
        
        auto f2( std::unique_ptr< A > p )
        {
          auto q = p.get();
          // ...
          return std::unique_ptr< A >( q );       // Non-compliant - causes double delete
        }
        
        void f3( std::shared_ptr< A > a, std::shared_ptr< B > b );
        
        void f4()
        {
          f3( std::shared_ptr< A >( new A() ),
              std::shared_ptr< B >( new B() ) );  // Non-compliant - but well defined
        }                                         //                 in C++17
        

        Prior to C++ 17, a possible sequencing for the operations in the call to f3, where $n represents an object in the abstract machine, was:

        • new A() -> $1
        • new B() -> $2
        • std::shared_ptr< A >( $1 ) -> $3
        • std::shared_ptr< B >( $2 ) -> $4
        • f3( $3, $4 )

        If an exception is thrown during the construction of B, the object of type A will leak. This does not happen in the following as there are no interleaving operations:

        void f5()
        {
          f3( std::make_shared< A >(),
              std::make_shared< B >() );          // Compliant
        }
        

        Copyright The MISRA Consortium Limited © 2023

          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

        © 2025 SonarSource Sàrl. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use