quasilyte/go-ruleguard

doesn't match already declared in previous expression variable in new expression

peakle opened this issue · 3 comments

This is probably because the first match stops the other rules from being applied.
This behavior is intuitive with single statement/expression patterns, but it gets more and more surprising the broader the scope becomes. In your example, a whole function is a "scope", so it short-circuits from the function as soon as the first rule matches.

Maybe it should be fixed for stmtList and exprList nodes, I'm not sure yet.

As a side note, patterns like that (from your referenced example) are quite fragile and should not be relied upon too much. ruleguard works best for the expression level and single statement level. It's also good enough for the X ; Y cases, where one statement is immediately followed by another, like in foo(); bar(). Other (more complicated) patterns are usually a sign that this diagnostic should be implemented manually (for example, in go-critic normal checkers).

As a side note, will this pattern be good enough in your case?

$err = $f($*args); if err != nil { $*_ }

Without surrounding $*_ and func.

As a side note, will this pattern be good enough in your case?

$err = $f($*args); if err != nil { $*_ }

Without surrounding $*_ and func.

yes, i also thougth about this pattern after your comment, also i split rule into two unrelated groups, but problem still reproducable: delivery-club/delivery-club-rules@f77c82c