-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is part of the ruleguard integration experiment. Hopefully, we can get some feedback to improve the way it's integrated. The simplest way to try it out: gocritic check -enable ruleguard -@ruleguard.rules <gorules> <target> Where: <gorules> is a path to a Go rules file <target> is a linting target An example gorules file can be found here: https://github.com/quasilyte/go-ruleguard/blob/master/rules.go Visit https://github.com/quasilyte/go-ruleguard for more info on the ruleguard. Updates #873 Updates #869 Updates #837 Updates #836 Updates #831 Signed-off-by: Iskander Sharipov <quasilyte@gmail.com>
- Loading branch information
Showing
7 changed files
with
131 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package checkers | ||
|
||
import ( | ||
"bytes" | ||
"go/ast" | ||
"go/token" | ||
"io/ioutil" | ||
"log" | ||
|
||
"github.com/go-lintpack/lintpack" | ||
"github.com/quasilyte/go-ruleguard/ruleguard" | ||
) | ||
|
||
func init() { | ||
var info lintpack.CheckerInfo | ||
info.Name = "ruleguard" | ||
info.Tags = []string{"style", "experimental"} | ||
info.Params = lintpack.CheckerParams{ | ||
"rules": { | ||
Value: "", | ||
Usage: "path to a gorules file", | ||
}, | ||
} | ||
info.Summary = "Runs user-defined rules using ruleguard linter" | ||
info.Details = "Reads a rules file and turns them into go-critic checkers." | ||
info.Before = `N/A` | ||
info.After = `N/A` | ||
info.Note = "See https://github.com/quasilyte/go-ruleguard." | ||
|
||
collection.AddChecker(&info, func(ctx *lintpack.CheckerContext) lintpack.FileWalker { | ||
return newRuleguardChecker(&info, ctx) | ||
}) | ||
} | ||
|
||
func newRuleguardChecker(info *lintpack.CheckerInfo, ctx *lintpack.CheckerContext) *ruleguardChecker { | ||
c := &ruleguardChecker{ctx: ctx} | ||
rulesFilename := info.Params.String("rules") | ||
if rulesFilename == "" { | ||
return c | ||
} | ||
|
||
// TODO(quasilyte): handle initialization errors better when we make | ||
// a transition to the go/analysis framework. | ||
// | ||
// For now, we log error messages and return a ruleguard checker | ||
// with an empty rules set. | ||
|
||
data, err := ioutil.ReadFile(rulesFilename) | ||
if err != nil { | ||
log.Printf("ruleguard init error: %+v", err) | ||
return c | ||
} | ||
|
||
fset := token.NewFileSet() | ||
rset, err := ruleguard.ParseRules(rulesFilename, fset, bytes.NewReader(data)) | ||
if err != nil { | ||
log.Printf("ruleguard init error: %+v", err) | ||
return c | ||
} | ||
|
||
c.rset = rset | ||
return c | ||
} | ||
|
||
type ruleguardChecker struct { | ||
ctx *lintpack.CheckerContext | ||
|
||
rset *ruleguard.GoRuleSet | ||
} | ||
|
||
func (c *ruleguardChecker) WalkFile(f *ast.File) { | ||
if c.rset == nil { | ||
return | ||
} | ||
|
||
ctx := &ruleguard.Context{ | ||
Pkg: c.ctx.Pkg, | ||
Types: c.ctx.TypesInfo, | ||
Sizes: c.ctx.SizesInfo, | ||
Fset: c.ctx.FileSet, | ||
Report: func(n ast.Node, msg string, _ *ruleguard.Suggestion) { | ||
// TODO(quasilyte): investigate whether we should add a rule name as | ||
// a message prefix here. | ||
c.ctx.Warn(n, msg) | ||
}, | ||
} | ||
|
||
err := ruleguard.RunRules(ctx, f, c.rset) | ||
if err != nil { | ||
// Normally this should never happen, but since | ||
// we don't have a better mechanism to report errors, | ||
// emit a warning. | ||
c.ctx.Warn(f, "execution error: %v", err) | ||
} | ||
} |
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,10 @@ | ||
package foo | ||
|
||
import "fmt" | ||
|
||
type myError error | ||
|
||
func _() { | ||
var s1, s2 string | ||
var _ = fmt.Sprintf("%s%s", s1, s2) | ||
} |
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,3 @@ | ||
exit status 1 | ||
./f1.go:5:1: ruleguard: error as an underlying type is probably a mistake | ||
./f1.go:9:10: ruleguard: suggestion: s1+s2 |
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 @@ | ||
check -@ruleguard.rules ./rules.go -enable ruleguard ./... | linttest.golden |
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,15 @@ | ||
// +build ignore | ||
|
||
package gorules | ||
|
||
import "github.com/quasilyte/go-ruleguard/dsl/fluent" | ||
|
||
func _(m fluent.Matcher) { | ||
m.Match(`type $x error`). | ||
Report(`error as an underlying type is probably a mistake`). | ||
Suggest(`type $x struct { error }`) | ||
|
||
m.Match(`fmt.Sprintf("%s%s", $a, $b)`). | ||
Where(m["a"].Type.Is(`string`) && m["b"].Type.Is(`string`)). | ||
Suggest(`$a+$b`) | ||
} |
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