Skip to content

feat: exclude linters from severity-rules #4434

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2892,6 +2892,14 @@ severity:
# Default: false
case-sensitive: true

# When a list of exclude-linters is provided, the default severity of issues will not be applied to them.
# This allows for more complex linters such as 'revive' to conserve their own severity rules.
# This takes precedence over `severity-rules`.
#
# Default: []
exclude-linters:
- revive

# When a list of severity rules are provided, severity information will be added to lint issues.
# Severity rules have the same filtering capability as exclude rules
# except you are allowed to specify one matcher per severity rule.
Expand Down
7 changes: 4 additions & 3 deletions pkg/config/severity.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package config
const severityRuleMinConditionsCount = 1

type Severity struct {
Default string `mapstructure:"default-severity"`
CaseSensitive bool `mapstructure:"case-sensitive"`
Rules []SeverityRule `mapstructure:"rules"`
Default string `mapstructure:"default-severity"`
CaseSensitive bool `mapstructure:"case-sensitive"`
ExcludeLinters []string `mapstructure:"exclude-linters"`
Rules []SeverityRule `mapstructure:"rules"`
}

type SeverityRule struct {
Expand Down
8 changes: 6 additions & 2 deletions pkg/lint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ type Runner struct {
func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
es *lintersdb.EnabledSet,
lineCache *fsutils.LineCache, fileCache *fsutils.FileCache,
dbManager *lintersdb.Manager, pkgs []*gopackages.Package) (*Runner, error) {
dbManager *lintersdb.Manager, pkgs []*gopackages.Package,
) (*Runner, error) {
// Beware that some processors need to add the path prefix when working with paths
// because they get invoked before the path prefixer (exclude and severity rules)
// or process other paths (skip files).
Expand Down Expand Up @@ -113,7 +114,8 @@ func NewRunner(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
}

func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context,
lc *linter.Config) (ret []result.Issue, err error) {
lc *linter.Config,
) (ret []result.Issue, err error) {
defer func() {
if panicData := recover(); panicData != nil {
if pe, ok := panicData.(*errorutil.PanicError); ok {
Expand Down Expand Up @@ -333,13 +335,15 @@ func getSeverityRulesProcessor(cfg *config.Severity, log logutils.Log, files *fs
if cfg.CaseSensitive {
severityRulesProcessor = processors.NewSeverityRulesCaseSensitive(
cfg.Default,
cfg.ExcludeLinters,
severityRules,
files,
log.Child(logutils.DebugKeySeverityRules),
)
} else {
severityRulesProcessor = processors.NewSeverityRules(
cfg.Default,
cfg.ExcludeLinters,
severityRules,
files,
log.Child(logutils.DebugKeySeverityRules),
Expand Down
27 changes: 20 additions & 7 deletions pkg/result/processors/severity_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package processors

import (
"regexp"
"slices"

"github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils"
Expand All @@ -20,17 +21,22 @@ type SeverityRule struct {

type SeverityRules struct {
defaultSeverity string
excludedLinters []string
rules []severityRule
files *fsutils.Files
log logutils.Log
}

func NewSeverityRules(defaultSeverity string, rules []SeverityRule, files *fsutils.Files, log logutils.Log) *SeverityRules {
func NewSeverityRules(defaultSeverity string, excludedLinters []string, rules []SeverityRule,
files *fsutils.Files, log logutils.Log,
) *SeverityRules {
r := &SeverityRules{
defaultSeverity: defaultSeverity,
excludedLinters: excludedLinters,
files: files,
log: log,
defaultSeverity: defaultSeverity,
}

r.rules = createSeverityRules(rules, "(?i)")

return r
Expand Down Expand Up @@ -61,11 +67,15 @@ func createSeverityRules(rules []SeverityRule, prefix string) []severityRule {
return parsedRules
}

func (p SeverityRules) Process(issues []result.Issue) ([]result.Issue, error) {
func (p *SeverityRules) Process(issues []result.Issue) ([]result.Issue, error) {
if len(p.rules) == 0 && p.defaultSeverity == "" {
return issues, nil
}
return transformIssues(issues, func(i *result.Issue) *result.Issue {
if slices.Contains(p.excludedLinters, i.FromLinter) {
return i
}

for _, rule := range p.rules {
rule := rule

Expand All @@ -87,19 +97,22 @@ func (p SeverityRules) Process(issues []result.Issue) ([]result.Issue, error) {
func (SeverityRules) Name() string { return "severity-rules" }
func (SeverityRules) Finish() {}

var _ Processor = SeverityRules{}
var _ Processor = &SeverityRules{}

type SeverityRulesCaseSensitive struct {
*SeverityRules
}

func NewSeverityRulesCaseSensitive(defaultSeverity string, rules []SeverityRule,
files *fsutils.Files, log logutils.Log) *SeverityRulesCaseSensitive {
func NewSeverityRulesCaseSensitive(defaultSeverity string, excludedLinters []string, rules []SeverityRule,
files *fsutils.Files, log logutils.Log,
) *SeverityRulesCaseSensitive {
r := &SeverityRules{
defaultSeverity: defaultSeverity,
excludedLinters: excludedLinters,
files: files,
log: log,
defaultSeverity: defaultSeverity,
}

r.rules = createSeverityRules(rules, "")

return &SeverityRulesCaseSensitive{r}
Expand Down
12 changes: 6 additions & 6 deletions pkg/result/processors/severity_rules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestSeverityRulesMultiple(t *testing.T) {
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
files := fsutils.NewFiles(lineCache, "")
log := report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
p := NewSeverityRules("error", []SeverityRule{
p := NewSeverityRules("error", []string{}, []SeverityRule{
{
Severity: "info",
BaseRule: BaseRule{
Expand Down Expand Up @@ -122,7 +122,7 @@ func TestSeverityRulesPathPrefix(t *testing.T) {
pathPrefix := path.Join("some", "dir")
files := fsutils.NewFiles(lineCache, pathPrefix)
log := report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
p := NewSeverityRules("error", []SeverityRule{
p := NewSeverityRules("error", []string{}, []SeverityRule{
{
Severity: "info",
BaseRule: BaseRule{
Expand Down Expand Up @@ -159,7 +159,7 @@ func TestSeverityRulesPathPrefix(t *testing.T) {
}

func TestSeverityRulesText(t *testing.T) {
p := NewSeverityRules("", []SeverityRule{
p := NewSeverityRules("", []string{}, []SeverityRule{
{
BaseRule: BaseRule{
Text: "^severity$",
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestSeverityRulesOnlyDefault(t *testing.T) {
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
files := fsutils.NewFiles(lineCache, "")
log := report.NewLogWrapper(logutils.NewStderrLog(logutils.DebugKeyEmpty), &report.Data{})
p := NewSeverityRules("info", []SeverityRule{}, files, log)
p := NewSeverityRules("info", []string{}, []SeverityRule{}, files, log)

cases := []issueTestCase{
{Path: "ssl.go", Text: "ssl", Linter: "gosec"},
Expand Down Expand Up @@ -219,13 +219,13 @@ func TestSeverityRulesOnlyDefault(t *testing.T) {
}

func TestSeverityRulesEmpty(t *testing.T) {
processAssertSame(t, NewSeverityRules("", nil, nil, nil), newIssueFromTextTestCase("test"))
processAssertSame(t, NewSeverityRules("", []string{}, nil, nil, nil), newIssueFromTextTestCase("test"))
}

func TestSeverityRulesCaseSensitive(t *testing.T) {
lineCache := fsutils.NewLineCache(fsutils.NewFileCache())
files := fsutils.NewFiles(lineCache, "")
p := NewSeverityRulesCaseSensitive("error", []SeverityRule{
p := NewSeverityRulesCaseSensitive("error", []string{}, []SeverityRule{
{
Severity: "info",
BaseRule: BaseRule{
Expand Down