SonarQube JavaScript
  1. SonarQube JavaScript
  2. SONARJS-128

Rule: If ... else if constructs shall be terminated with an else clause

    Details

    • Type: New Feature New Feature
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.2
    • Component/s: None
    • Labels:
      None
    • Number of attachments :
      0

      Description

      • Title
        If ... else if constructs shall be terminated with an else clause
      • Description
        This rule applies whenever an if statement is followed by one or more else if statements; the final else if shall be followed by an else statement. In the case of a simple if statement then the else statement need not be included.

      The requirement for a final else statement is defensive programming. The else statement should either take appropriate action or contain a suitable comment as to why no action is taken. This is consistent with the requirement to have a final default clause in a switch statement.

      The following code snippet illustrates this rule:

        int x = 0;
      
        /* This "if" statement is fine, since it has no "else if" clause, no "else" is required */
        if (x == 0)
        {
          x = 42;
        }
      
        /* This "if" statement is also fine */
        if (x == 0)
        {
          x = 42;
        }
        else
        {
          x = -42;
        }
      
        /* This "if" statement is bad, because it has an "else if" but no "else" clause */
        if (x == 0) /* Non-Compliant */
        {
          x = 42;
        }
        else if (x == 1)
        {
          x = -42;
        }
      
      • Violation message
        Add a final else to this If ... else if construct
      • Default severity
        Major

      Part of Sonar Way profile

        Issue Links

          Activity

          Hide
          Fabrice Bellingard added a comment -

          Works perfectly.

          Could you just remove it from "Sonar Way"? (this rule collides with "Avoid empty block" when the last "else" must intentionally be left empty)

          Show
          Fabrice Bellingard added a comment - Works perfectly. Could you just remove it from "Sonar Way"? (this rule collides with "Avoid empty block" when the last "else" must intentionally be left empty)
          Hide
          Evgeny Mandrikov added a comment -

          Fixed.

          Show
          Evgeny Mandrikov added a comment - Fixed.
          Hide
          Fabrice Bellingard added a comment -

          Perfect!

          Show
          Fabrice Bellingard added a comment - Perfect!
          Hide
          Evgeny Mandrikov added a comment -

          Integration tests were updated.

          Show
          Evgeny Mandrikov added a comment - Integration tests were updated.

            People

            • Assignee:
              Evgeny Mandrikov
              Reporter:
              Freddy Mallet
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: