Skip to content

Commit 3faf3af

Browse files
committed
review
1 parent f85fdaf commit 3faf3af

File tree

5 files changed

+47
-29
lines changed

5 files changed

+47
-29
lines changed

pkg/config/linters_exclusions.go

+19-5
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,22 @@ package config
22

33
import (
44
"fmt"
5+
"slices"
56
)
67

78
const (
8-
DefaultExclusionComments = "comments"
9-
DefaultExclusionStdErrorHandling = "stdErrorHandling"
10-
DefaultExclusionCommonFalsePositives = "commonFalsePositives"
11-
DefaultExclusionLegacy = "legacy"
9+
ExclusionPresetComments = "comments"
10+
ExclusionPresetStdErrorHandling = "stdErrorHandling"
11+
ExclusionPresetCommonFalsePositives = "commonFalsePositives"
12+
ExclusionPresetLegacy = "legacy"
1213
)
1314

1415
const excludeRuleMinConditionsCount = 2
1516

1617
type LinterExclusions struct {
1718
Generated string `mapstructure:"generated"`
1819
WarnUnused bool `mapstructure:"warn-unused"`
19-
Default []string `mapstructure:"default"`
20+
Presets []string `mapstructure:"preset"`
2021
Rules []ExcludeRule `mapstructure:"rules"`
2122
Paths []string `mapstructure:"paths"`
2223
PathsExcept []string `mapstructure:"paths-except"`
@@ -29,6 +30,19 @@ func (e *LinterExclusions) Validate() error {
2930
}
3031
}
3132

33+
allPresets := []string{
34+
ExclusionPresetComments,
35+
ExclusionPresetStdErrorHandling,
36+
ExclusionPresetCommonFalsePositives,
37+
ExclusionPresetLegacy,
38+
}
39+
40+
for _, preset := range e.Presets {
41+
if !slices.Contains(allPresets, preset) {
42+
return fmt.Errorf("invalid preset: %s", preset)
43+
}
44+
}
45+
3246
return nil
3347
}
3448

pkg/config/loader.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ func (l *Loader) Load(opts LoadOptions) error {
7373
l.cfg.Linters.LinterExclusions.Generated = cmp.Or(l.cfg.Issues.ExcludeGenerated, "strict")
7474
}
7575

76+
// Compatibility layer with v1.
77+
// TODO(ldez): should be removed in v2.
7678
if l.cfg.Issues.UseDefaultExcludes {
77-
l.cfg.Linters.LinterExclusions.Default = []string{
78-
DefaultExclusionComments,
79-
DefaultExclusionStdErrorHandling,
80-
DefaultExclusionCommonFalsePositives,
81-
DefaultExclusionLegacy,
79+
l.cfg.Linters.LinterExclusions.Presets = []string{
80+
ExclusionPresetComments,
81+
ExclusionPresetStdErrorHandling,
82+
ExclusionPresetCommonFalsePositives,
83+
ExclusionPresetLegacy,
8284
}
8385
}
8486

pkg/result/processors/exclusion_default.go pkg/result/processors/exclusion_presets.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package processors
22

33
import "github.com/golangci/golangci-lint/pkg/config"
44

5-
var defaultLintersExclusions = map[string][]config.ExcludeRule{
6-
config.DefaultExclusionComments: {
5+
var linterExclusionPresets = map[string][]config.ExcludeRule{
6+
config.ExclusionPresetComments: {
77
{
88
// Annoying issue about not having a comment. The rare codebase has such comments.
99
// CheckPackageComment, CheckExportedFunctionDocs, CheckExportedTypeDocs, CheckExportedVarDocs
@@ -50,7 +50,7 @@ var defaultLintersExclusions = map[string][]config.ExcludeRule{
5050
},
5151
},
5252
},
53-
config.DefaultExclusionStdErrorHandling: {
53+
config.ExclusionPresetStdErrorHandling: {
5454
{
5555
// Almost all programs ignore errors on these functions and in most cases it's ok.
5656
BaseRule: config.BaseRule{
@@ -61,7 +61,7 @@ var defaultLintersExclusions = map[string][]config.ExcludeRule{
6161
},
6262
},
6363
},
64-
config.DefaultExclusionCommonFalsePositives: {
64+
config.ExclusionPresetCommonFalsePositives: {
6565
{
6666
// Too many false-positives on 'unsafe' usage.
6767
BaseRule: config.BaseRule{
@@ -87,7 +87,7 @@ var defaultLintersExclusions = map[string][]config.ExcludeRule{
8787
},
8888
},
8989
},
90-
config.DefaultExclusionLegacy: {
90+
config.ExclusionPresetLegacy: {
9191
{
9292
// Common false positives.
9393
BaseRule: config.BaseRule{
@@ -125,11 +125,13 @@ var defaultLintersExclusions = map[string][]config.ExcludeRule{
125125
},
126126
}
127127

128-
func getDefaultLintersExclusions(names []string) []config.ExcludeRule {
128+
func getLinterExclusionPresets(names []string) []config.ExcludeRule {
129129
var rules []config.ExcludeRule
130130

131131
for _, name := range names {
132-
rules = append(rules, defaultLintersExclusions[name]...)
132+
if p, ok := linterExclusionPresets[name]; ok {
133+
rules = append(rules, p...)
134+
}
133135
}
134136

135137
return rules

pkg/result/processors/exclusion_rules.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func NewExclusionRules(log logutils.Log, files *fsutils.Files, cfg *config.Linte
3939
}
4040

4141
excludeRules := slices.Concat(slices.Clone(cfg.Rules),
42-
filterInclude(getDefaultLintersExclusions(cfg.Default), oldCfg.IncludeDefaultExcludes))
42+
filterInclude(getLinterExclusionPresets(cfg.Presets), oldCfg.IncludeDefaultExcludes))
4343

4444
p.rules = createExcludeRules(excludeRules, prefix)
4545

pkg/result/processors/path_relativity.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var _ Processor = (*PathRelativity)(nil)
1414
// PathRelativity computes [result.Issue.RelativePath] and [result.Issue.WorkingDirectoryRelativePath],
1515
// based on the base path.
1616
type PathRelativity struct {
17-
log logutils.Log
18-
basePath string
19-
wd string
17+
log logutils.Log
18+
basePath string
19+
workingDirectory string
2020
}
2121

2222
func NewPathRelativity(log logutils.Log, basePath string) (*PathRelativity, error) {
@@ -26,12 +26,16 @@ func NewPathRelativity(log logutils.Log, basePath string) (*PathRelativity, erro
2626
}
2727

2828
return &PathRelativity{
29-
log: log.Child(logutils.DebugKeyPathRelativity),
30-
basePath: basePath,
31-
wd: wd,
29+
log: log.Child(logutils.DebugKeyPathRelativity),
30+
basePath: basePath,
31+
workingDirectory: wd,
3232
}, nil
3333
}
3434

35+
func (*PathRelativity) Name() string {
36+
return "path_relativity"
37+
}
38+
3539
func (p *PathRelativity) Process(issues []result.Issue) ([]result.Issue, error) {
3640
return transformIssues(issues, func(issue *result.Issue) *result.Issue {
3741
newIssue := *issue
@@ -43,7 +47,7 @@ func (p *PathRelativity) Process(issues []result.Issue) ([]result.Issue, error)
4347
return nil
4448
}
4549

46-
newIssue.WorkingDirectoryRelativePath, err = filepath.Rel(p.wd, issue.FilePath())
50+
newIssue.WorkingDirectoryRelativePath, err = filepath.Rel(p.workingDirectory, issue.FilePath())
4751
if err != nil {
4852
p.log.Warnf("Getting relative path (wd): %v", err)
4953
return nil
@@ -53,8 +57,4 @@ func (p *PathRelativity) Process(issues []result.Issue) ([]result.Issue, error)
5357
}), nil
5458
}
5559

56-
func (*PathRelativity) Name() string {
57-
return "path_relativity"
58-
}
59-
6060
func (*PathRelativity) Finish() {}

0 commit comments

Comments
 (0)