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

FP on S1905 when casted argument is an ambiguous method reference.

    XMLWordPrintable

    Details

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

      Description

      S1905 reports an issue when the casted argument is an ambiguous method reference. It typically happens for Function and Supplier.

      See following situation:

      import java.util.function.Function;
      import java.util.function.Supplier;
      
      class FP_S1905 {
        static class Overloaded {
          static String f() {
            return "";
          }
      
          static String f(String a) {
            return "";
          }
        }
      
        void main() {
          foo((Supplier<String>) Overloaded::f); // FP, cast is mandatory
          foo((Function<String, String>) Overloaded::f); // FP, cast is mandatory
          foo(Overloaded::f); // Does not compile without cast
          // It can typically happen with constructor:
          foo((Supplier<String>) String::new); // FP
          foo((Function<String, String>) String::new); // FP
          foo(String::new); // Does not compile without cast
        }
      
        void foo(Supplier<String> supplier) {
        }
        void foo(Function<String, String> function) {
        }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              margarita.nedzelska Margarita Nedzelska
              Reporter:
              quentin.jaquier Quentin Jaquier
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Due:
                Created:
                Updated:
                Resolved: