FP java:S1612 Replace lambda with ::cast

  • What language is this for? Java

  • Which rule? java:S1612

  • Why do you believe it’s a false-positive/false-negative? The suggested fix produces a compile error

  • Are you using

    • SonarQube for IDE - which IDE/version?
    • SonarLint 10.12.0.79769 with IntelliJ IDEA 2024.3 Ultimate
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

Rule suggests to replace combined cast by a method reference.

List<C> result = items.stream()
  .filter(i instanceof A & i instanceof B)
  .map(i -> (A & B) i) // FP Replace this lambda with method A.class::cast
  .filter(A::isFoo)
  .map(B::asC)
  .toList();

Hello @bduderstadt,

Thank you for your report. This is indeed a case that the rule should ignore. I have added it to this existing ticket about S1612 related to cast and instanceof operations.

1 Like