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: 55 rules found
bad-practice
    Impact
      Clean code attribute
        1. Pointer and reference parameters should be "const" if the corresponding object is not modified

           Code Smell
        2. Reserved identifiers should not be defined or declared

           Code Smell
        3. Only standard forms of the "defined" directive should be used

           Code Smell
        4. Function-like macros should not be used

           Code Smell
        5. Recursion should not be used

           Code Smell
        6. "continue" should not be used

           Code Smell
        7. Bitwise operators should not be applied to signed operands

           Bug
        8. Escape sequences should use the delimited form (\u{}, \o{}, \x{})

           Code Smell
        9. Names of well-known C standard library macros and functions should not be used as identifiers

           Code Smell
        10. C++ formatting functions should be used instead of C printf-like functions

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

           Code Smell
        12. Template should not be constrained with ad-hoc requires-expression

           Code Smell
        13. Use type-erased "coroutine_handle" when applicable

           Code Smell
        14. Well-defined type-punning method should be used instead of a union-based one

           Bug
        15. "std::cmp_*" functions should be used to compare unsigned values with negative values

           Bug
        16. STL constrained algorithms with range parameter should be used when iterating over the entire range

           Code Smell
        17. "std::cmp_*" functions should be used to compare signed and unsigned values

           Code Smell
        18. "std::bit_cast" should be used to reinterpret binary representation instead of "std::memcpy"

           Code Smell
        19. "[[likely]]" and "[[unlikely]]" should be used instead of compiler built-ins

           Code Smell
        20. "starts_with" and "ends_with" should be used for prefix and postfix checks

           Code Smell
        21. "std::jthread" should be used instead of "std::thread"

           Code Smell
        22. "nodiscard" attributes on functions should include explanations

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

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

           Code Smell
        25. Macros should not be used as replacements for "typedef" and "using"

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

           Code Smell
        27. Multiple mutexes should not be acquired with individual locks

           Code Smell
        28. "try_lock", "lock" and "unlock" should not be directly used for mutexes

           Code Smell
        29. A single statement should not have more than one resource allocation

           Code Smell
        30. Function parameters that are rvalue references should be moved

           Code Smell
        31. Capture by reference in lambdas used locally

           Code Smell
        32. Size of bit fields should not exceed the size of their types

           Code Smell
        33. Functions should not have more than one argument of type "bool"

           Code Smell
        34. Non-const global variables should not be used

           Code Smell
        35. The order for arguments of the same type in a function call should be obvious

           Code Smell
        36. "std::move" should only be used where moving can happen

           Code Smell
        37. Classes should not contain both public and private data members

           Code Smell
        38. Pointer and reference local variables should be "const" if the corresponding object is not modified

           Code Smell
        39. Argument of "printf" should be a format string

           Code Smell
        40. Template parameters should be preferred to "std::function" when configuring behavior at compile time

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

           Code Smell
        42. Memory should not be managed manually

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

           Code Smell
        44. "nullptr" should be used to denote the null pointer

           Code Smell
        45. Exceptions should not be thrown in "noexcept" functions

           Code Smell
        46. Redundant lambda return types should be omitted

           Code Smell
        47. Member variables should be initialized

           Bug
        48. Loops with at most one iteration should be refactored

           Bug
        49. "if" statements should be preferred over "switch" when simpler

           Code Smell
        50. Track uses of "NOSONAR" comments

           Code Smell
        51. Generic exceptions should not be caught

           Code Smell
        52. Deprecated attributes should include explanations

           Code Smell
        53. Standard outputs should not be used directly to log anything

           Code Smell
        54. Classes should not be inherited virtually

           Code Smell
        55. The "union" keyword shall not be used

           Code Smell

        Redundant lambda return types should be omitted

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

        Why is this an issue?

        More Info

        It is a best practice to make lambda return types implicit whenever possible. First and foremost, doing so avoids implicit conversions, which could result in data or precision loss. Second, omitting the return type often helps future-proof the code.

        The issue is raised when explicit return types are used.

        Noncompliant code example

        // Noncompliant: the explicit return types are redundant.
        [](int i) -> int { return i + 42; }
        []() -> auto { return foo(); }
        [](int x) -> std::vector<int> { return std::vector<int>{ x }; }
        

        Compliant solution

        // Compliant: no explicit return type.
        [](int i) { return i + 42; }
        []() { return foo(); }
        [](int x) { return std::vector<int>{ x }; }
        

        Exceptions

        There are a few exceptions to this rule.

        First, no issue is raised when the compiler is not deducing the same type by itself. This can happen when a conversion is requested.

        // Compliant: the compiler would deduce a different return type.
        [](int x) -> double { return x; }
        [](float x) -> int { return x; } // Precision loss, see S5276.
        

        The compiler also deduces a different type when there are no return statements and the explicit return type is not void.

        // Compliant: removing these explicit return types would result in a different signature.
        [](int x) -> int { throw std::runtime_error("No more eggs"); }
        [](int x) -> int { std::terminate(); }
        

        Another similar situation is when references are involved: instead, the compiler deduces a value without an explicit return type. This can have an impact on both correctness and performance.

        // Compliant: removing the explicit return type would return a copy.
        [](std::vector<int>& data) -> auto& {
            std::sort(data.begin(), data.end());
            return data;
        }
        

        Additionally, no issues are raised when the deduction of the return type is not available. This is the case with C++20 coroutines in their lambda form.

        // Compliant: coroutine lambdas cannot rely on type deduction.
        []() -> Task { co_await std::suspend_always{}; }
        

        In some other cases, removing the explicit return type would result in ill-formed programs. This is the case when using initializer lists or aggregate initializations.

        // Compliant: type deduction wouldn't work.
        [](int x) -> std::vector<int> { return { x }; }
        []() -> std::array<int, 4> { return { 1, 2, 3, 4 }; }
        

        Removing the explicit return type when a lambda has multiple return statements of different types would also result in ill-formed programs. Here are two examples where the explicit return type introduces useful implicit conversions.

        // Compliant: omitting the return type would result in a compilation error.
        [](Base* ptr) -> Derived* {
            if (auto* derived = dynamic_cast<Derived*>(ptr)) {
                actOnDerived(derived);
                return derived;
            }
        
            // "nullptr_t" mismatches the previous return type "Derived*".
            return nullptr;
        }
        
        // Compliant: omitting the return type would result in a compilation error.
        [](std::string_view request) -> std::variant<Error, Data> {
            if (!isRequestValid(request)) {
                return Error{ "invalid request" };
            }
        
            auto reader = readRequest(request);
            // "Data" mismatches the previous return type "Error".
            return Data{ reader.data(), reader.size() };
        }
        

        Finally, this rule does not trigger on explicit template-dependent or constrained return types since they can have a use of their own, help readability or improve maintainability.

        // Compliant: enforce "process()" returns a reference.
        [](auto x) -> auto& { return process(x); }
        
        // Compliant: ensure the lambda returns a reference when "f()" does.
        [f](auto arg) -> decltype(f(arg)) { return f(arg); }
        
        // Compliant: the return type restricts possible input types.
        [](auto x) -> std::enable_if_t<std::is_integral_v<decltype(x)>> { process(x); }
        
        template <typename T>
        T getGlobalProperty(std::string_view name, T defaultValue) { /* ... */ }
        
        // Compliant: the return type is constrained (C++20) and can help maintainability.
        auto getProperty = [](std::string_view name) -> std::totally_ordered auto {
            return getGlobalProperty(name, 0);
        }
        
          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