Enum: difference of behaviour between exhaustive switch vs. using default:

XMLWordPrintable

    • Type: Bug
    • Resolution: Not an Issue
    • Priority: P4
    • 25
    • Affects Version/s: None
    • Component/s: tools
    • None

      This is possibly a bug with DA/DU.

      public enum Action { IGNORE, REJECT }
      private static void bad() {
          String s;
          switch (getAction()) {
              case IGNORE:
                  s = "foo";
                  break;
              case REJECT:
                  throw new RuntimeException("REJECTED");
          };
          System.out.println(s); // <------- variable s might not have been initialized
      }

      private static void ok() {
          String s = switch (getAction()) {
              case IGNORE:
                  yield "foo";
              case REJECT:
                  throw new RuntimeException("REJECTED");
          };
          System.out.println(s); // ok !
      }

      reported and discussed in: https://mail.openjdk.org/pipermail/amber-dev/2024-December/009139.html

            Assignee:
            Jan Lahoda
            Reporter:
            Angelos Bimpoudis
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: