squid:S2293 generate false-positives for sub classes

We updated the sonar to the latest plugins and now enabled this rule again.

This is generating false-positives in this case:

public interface BaseClass { }
public static class ChildClass implements BaseClass { }
public static Map.Entry<String, BaseClass> provideExample() {
    return new AbstractMap.SimpleImmutableEntry<String, BaseClass>("", new ChildClass());
}

When I remove the types as suggested by the issue, I got an error:
image

Sonar version: 6.7.1 (build 35068)

Sonar java: sonar-java-plugin-5.11.0.17289

1 Like

Example.java with code provided by you after replacement of <String, BaseClass> on diamond operator <>

import java.util.AbstractMap;
import java.util.Map;

public class Example {

  public interface BaseClass { }
  public static class ChildClass implements BaseClass { }
  public static Map.Entry<String, BaseClass> provideExample() {
    return new AbstractMap.SimpleImmutableEntry<>("", new ChildClass());
  }
  
}

compiles just fine with javac 11.0.1, 10.0.1, 9.0.4 and 1.8.0_202.

To me seems that it is IntelliJ IDEA on your screenshot (is it?) and AFAIR its old versions had bugs in inference of types. IntelliJ IDEA 2019.1 suggest replacement

and doesn’t complain after replacement.

2 Likes