False positive for "Local variables should not be declared and then immediately returned or thrown (php:S1488)"?

VSCode 1.55.0 (Windows)
Sonarlint 1.21.0

“Local variables should not be declared and then immediately returned or thrown (php:S1488)”

Is not only triggered for

[...]
$local_variable ='value';
return $local_variable;

where it makes a lot of sense but also for

$local_variable = '';
$local_variable .= 'append something';
[...]
$local_variable .= 'append the last thing';
return $local_variable;

where it would be difficult to include the value of the local variable in the return statement.

Bug or feature?

Hello @OlafEichstaedt, and welcome to the community!

Good question, indeed.

I’ll discuss that with the team and get back to you in the next couple of days.

Karim.

Hey @OlafEichstaedt,

Finally, we have decided this is not how we want the rule to behave. Thus, it is considered as a bug.

I’ve created a ticket to track this.

Thank you very much for noticing and reporting!

Karim.

1 Like

My pleasure, I enjoy working with your poducts.

Thanks for the feedback.

4 Likes

Please note, the rule also triggers a false positive in the following case:

$html = "something [...]";
$html = str_replace( 'something', 'something else', $html );
return $html;

Hey @OlafEichstaedt,

Good catch. I’ve adapted the created ticket to take into account the cases in which the assigned value depends on the variable itself.

Thanks again!

Karim.

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