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

FP in S2583 with Java 16 pattern matching

    XMLWordPrintable

    Details

      Description

      When using java 16 pattern matching, S2583 (condition always true or false) can raise false positives.

      public static void main(String[] args) {
       Object o = new byte[0];
       if (o instanceof byte[] arrayByte) {
         doSomethingWithByte(arrayByte);
       } else if (o instanceof char[] arrayChar) { // always evaluate to "false" --> FP
         doSomethingWithChar(arrayChar);
       }
      }
      
      private static void doSomethingWithChar(char[] arrayChar) {
       System.out.println("char");
      }
      
      private static void doSomethingWithByte(byte[] arrayByte) {
       System.out.println("byte");
      }
      

        Attachments

          Activity

            People

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

              Dates

              Created:
              Updated:
              Resolved: