Uploaded image for project: 'SonarJava'
  1. SonarJava
  2. SONARJAVA-3730

Add an exception to rule S121 for early returns

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.14
    • Component/s: Rules
    • Labels:
      None

      Description

      Rule S121 requires the body of control structures to be surrounded by curly braces. This is generally considered best practice with one exception:

      Early return statements are often written as a single line with no braces: if (condition) return false;. This style is so common that code that's auto-generated by IntelliJ for equals (and possibly other methods) uses this style as well.

      To not report issues on what's commonly considered idiomatic code, we should not raise an issue when the following conditions are met:

      • The control structure in question is an if statement
      • The body is on the same line as the if keyword and no further statements appear on that same line.
      • The body is a return, break or continue statement.

      The description of the rule should be updated (for Java) to document this exception.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                quentin.jaquier Quentin Jaquier
                Reporter:
                sebastian.hungerecker Sebastian Hungerecker
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: