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: 6 rules found
since-c++23
    Impact
      Clean code attribute
        1. "std::views::as_const" should be used to prevent modifying range elements

           Code Smell
        2. Escape sequences should use the delimited form (\u{}, \o{}, \x{})

           Code Smell
        3. "std::stringstream" or "std::spanstream" should be used instead of "std::strstream"

           Code Smell
        4. The underlying value of an enum should be accessed through "to_underlying"

           Code Smell
        5. "contains" should be used to test whether a substring is part of a string

           Code Smell
        6. "if consteval" should be used instead of "if (std::is_constant_evaluated())"

           Code Smell

        "std::views::as_const" should be used to prevent modifying range elements

        intentionality - clear
        maintainability
        reliability
        Code Smell
        • pitfall
        • since-c++23

        Why is this an issue?

        How can I fix it?

        More Info

        std::views::as_const provides a constant view over a range. Using it removes the possibility of inadvertently modifying the underlying data of a range. This function is especially useful because in C++ const only applies to the top-level element, it is not deep-const.

        One common misunderstanding of how const works is when iterating on ranges whose element type is a pair/tuple of references. Even if the iteration variable is const-qualified, it is possible to modify the range’s underlying data through this variable:

        void example() {
          std::vector<int> v1 = createVector1();
          std::vector<int> v2 = createVector2();
        
          for (auto const&t : std::views::zip(v1, v2)) {
            get<0>(t) = get<1>(t); // Modifies data in v1
          }
        

        In this example, zip returns a view of std::tuple<int&, int&>, and even if p is const-qualified, it is still possible to change the underlying data via p, because the references inside the tuple never become const.

        On the other hand, if the example is rewritten this way:

        void example() {
          std::vector<int> v1 = createVector1();
          std::vector<int> v2 = createVector2();
        
          for (auto const &t : std::views::zip(v1, v2) | std::views::as_const) {
            get<0>(t) = get<1>(t); // Does not compile
          }
        

        The view is now over elements of type std::tuple<int const&, int const&> and can no longer be used to modify the data in v1 or v2.

        This rule raises an issue when iterating with a range-based for loop over a range whose elements are tuples or pairs containing non-constant references, and the iteration variable is a reference to a constant.

        This situation can easily happen when using std::views::zip, std::views::enumerate, std::flat_map…​

          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
          10.7

        © 2008-2025 SonarSource SA. All rights reserved.

        Privacy Policy | Cookie Policy | Terms of Use