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

FP in S1612 when lambda argument is a subtype of ambiguous method parameter

    Details

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

      Description

      SONARJAVA-2795 removed FP when the call is ambiguous and can not be replaced by a lambda.

      It uses a heuristic to identify if a method can be ambiguous:

      suitable method is instance method with no parameters, or static method with single parameter of the same type as lambda argument

      The last part is not precise enough, the lambda argument type can also be a subtype. If it is the case, the rule will still report FP.

      public class S1612 {
      
        public static void main(String[] args) {
          Function<B, String> f = a -> a.f();  // FP , A::f can't be used
        }
      }
      
      class A {
      
        String f() {
          return "";
        }
      
        static String f(A a) {
          return "";
        }
      
      }
      
      class B extends A { }

        Attachments

          Issue Links

            Activity

              People

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

                Dates

                • Due:
                  Created:
                  Updated:
                  Resolved: