UnusedPrivateMethod when overloaded method called from lambda

  • versions used (SonarQube, Scanner, language analyzer)
    sonar maven plugin 3.7.0.1746
    sonarqube 8.4.2.36762
  • minimal code sample to reproduce (with analysis parameter, and potential instructions to compile).
import java.util.Map;
import java.util.function.IntFunction;

public class UnusedMethod {
	private static final IntFunction<String> NO_OP = n -> "";

	private final Map<String, IntFunction<String>> functions = Map.of("key", input -> method(input));

	public String result(String key, Integer input) {
		return functions.getOrDefault(key, NO_OP).apply(input);
	}

	private String method(Double input) {
		return input.toString();
	}

	private String method(Integer input) {
		return method(input.doubleValue());
	}
}

Hello,
In the example above sonar reports that String method(Integer input) is unused and should be removed, although the method is called from within the lambda expression in the map.
If the lambda is replaced with method reference, or the methods use primitives as parameters then the issue does not appear.

Thank you for your time.

Hello @greenman18523,

I just tested this exact same sample of code with a fresh install of SQ 8.4.2, the same maven plugin, and a small maven project on Java 11, having only this class.
There is no issue raised on my side, and the rule is behaving as expected.

There is maybe something different in your configuration.

  • What’s your version of the SonarSource Java Analyzer (visible through the marketplace, or at this URL once you replaced localhost by your host: http://localhost:9000/deploy/plugins/index.txt). I would expect to have sonar-java-plugin-6.5.1.22586.jar
  • Did you simplify the code a bit too much?

Cheers,
Michael

Hello @Michael,

Thanks for the fast reply. You are right, on 8.4.2 there is no issue, there was a misconfiguration on my part and the analysis was done on an older version of SQ.

Great! Thanks for the update and happy to see you fixed the issue on your side!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.