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

Related "if/else if" statements should not have the same condition

    Details

    • Type: Bug Detection
    • Status: Active
    • Resolution: Unresolved
    • Labels:
    • Message:
      This [branch|case] duplicates the one on line n.
    • Highlighting:
      Hide
      • primary: 2nd duplicate condition
      • secondary: 1st condition
        • message: Original

      Only the characters part of the conditions should be underlined.

      Show
      primary: 2nd duplicate condition secondary: 1st condition message: Original Only the characters part of the conditions should be underlined.
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Flex, PL/I, VB6
    • Covered Languages:
      ABAP, C#, C, C++, Cobol, Go, Java, JavaScript, Kotlin, Objective-C, PHP, PL/SQL, Python, RPG, Ruby, Scala, Swift, T-SQL, TypeScript, VB.Net
    • Irrelevant for Languages:
      HTML, XML
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      10min
    • Analysis Level:
      Syntactic Analysis
    • Analysis Scope:
      Main Sources, Test Sources
    • Common Rule:
      Yes
    • CERT:
      MSC12-C.
    • CPPCheck:
      duplicateIf, multiCondition
    • ESLint:
      no-duplicate-case
    • ESLint-SonarJS:
      no-identical-conditions
    • PC-Lint:
      137, 1137, 142, 1142
    • PVS-Studio:
      V3003
    • TSLint-SonarTS:
      no-identical-conditions

      Description

      A chain of if/else if statements is evaluated from top to bottom. At most, only one branch will be executed: the first one with a condition that evaluates to true.

      Therefore, duplicating a condition automatically leads to dead code. Usually, this is due to a copy/paste error. At best, it's simply dead code and at worst, it's a bug that is likely to induce further bugs as the code is maintained, and obviously it could lead to unexpected behavior.

      See

      • CERT, MSC12-C. - Detect and remove code that has no effect or is never executed

        Attachments

          Issue Links

          1.
          Java RSPEC-1863 Language-Specification Active Unassigned
          2.
          C-Family RSPEC-1864 Language-Specification Active Unassigned
          3.
          JavaScript: Related "if/else if" statements should not have the same condition RSPEC-1865 Language-Specification Active Unassigned
          4.
          PHP: Related "if/else if" statements and "cases" in a "switch" should not have the same condition RSPEC-1866 Language-Specification Active Unassigned
          5.
          PL/SQL: Related "IF/ELSIF" statements and "WHEN" clauses in a "CASE" should not have the same condition RSPEC-1868 Language-Specification Active Unassigned
          6.
          Cobol: Conditions in related "IF/ELSE IF" statements and "WHEN" clauses in "EVALUATE" statements should not have the same condition RSPEC-1972 Language-Specification Active Unassigned
          7.
          Swift: Related "if/else if" statements and "cases" in a "switch" should not have the same condition RSPEC-2628 Language-Specification Active Unassigned
          8.
          Python RSPEC-2680 Language-Specification Active Unassigned
          9.
          C# RSPEC-2731 Language-Specification Active Unassigned
          10.
          RPG: Related "IF/ELSEIF" statements and "WHEN" clauses in a "SELECT" should not have the same condition RSPEC-2867 Language-Specification Active Unassigned
          11.
          ABAP RSPEC-3702 Language-Specification Active Unassigned
          12.
          VB.NET: Related "If/ElseIf" statements should not have the same condition RSPEC-3713 Language-Specification Active Unassigned
          13.
          T-SQL: Related "IF"/"ELSE IF" statements and "WHEN" clauses in a "CASE" should not have the same condition RSPEC-4097 Language-Specification Active Unassigned
          14.
          Go RSPEC-4515 Language-Specification Active Unassigned
          15.
          Ruby: Related "if/elsif" statements and "when" in a "case" should not have the same condition RSPEC-4765 Language-Specification Active Unassigned
          16.
          Scala: Related "if"/"else if" statements and "case" in a "match" should not have the same condition RSPEC-4934 Language-Specification Active Unassigned
          17.
          Apex: Related "if"/"else if" statements and "when" in a "switch" should not have the same condition RSPEC-5013 Language-Specification Active Unassigned

            Activity

              People

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

                Dates

                • Created:
                  Updated: