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: 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. The literal value zero shall be the only value assigned to "errno"

           Code Smell

        The literal value zero shall be the only value assigned to "errno"

        consistency - conventional
        maintainability
        Code Smell
        • bad-practice
        • design
        • misra-c++2023
        • misra-required

        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 22.4.1 - The literal value zero shall be the only value assigned to errno

        Category: Required

        Analysis: Decidable,Single Translation Unit

        Amplification

        Note: the C++ Standard Library is permitted to assign a non-zero value to errno.

        Rationale

        Various functions within the C++ Standard Library set errno to a non-zero value to indicate that an error has been detected.

        This rule allows this error reporting behaviour to be used, but prevents developers from using errno as an error reporting mechanism within a project’s code. C++ provides better mechanisms for error handling.

        Example

        std::string getKey ( std::optional< std::string > const & key_data )
        {
          if ( key_data.has_value() && !key_data->empty() )
          {
            return key_data.value();
          }
        
          errno = 42;                  // Non-compliant - non-zero value
          errno = EINVAL;              // Non-compliant - does not expand to literal '0'
        
          return std::string {};
        }
        
        
        #define OK 0
        
        void f()
        {
          uint32_t success { 0 };
        
          errno = success;             // Non-compliant - must use literal '0'
          errno = OK;                  // Compliant - 'OK' expands to literal '0'
        
          errnoSettingFunction();
        
          if ( errno != success )
          {
            handleError();
          }
        }
        

        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