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

FP in S2589/S2583 when value changes in lambda

    Details

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

      Description

      S2589/S2583 are not correctly detecting when a boolean value is changed inside a lambda (for a Listener, Consumer, ...).

      This results in FP for S2589.
      Since S2583 rely on the same code, it is also probably affected.

      import java.util.function.Consumer;
      
      class MyClasss {
        private static boolean test = false;
      
        static void listener_noncompliant() {
          test = false;
          MyClasss.doSomethingWithIt(e -> test = true);
          if (test) { // Noncompliant, FP
            System.out.println("Is this reachable? Yes");
          }
        }
      
        static void listener_compliant() {
          test = false;
          doSomethingWithIt(e -> test = true);
          if (test) { // Compliant, True negative
            System.out.println("Is this reachable? Yes");
          }
        }
        static void doSomethingWithIt(Consumer<String> o) {
          o.accept("");
        }
      }
      
      

      Note that everything works as expected when the method is called directly (see listener_compliant()).

       

      Other example with listener.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                quentin.jaquier Quentin Jaquier
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated: