Skip to content

Commit

Permalink
ruleguard: continue pattern matching for multi-match patterns (#340)
Browse files Browse the repository at this point in the history
This is probably a temporary and sub-optimal solution, but
it should do for now.

Since matches are much more rare than pattern misses,
we do the check under the `matched` condition.
Extra condition shouldn't make anything measurably slower there.

Fixes #339
  • Loading branch information
quasilyte committed Jan 2, 2022
1 parent 00cd1e9 commit 9b8154c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 7 deletions.
10 changes: 5 additions & 5 deletions analyzer/testdata/src/regression/issue115.go
Expand Up @@ -4,10 +4,10 @@ func testIssue115() {
intFunc := func() int { return 19 }
stringFunc := func() string { return "19" }

println(13)
println(43 + 5)
println(13, "!constexpr int")
println(43+5, "!constexpr int")

println("foo") // want `\Q"foo" is not a constexpr int`
println(intFunc()) // want `\QintFunc() is not a constexpr int`
println(stringFunc()) // want `\QstringFunc() is not a constexpr int`
println("foo", "!constexpr int") // want `\Q"foo" is not a constexpr int`
println(intFunc(), "!constexpr int") // want `\QintFunc() is not a constexpr int`
println(stringFunc(), "!constexpr int") // want `\QstringFunc() is not a constexpr int`
}
22 changes: 22 additions & 0 deletions analyzer/testdata/src/regression/issue339.go
@@ -0,0 +1,22 @@
package regression

func _() {
println("339") // want `\Qpattern1`
println("x") // want `\Qpattern2`

println("339") // want `\Qpattern1`

println("x")
}

func _() {
println("x") // want `\Qpattern2`
println("339") // want `\Qpattern1`

println("x") // want `\Qpattern2`
println("339") // want `\Qpattern1`

println("x")
println("x") // want `\Qpattern2`
println("339")
}
8 changes: 7 additions & 1 deletion analyzer/testdata/src/regression/rules.go
@@ -1,3 +1,4 @@
//go:build ignore
// +build ignore

package gorules
Expand All @@ -21,7 +22,7 @@ func issue72(m dsl.Matcher) {
}

func issue115(m dsl.Matcher) {
m.Match(`println($x)`).
m.Match(`println($x, "!constexpr int")`).
Where(!(m["x"].Const && m["x"].Type.Is("int"))).
Report("$x is not a constexpr int")
}
Expand All @@ -45,3 +46,8 @@ func issue291(m dsl.Matcher) {
At(m["iota"]).
Report("good, have explicit type")
}

func issue339(m dsl.Matcher) {
m.Match(`println("339"); println("x")`).Report("pattern1")
m.Match(`println("x"); println("339")`).Report("pattern2")
}
9 changes: 8 additions & 1 deletion ruleguard/runner.go
Expand Up @@ -233,7 +233,7 @@ func (rr *rulesRunner) runRules(n ast.Node) {
profiling.Leave(rr.bgContext)
}

if matched {
if matched && !multiMatchTags[tag] {
break
}
}
Expand Down Expand Up @@ -414,3 +414,10 @@ func truncateText(s string, maxLen int) string {
right := s[len(s)-rightLen:]
return left + placeholder + right
}

var multiMatchTags = [nodetag.NumBuckets]bool{
nodetag.BlockStmt: true,
nodetag.CaseClause: true,
nodetag.CommClause: true,
nodetag.File: true,
}

0 comments on commit 9b8154c

Please sign in to comment.