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

S1612 should not suggest casting though method reference for generic classes

    XMLWordPrintable

    Details

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

      Description

      public static List<MyClass<String>> codeLensSuppliersById(List<Object> l) {
        return l.stream()
          .map(o -> (MyClass<String>) o) // FP: result in "java: incompatible types: cannot infer type-variable(s) R,A,capture#1 of ?,T"
          .collect(Collectors.toList());
      }
      
      class MyClass<T> {
      }
      

      While it is possible to legitimately use method reference for casting to a generic class, we should not suggest it as it is not strictly equivalent, and can result in an incompatible type.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              massimo.paladin Massimo PALADIN
              Reporter:
              quentin.jaquier Quentin Jaquier
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: