diff --git a/checkers/boolExprSimplify_checker.go b/checkers/boolExprSimplify_checker.go index 325fb56a3..8e931cc4b 100644 --- a/checkers/boolExprSimplify_checker.go +++ b/checkers/boolExprSimplify_checker.go @@ -6,15 +6,16 @@ import ( "go/token" "strconv" - "github.com/go-critic/go-critic/checkers/internal/astwalk" - "github.com/go-critic/go-critic/checkers/internal/lintutil" - "github.com/go-critic/go-critic/framework/linter" "github.com/go-toolsmith/astcast" "github.com/go-toolsmith/astcopy" "github.com/go-toolsmith/astequal" "github.com/go-toolsmith/astp" "github.com/go-toolsmith/typep" "golang.org/x/tools/go/ast/astutil" + + "github.com/go-critic/go-critic/checkers/internal/astwalk" + "github.com/go-critic/go-critic/checkers/internal/lintutil" + "github.com/go-critic/go-critic/framework/linter" ) func init() { diff --git a/checkers/ruleguard_checker.go b/checkers/ruleguard_checker.go index d65669fdd..e164dede8 100644 --- a/checkers/ruleguard_checker.go +++ b/checkers/ruleguard_checker.go @@ -12,8 +12,9 @@ import ( "sort" "strings" - "github.com/go-critic/go-critic/framework/linter" "github.com/quasilyte/go-ruleguard/ruleguard" + + "github.com/go-critic/go-critic/framework/linter" ) func init() { @@ -41,6 +42,14 @@ If flag is set, the value must be a comma-separated list of error conditions. * 'import': rule refers to a package that cannot be loaded. * 'dsl': gorule file does not comply with the ruleguard DSL.`, }, + "enable": { + Value: "", + Usage: "comma-separated list of enabled groups or skip empty to enable everything", + }, + "disable": { + Value: "", + Usage: "comma-separated list of disabled groups or skip empty to enable everything", + }, } info.Summary = "Runs user-defined rules using ruleguard linter" info.Details = "Reads a rules file and turns them into go-critic checkers." @@ -124,13 +133,43 @@ func newRuleguardChecker(info *linter.CheckerInfo, ctx *linter.CheckerContext) ( fset := token.NewFileSet() filePatterns := strings.Split(rulesFlag, ",") + enabledGroups := make(map[string]bool) + disabledGroups := make(map[string]bool) + + for _, g := range strings.Split(info.Params.String("disable"), ",") { + g = strings.TrimSpace(g) + disabledGroups[g] = true + } + flagEnable := info.Params.String("enable") + if flagEnable != "" { + for _, g := range strings.Split(flagEnable, ",") { + g = strings.TrimSpace(g) + enabledGroups[g] = true + } + } ruleguardDebug := os.Getenv("GOCRITIC_RULEGUARD_DEBUG") != "" loadContext := &ruleguard.LoadContext{ Fset: fset, DebugImports: ruleguardDebug, - DebugPrint: func(s string) { - fmt.Println("debug:", s) + DebugPrint: debugPrint, + GroupFilter: func(g string) bool { + whyDisabled := "" + enabled := flagEnable == "" || enabledGroups[g] + switch { + case !enabled: + whyDisabled = "not enabled by -enabled flag" + case disabledGroups[g]: + whyDisabled = "disabled by -disable flag" + } + if ruleguardDebug { + if whyDisabled != "" { + debugPrint(fmt.Sprintf("(-) %s is %s", g, whyDisabled)) + } else { + debugPrint(fmt.Sprintf("(+) %s is enabled", g)) + } + } + return whyDisabled == "" }, } @@ -236,3 +275,7 @@ func runRuleguardEngine(ctx *linter.CheckerContext, f *ast.File, e *ruleguard.En } } } + +func debugPrint(s string) { + fmt.Println("debug:", s) +}