-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ruleguard: add support for local functions
This feature is useful for rule filters readability improvements. Instead of copying a complex `Where()` expression several times, one can now use a local function literal to define that filter operation and use it inside `Where()` expressions. Here is an example: ```go func preferFprint(m dsl.Matcher) { isFmtPackage := func(v dsl.Var) bool { return v.Text == "fmt" && v.Object.Is(`PkgName`) } m.Match(`$w.Write([]byte($fmt.Sprint($*args)))`). Where(m["w"].Type.Implements("io.Writer") && isFmtPackage(m["fmt"])). Suggest("fmt.Fprint($w, $args)"). Report(`fmt.Fprint($w, $args) should be preferred to the $$`) m.Match(`$w.Write([]byte($fmt.Sprintf($*args)))`). Where(m["w"].Type.Implements("io.Writer") && isFmtPackage(m["fmt"])). Suggest("fmt.Fprintf($w, $args)"). Report(`fmt.Fprintf($w, $args) should be preferred to the $$`) // ...etc } ``` Note that we used `isFmtPackage` in more than 1 rule. Functions can accept almost arbitrary params, but there are some restrictions on what kinds of arguments they can receive right now. These arguments work: * Matcher var expressions like `m["varname"]` * Basic literals like `"foo"`, `104`, `5.2` * Constants
- Loading branch information
Showing
11 changed files
with
297 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
//go:build ignore | ||
// +build ignore | ||
|
||
package gorules | ||
|
||
import "github.com/quasilyte/go-ruleguard/dsl" | ||
|
||
func testRules(m dsl.Matcher) { | ||
bothConst := func(x, y dsl.Var) bool { | ||
return x.Const && y.Const | ||
} | ||
m.Match(`test("both const", $x, $y)`). | ||
Where(bothConst(m["x"], m["y"])). | ||
Report(`true`) | ||
|
||
intValue := func(x dsl.Var, val int) bool { | ||
return x.Value.Int() == val | ||
} | ||
m.Match(`test("== 10", $x)`). | ||
Where(intValue(m["x"], 10)). | ||
Report(`true`) | ||
|
||
isZero := func(x dsl.Var) bool { return x.Value.Int() == 0 } | ||
m.Match(`test("== 0", $x)`). | ||
Where(isZero(m["x"])). | ||
Report(`true`) | ||
|
||
// Testing closure-captured m variable. | ||
fmtIsImported := func() bool { | ||
return m.File().Imports(`fmt`) | ||
} | ||
m.Match(`test("fmt is imported")`). | ||
Where(fmtIsImported()). | ||
Report(`true`) | ||
|
||
// Testing explicitly passed matcher. | ||
ioutilIsImported := func(m2 dsl.Matcher) bool { | ||
return m2.File().Imports(`io/ioutil`) | ||
} | ||
m.Match(`test("ioutil is imported")`). | ||
Where(ioutilIsImported(m)). | ||
Report(`true`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package localfunc | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
) | ||
|
||
func test(args ...interface{}) {} | ||
|
||
func _() { | ||
fmt.Println("ok") | ||
_ = ioutil.Discard | ||
|
||
var i int | ||
|
||
test("both const", 1, 2) // want `true` | ||
test("both const", 1, 2+2) // want `true` | ||
test("both const", i, 2) | ||
test("both const", 1, i) | ||
test("both const", i, i) | ||
|
||
test("== 10", 10) // want `true` | ||
test("== 10", 9+1) // want `true` | ||
test("== 10", 11) | ||
test("== 10", i) | ||
|
||
test("== 0", 0) // want `true` | ||
test("== 0", 1-1) // want `true` | ||
test("== 0", 11) | ||
test("== 0", i) | ||
|
||
test("fmt is imported") // want `true` | ||
|
||
test("ioutil is imported") // want `true` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package localfunc | ||
|
||
func _() { | ||
test("fmt is imported") | ||
|
||
test("ioutil is imported") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.