java:S6212 has false positive when local variable is an array

Hi there,

For the following example code:

String[] array = {"A", "B", "C"};

The variable name is annotated with the warning “Declare this local variable with “var” instead. | sonarlint(java:S6212)”.

If the code is amended as follows:

var array = {"A", "B", "C"};

A compiler error results: “Array initializer needs an explicit target-type | Java(16778722)”

In fact, according to JEP 286 (emphasis added):

Poly expressions that require such a type, like lambdas, method references, and array initializers, will trigger an error.

Hence, this is positively a false positive :slight_smile:

Thanks,
ADTC

1 Like

Hello @ADTC

You are correct, this is a false positive. Ticket created: SONARJAVA-3991.

Thanks for taking the time to report and clearly describe this problem.

Best,
Quentin

1 Like

You’re welcome, and thank you too. I appreciate your response.

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