Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ruleguard: add filter by tags for analyzer mode #1205

Merged
merged 12 commits into from Mar 28, 2022
3 changes: 2 additions & 1 deletion checkers/analyzer/run.go
Expand Up @@ -7,9 +7,10 @@ import (
"strings"
"sync"

"golang.org/x/tools/go/analysis"

_ "github.com/go-critic/go-critic/checkers" // Register go-critic checkers
"github.com/go-critic/go-critic/framework/linter"
"golang.org/x/tools/go/analysis"
)

type gocritic struct {
Expand Down
25 changes: 25 additions & 0 deletions checkers/ruleguard_checker.go
Expand Up @@ -135,20 +135,41 @@ func newRuleguardChecker(info *linter.CheckerInfo, ctx *linter.CheckerContext) (

enabledGroups := make(map[string]bool)
disabledGroups := make(map[string]bool)
enabledTags := make(map[string]bool)
disabledTags := make(map[string]bool)

for _, g := range strings.Split(info.Params.String("disable"), ",") {
g = strings.TrimSpace(g)
if t := strings.Split(g, "#"); len(t) == 2 {
disabledTags[t[1]] = true
continue
}

disabledGroups[g] = true
}
flagEnable := info.Params.String("enable")
if flagEnable != "<all>" {
for _, g := range strings.Split(flagEnable, ",") {
g = strings.TrimSpace(g)
if t := strings.Split(g, "#"); len(t) == 2 {
enabledTags[t[1]] = true
quasilyte marked this conversation as resolved.
Show resolved Hide resolved
continue
}

enabledGroups[g] = true
}
}
ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != ""

inTags := func(g *ruleguard.GoRuleGroup, tags map[string]bool) bool {
for _, t := range g.DocTags {
if tags[t] {
return true
}
}
return false
}

loadContext := &ruleguard.LoadContext{
Fset: fset,
DebugImports: ruleguardDebug,
Expand All @@ -161,6 +182,10 @@ func newRuleguardChecker(info *linter.CheckerInfo, ctx *linter.CheckerContext) (
whyDisabled = "not enabled by -enabled flag"
case disabledGroups[g.Name]:
whyDisabled = "disabled by -disable flag"
case len(enabledTags) != 0 && !inTags(g, enabledTags):
whyDisabled = "not enabled by tags in -enable flag"
case len(disabledTags) != 0 && inTags(g, disabledTags):
whyDisabled = "disabled by tags in -disable flag"
}
if ruleguardDebug {
if whyDisabled != "" {
Expand Down
5 changes: 3 additions & 2 deletions framework/lintmain/internal/check/check.go
Expand Up @@ -19,10 +19,11 @@ import (
"strings"
"sync"

"github.com/go-critic/go-critic/framework/linter"
"github.com/go-critic/go-critic/framework/lintmain/internal/hotload"
"github.com/go-toolsmith/pkgload"
"golang.org/x/tools/go/packages"

"github.com/go-critic/go-critic/framework/linter"
"github.com/go-critic/go-critic/framework/lintmain/internal/hotload"
)

// Main implements sub-command entry point.
Expand Down