S878 Unexpected use of comma operator

SonarLint v3.3.3
vscode 1.64.2

Possibly false positive in such a case:

[1, 2, 1, 1].reduce(
    ( acc, n ) => ( acc[n] += 1, acc ), [0, 0, 0] ) // [ 0, 3, 1 ]
//                             ^
// Unexpected use of comma operator. ( javascript:S878 )

According to JavaScript static code analysis: Comma operator should not be used

Exceptions
Use of comma operator is tolerated:

  • If the expression sequence is explicitly wrapped in parentheses.

With additional parentheses the linter issues another warning

[1, 2, 1, 1].reduce(
    ( acc, n ) => ( ( acc[n] += 1, acc ) ), [0, 0, 0] ) // [ 0, 3, 1 ]
//                ^
//  Remove these useless parentheses. [+1 location] ( javascript:S1110 )

Hello John,

Welcome to SonarSource community!

There seems to be some inconsistencies between the behaviour of these two rule, that is, one suggesting to wrap a sequence expression in parentheses and another one hinting to remove the very same parentheses… This is indeed a false-positive that deserves a fix

I created the following ticket to address that as soon as possible.

Thank you,
Yassin

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