-
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.
all: make syntax patterns aware of stdlib packages (#260)
When you write `fmt.Sprint`, you probably want to only match `Sprint` function from `fmt` package, not a `Sprint` method call on `fmt` variable or whatever. If we need to match a variable, a filter expression in `Where` can explicitly state that. For now, we only recognize stdlib packages. In the future, I would consider matching `m.Import()`-ed packages in a smart way as well. That could simplify the rules writing and make them more correct by default.
- Loading branch information
Showing
17 changed files
with
490 additions
and
335 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package stdlib | ||
|
||
import "io" | ||
|
||
type foo struct{} | ||
|
||
func (foo) WriteString(args ...interface{}) {} | ||
|
||
func (foo) Sprint(args ...interface{}) string { return "" } | ||
|
||
func sink(args ...interface{}) {} | ||
|
||
func test(w io.Writer) { | ||
io.WriteString(w, "") // want `\QWriteString from stdlib` | ||
|
||
{ | ||
var io foo | ||
io.WriteString(w, "") | ||
} | ||
} |
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,17 @@ | ||
package stdlib | ||
|
||
import ( | ||
"fmt" | ||
io "myio" | ||
) | ||
|
||
func _(w io.Writer) { | ||
io.WriteString(w, "") | ||
|
||
sink(fmt.Sprint(1), fmt.Sprint("ok")) // want `\Qsink with two Sprint from stdlib` | ||
|
||
{ | ||
var fmt foo | ||
sink(fmt.Sprint(1), fmt.Sprint("ok")) | ||
} | ||
} |
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,18 @@ | ||
package stdlib | ||
|
||
import ( | ||
iorenamed "io" | ||
) | ||
|
||
func _(w iorenamed.Writer) { | ||
iorenamed.WriteString(w, "") // want `\QWriteString from stdlib` | ||
|
||
{ | ||
var io foo | ||
io.WriteString(w, "") | ||
} | ||
{ | ||
var iorenamed foo | ||
iorenamed.WriteString(w, "") | ||
} | ||
} |
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,13 @@ | ||
// +build ignore | ||
|
||
package gorules | ||
|
||
import ( | ||
"github.com/quasilyte/go-ruleguard/dsl" | ||
) | ||
|
||
func testRules(m dsl.Matcher) { | ||
m.Match(`io.WriteString($*_)`).Report(`WriteString from stdlib`) | ||
|
||
m.Match(`sink(fmt.Sprint($_), fmt.Sprint($_))`).Report(`sink with two Sprint from stdlib`) | ||
} |
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,9 @@ | ||
package myio | ||
|
||
import "io" | ||
|
||
type Writer interface { | ||
io.Writer | ||
} | ||
|
||
func WriteString(w Writer, s string) {} |
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
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.