Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-2589

Boolean expressions should not be gratuitous

    Details

    • Message:
      * Remove this expression which always evaluates to "[true|false]".
    • Highlighting:
      Hide

      Primary: the gratuitous expression
      Secondary: the last locations in the code leading to know something on each variable part of the gratuitous expression.

      Show
      Primary: the gratuitous expression Secondary: the last locations in the code leading to know something on each variable part of the gratuitous expression.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      ABAP, Flex, Objective-C, PHP, PL/I, PL/SQL, Python, RPG, Swift, T-SQL, VB.Net
    • Covered Languages:
      C#, C, C++, Cobol, Java, JavaScript, TypeScript
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Level:
      Control-flow Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Implementation details:
    • Common Rule:
      Yes
    • CERT:
      MSC12-C.
    • CWE:
      CWE-489, CWE-571, CWE-570
    • MISRA C 2004:
      13.7
    • MISRA C 2012:
      14.3
    • CPPCheck:
      secondAlwaysTrueFalseWhenFirstTrue, redundantCondition
    • FindBugs:
      BC_VACUOUS_INSTANCEOF,BC_IMPOSSIBLE_INSTANCEOF,NP_NULL_INSTANCEOF,SIO_SUPERFLUOUS_INSTANCEOF

      Description

      If a boolean expression doesn't change the evaluation of the condition, then it is entirely unnecessary, and can be removed. If it is gratuitous because it does not match the programmer's intent, then it's a bug and the expression should be fixed.

      Noncompliant Code Example

      a = true;
      if (a) { // Noncompliant
        doSomething();
      }
      
      if (b && a) { // Noncompliant; "a" is always "true"
        doSomething();
      }
      
      if (c || !a) { // Noncompliant; "!a" is always "false"
        doSomething();
      }
      

      Compliant Solution

      a = true;
      if (foo(a)) {
        doSomething();
      }
      
      if (b) {
        doSomething();
      }
      
      if (c) {
        doSomething();
      }
      

      See

        Attachments

          Issue Links

          1.
          COBOL RSPEC-2666 Language-Specification Active Unassigned
          2.
          JavaScript RSPEC-3952 Language-Specification Active Unassigned
          3.
          C# RSPEC-3970 Language-Specification Active Unassigned
          4.
          C-Family RSPEC-5365 Language-Specification Active Unassigned
          5.
          Python RSPEC-5609 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                pierre-yves.nicolas Pierre-Yves Nicolas
              • Votes:
                1 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: