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

FP on S3655 when the Optional is a class instance field

    Details

    • Type: False-Positive
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 6.7
    • Component/s: Symbolic Execution
    • Labels:
      None

      Description

      The following snippet raises FP on S3655 (OptionalGetBeforeIsPresentCheck) when the Optional value is a field from a different class instance:

      import java.util.Optional;
      
      class S3655FP {
      
        void method() {
          Foo foo = new Foo();
          if (foo.bar.isPresent() && foo.bar.get() instanceof String) { // FP here
            // do something
          }
          if (foo.bar.isPresent()) {
            doSomething(foo.bar.get()); // FP here
          }
      
          Optional<String> bar = new Foo().bar;
      
          if (bar.isPresent() && bar.get() instanceof String) { // No FP here
            //do something
          }
        }
      
        void doSomething(String string) {
          // do something
        }
      
        class Foo {
          Optional<String> bar = Optional.of("hello");
        }
      }
      

       

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                quentin.jaquier Quentin Jaquier
                Reporter:
                guillaume.dequenne Guillaume Dequenne
              • Votes:
                1 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: