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

All branches in a conditional structure should not have exactly the same implementation

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
      None
    • Message:
      Hide
      Remove this conditional structure or edit its code blocks so that they're not all the same.
      This conditional operation returns the same value whether the condition is "true" or "false".
      Show
      Remove this conditional structure or edit its code blocks so that they're not all the same. This conditional operation returns the same value whether the condition is "true" or "false".
    • Highlighting:
      • if or switch.
      • For a ternary, condition and ?
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      APEX, PL/I, Python, RPG, Rust, Solidity, VB6
    • Covered Languages:
      ABAP, C#, C, C++, Cobol, Flex, Go, Java, JavaScript, Kotlin, Objective-C, PHP, PL/SQL, Ruby, Scala, Swift, T-SQL, TypeScript, VB.Net
    • Irrelevant for Languages:
      HTML, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      15min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • ESLint-SonarJS:
      no-all-duplicated-branches
    • TSLint-SonarTS:
      no-all-duplicated-branches

      Description

      Having all branches in a switch or if chain with the same implementation is an error. Either a copy-paste error was made and something different should be executed, or there shouldn't be a switch/if chain at all.

      Noncompliant Code Example

      if (b == 0) {  // Noncompliant
        doOneMoreThing();
      } else {
        doOneMoreThing();
      }
      
      int b = a > 12 ? 4 : 4;  // Noncompliant
      
      switch (i) {  // Noncompliant
        case 1: 
          doSomething();
          break;
        case 2: 
          doSomething();
          break;
        case 3:
          doSomething(); 
          break;
        default: 
          doSomething();
      }
      

      Exceptions

      This rule does not apply to if chains without else-s, or to switch-es without default clauses.

      if(b == 0) {    //no issue, this could have been done on purpose to make the code more readable
        doSomething();
      } else if(b == 1) {
        doSomething();
      }
      

        Attachments

          Issue Links

          1.
          JavaScript RSPEC-3929 Language-Specification Active Unassigned
          2.
          T-SQL RSPEC-4093 Language-Specification Active Unassigned
          3.
          TypeScript RSPEC-4126 Language-Specification Active Unassigned
          4.
          PL/SQL RSPEC-4234 Language-Specification Active Unassigned
          5.
          ABAP RSPEC-4257 Language-Specification Active Unassigned
          6.
          COBOL RSPEC-4270 Language-Specification Active Unassigned
          7.
          PHP RSPEC-4311 Language-Specification Active Unassigned
          8.
          C# RSPEC-4523 Language-Specification Active Unassigned
          9.
          Go RSPEC-4550 Language-Specification Active Unassigned
          10.
          Swift RSPEC-4609 Language-Specification Active Unassigned
          11.
          Ruby RSPEC-4761 Language-Specification Active Unassigned
          12.
          Kotlin RSPEC-4802 Language-Specification Active Unassigned
          13.
          VB.NET RSPEC-4856 Language-Specification Active Unassigned
          14.
          Scala RSPEC-4924 Language-Specification Active Unassigned
          15.
          Apex RSPEC-5003 Language-Specification Active Unassigned

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                ann.campbell.2 Ann Campbell
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: