S1612 and casting inside ifPresent()

Hi, please consider following case:

public class IfPresent {
    interface Interface {

    class Type1 implements Interface {

    class Type2 implements Interface {
    	void method(final boolean parameter) {

    public static void main(final String[] args) {
    	final Optional<Boolean> optional = Optional.ofNullable(null);
    	final Interface instance = new IfPresent().new Type1();
        // optional.ifPresent(((Type2) instance)::method);
    	optional.ifPresent(i -> ((Type2) instance).method(i));

There is a rule S1612 violation (“Lambdas should be replaced with method references”), but this cannot be replaced - this can be seen by uncommenting the above line - in that case the expression is evaluated before checking if optional is present (from what I understand) and it throws an exception.

version used: SonarLint for Eclipse 3.6

Hi @noelo

Good catch, indeed it’s a false positive, but I don’t think we will fix it as it represents such a corner case, ROI is low. Also I don’t think it’s a good idea to rely on optional to know which implementation of the interface you have, it seems to me that such approach is pretty error-prone.