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

Useless "if(true) {...}" and "if(false){...}" blocks should be removed

    Details

    • Type: Bug Detection
    • Status: Superseded
    • Resolution: Unresolved
    • Labels:
    • Message:
      Hide
      Java: Remove this useless ["if" statement|"else" clause].
      PL/SQL: Remove this ["IF"|"ELSIF"] statement.
      Show
      Java: Remove this useless ["if" statement|"else" clause]. PL/SQL: Remove this ["IF"|"ELSIF"] statement.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Targeted languages:
      ABAP, Objective-C, PL/I, Python, RPG, VB6
    • Covered Languages:
      C#, Flex, Go, Java, JavaScript, Kotlin, PHP, PL/SQL, Ruby, Scala, Swift, VB.Net
    • Irrelevant for Languages:
      HTML, T-SQL, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      2min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • CWE:
      CWE-489, CWE-570, CWE-571

      Description

      if statements with conditions that are always false have the effect of making blocks of code non-functional. if statements with conditions that are always true are completely redundant, and make the code less readable.

      There are three possible causes for the presence of such code:

      • An if statement was changed during debugging and that debug code has been committed.
      • Some value was left unset.
      • Some logic is not doing what the programmer thought it did.

      In any of these cases, unconditional if statements should be removed.

      Noncompliant Code Example

      if (true) {  
        doSomething(); 
      }
      ...
      if (false) {  
        doSomethingElse(); 
      }
      
      if (2 < 3 ) { ... }  // Noncompliant; always false
      
      int i = 0;
      int j = 0;
      // ...
      j = foo();
      
      if (j > 0 && i > 0) { ... }  // Noncompliant; always false - i never set after initialization
      
      boolean b = true;
      //...
      if (b || !b) { ... }  // Noncompliant
      

      Compliant Solution

      doSomething(); 
      ...
      

      See

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: