Skip to content

Commit

Permalink
checkers: add skip experimental tag by default for user defined rules (
Browse files Browse the repository at this point in the history
  • Loading branch information
peakle committed Apr 2, 2022
1 parent fa0a170 commit 498eef8
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 9 deletions.
4 changes: 4 additions & 0 deletions checkers/ruleguard_checker.go
Expand Up @@ -159,6 +159,10 @@ func newRuleguardChecker(info *linter.CheckerInfo, ctx *linter.CheckerContext) (
enabledGroups[g] = true
}
}

if !enabledTags["experimental"] {
disabledTags["experimental"] = true
}
ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != ""

inEnabledTags := func(g *ruleguard.GoRuleGroup) bool {
Expand Down
8 changes: 6 additions & 2 deletions checkers/testdata/_integration/ruleguard/f1.go
@@ -1,12 +1,16 @@
package foo

import "fmt"
import (
"fmt"
"regexp"
)

type myError error

func _() {
var s1, s2 string
var _ = fmt.Sprintf("%s%s", s1, s2)
r, _ := regexp.MatchString(`^\s`, fmt.Sprintf("%s%s", s1, s2))
println(r)
}

func _() {
Expand Down
6 changes: 3 additions & 3 deletions checkers/testdata/_integration/ruleguard/linttest.golden
@@ -1,4 +1,4 @@
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
./f1.go:25:6: ruleguard: use errors.New(k()) or fmt.Errorf("%s", k()) instead
./f1.go:8:1: ruleguard: error as an underlying type is probably a mistake
./f1.go:12:36: ruleguard: suggestion: s1+s2
./f1.go:29:6: ruleguard: use errors.New(k()) or fmt.Errorf("%s", k()) instead
2 changes: 1 addition & 1 deletion checkers/testdata/_integration/ruleguard/linttest.golden2
@@ -1,2 +1,2 @@
exit status 1
./f1.go:25:6: ruleguard: use errors.New(k()) or fmt.Errorf("%s", k()) instead
./f1.go:29:6: ruleguard: use errors.New(k()) or fmt.Errorf("%s", k()) instead
5 changes: 3 additions & 2 deletions checkers/testdata/_integration/ruleguard/linttest.golden3
@@ -1,3 +1,4 @@
exit status 1
./f1.go:14:2: ruleguard: may be simplified to one if
./f1.go:25:6: ruleguard: use errors.New(k()) or fmt.Errorf("%s", k()) instead
./f1.go:18:2: ruleguard: may be simplified to one if
./f1.go:12:10: ruleguard: regexp compilation should be avoided on the hot paths
./f1.go:29:6: ruleguard: use errors.New(k()) or fmt.Errorf("%s", k()) instead
5 changes: 5 additions & 0 deletions checkers/testdata/_integration/ruleguard/linttest.golden4
@@ -0,0 +1,5 @@
exit status 1
./f1.go:8:1: ruleguard: error as an underlying type is probably a mistake
./f1.go:18:2: ruleguard: may be simplified to one if
./f1.go:12:36: ruleguard: suggestion: s1+s2
./f1.go:29:6: ruleguard: use errors.New(k()) or fmt.Errorf("%s", k()) instead
3 changes: 2 additions & 1 deletion checkers/testdata/_integration/ruleguard/linttest.params
@@ -1,3 +1,4 @@
check -@ruleguard.rules ./rules.go -enable ruleguard -@ruleguard.disable=#test ./... | linttest.golden
check -@ruleguard.rules ./rules.go -enable ruleguard -@ruleguard.enable=#style -@ruleguard.disable=#test ./... | linttest.golden2
check -@ruleguard.rules ./rules.go -enable ruleguard -@ruleguard.enable=#style,#test ./... | linttest.golden3
check -@ruleguard.rules ./rules.go -enable ruleguard -@ruleguard.enable=#style,#test,#experimental ./... | linttest.golden3
check -@ruleguard.rules ./rules.go -enable ruleguard ./... | linttest.golden4
8 changes: 8 additions & 0 deletions checkers/testdata/_integration/ruleguard/rules.go
Expand Up @@ -29,3 +29,11 @@ func dynamicFmtString(m dsl.Matcher) {
Suggest("errors.New($f($args))").
Report(`use errors.New($f($args)) or fmt.Errorf("%s", $f($args)) instead`)
}

//doc:tags experimental
func regexpCompile(m dsl.Matcher) {
m.Match(`regexp.Match($*_)`,
`regexp.MatchString($*_)`,
`regexp.MatchReader($*_)`,
).Report(`regexp compilation should be avoided on the hot paths`)
}

0 comments on commit 498eef8

Please sign in to comment.