SonarLint Rule S1612 is giving wrong hint

Using SonarLint Plugin 5.1.0.34721 for IntelliJ gives a hint based on S1612 for line with mapToObj:

Replace this lambda with method reference ‘char.class::cast’.

    public static void main(String[] args) {
        final List<Character> characters = "abc"
                .chars()
                .mapToObj(i -> (char) i)
                .collect(Collectors.toList());
        
        characters.forEach(c->System.out.println("char: "+c));
    }

After changing the code it produces a ClassCastException at runtime:

Cannot cast java.lang.Integer to char

Hi @jazz64,

Thank you for the report. You’re right, the rule is clearly giving wrong advice here. I’ve created a ticket to fix the issue.

Cheers,
Sebastian

That’s nice. Thanks for your effort so far! :smiley:

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