Skip to content
This repository has been archived by the owner on Jun 9, 2021. It is now read-only.

Commit

Permalink
fix range stmt matching
Browse files Browse the repository at this point in the history
We also need to check Tok in non-aggressive mode,
so range:= and range= patterns match only respective statements.

Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
  • Loading branch information
quasilyte authored and mvdan committed Dec 26, 2019
1 parent 1489261 commit 1827046
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion match.go
Expand Up @@ -588,7 +588,13 @@ func (m *matcher) node(expr, node ast.Node) bool {
m.optNode(x.Post, y.Post) && m.node(x.Body, y.Body)
case *ast.RangeStmt:
y, ok := node.(*ast.RangeStmt)
return ok && m.node(x.Key, y.Key) && m.node(x.Value, y.Value) &&
if !ok {
return false
}
if !m.aggressive && x.Tok != y.Tok {
return false
}
return m.node(x.Key, y.Key) && m.node(x.Value, y.Value) &&
m.node(x.X, y.X) && m.node(x.Body, y.Body)

case *ast.TypeSpec:
Expand Down
2 changes: 2 additions & 0 deletions match_test.go
Expand Up @@ -530,6 +530,8 @@ func TestMatch(t *testing.T) {
// for and range stmts
{[]string{"-x", "for $x { $y }"}, "for b { c() }", 1},
{[]string{"-x", "for $x := range $y { $z }"}, "for i := range l { c() }", 1},
{[]string{"-x", "for $x := range $y { $z }"}, "for i = range l { c() }", 0},
{[]string{"-x", "for $x = range $y { $z }"}, "for i := range l { c() }", 0},
{[]string{"-x", "for range $y { $z }"}, "for _, e := range l { e() }", 0},

// $*_ matching stmt+expr combos (ifs)
Expand Down

0 comments on commit 1827046

Please sign in to comment.