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

Change message of S3655 to mention isEmpty and improve rule description

    XMLWordPrintable

    Details

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

      Description

      Consider the following code:

      if (methodThatReturnsOptional().isEmpty()) return;
      doSomethingWith(methodThatReturnsOptional().get());
      

      This will tell the user to call isPresent() on the optional before calling get() on it. This happens because the call to isEmpty() is performed on a different symbolic value than the call to get() because of the repeated method call. However, a user might reasonably assume that it's because isEmpty() is used instead of isPresent().

      To avoid this confusion, the message should mention isEmpty() as well as isPresent().

      The rule description should also be amended to explain that results of multiple method calls will be treated as different values as a method might potentially return a different value each time (and that the current rule implementation currently treats all methods as if they do so). With a noncompliant-compliant example pair showing how storing a method's result in a local variable fix the issue.

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                Created:
                Updated:
                Resolved: