Skip to content

Commit 0450995

Browse files
authored
feat: add warn-unused option for fmt command (#5668)
1 parent 835bf75 commit 0450995

File tree

5 files changed

+32
-9
lines changed

5 files changed

+32
-9
lines changed

.golangci.next.reference.yml

+3
Original file line numberDiff line numberDiff line change
@@ -4022,6 +4022,9 @@ formatters:
40224022
chain-split-dots: false
40234023

40244024
exclusions:
4025+
# Log a warning if an exclusion path is unused.
4026+
# Default: false
4027+
warn-unused: true
40254028
# Mode of the generated files analysis.
40264029
#
40274030
# - `strict`: sources are excluded by strictly following the Go generated file convention.

jsonschema/golangci.next.jsonschema.json

+4
Original file line numberDiff line numberDiff line change
@@ -4634,6 +4634,10 @@
46344634
"items": {
46354635
"type": "string"
46364636
}
4637+
},
4638+
"warn-unused": {
4639+
"type": "boolean",
4640+
"default": false
46374641
}
46384642
}
46394643
}

pkg/config/formatters.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func (f *Formatters) Validate() error {
2222
}
2323

2424
type FormatterExclusions struct {
25-
Generated string `mapstructure:"generated"`
26-
Paths []string `mapstructure:"paths"`
25+
Generated string `mapstructure:"generated"`
26+
Paths []string `mapstructure:"paths"`
27+
WarnUnused bool `mapstructure:"warn-unused"`
2728
}

pkg/goformat/runner.go

+20-5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ func (c *Runner) Run(paths []string) error {
6767
}
6868
}
6969

70+
for pattern, count := range c.opts.excludedPathCounter {
71+
if c.opts.warnUnused && count == 0 {
72+
c.log.Warnf("The pattern %q match no issues", pattern)
73+
} else {
74+
c.log.Infof("Skipped %d issues by pattern %q", count, pattern)
75+
}
76+
}
77+
7078
return nil
7179
}
7280

@@ -181,6 +189,9 @@ type RunnerOptions struct {
181189
diff bool
182190
colors bool
183191
stdin bool
192+
193+
warnUnused bool
194+
excludedPathCounter map[*regexp.Regexp]int
184195
}
185196

186197
func NewRunnerOptions(cfg *config.Config, diff, diffColored, stdin bool) (RunnerOptions, error) {
@@ -190,11 +201,13 @@ func NewRunnerOptions(cfg *config.Config, diff, diffColored, stdin bool) (Runner
190201
}
191202

192203
opts := RunnerOptions{
193-
basePath: basePath,
194-
generated: cfg.Formatters.Exclusions.Generated,
195-
diff: diff || diffColored,
196-
colors: diffColored,
197-
stdin: stdin,
204+
basePath: basePath,
205+
generated: cfg.Formatters.Exclusions.Generated,
206+
diff: diff || diffColored,
207+
colors: diffColored,
208+
stdin: stdin,
209+
excludedPathCounter: make(map[*regexp.Regexp]int),
210+
warnUnused: cfg.Formatters.Exclusions.WarnUnused,
198211
}
199212

200213
for _, pattern := range cfg.Formatters.Exclusions.Paths {
@@ -204,6 +217,7 @@ func NewRunnerOptions(cfg *config.Config, diff, diffColored, stdin bool) (Runner
204217
}
205218

206219
opts.patterns = append(opts.patterns, exp)
220+
opts.excludedPathCounter[exp] = 0
207221
}
208222

209223
return opts, nil
@@ -221,6 +235,7 @@ func (o RunnerOptions) MatchAnyPattern(path string) (bool, error) {
221235

222236
for _, pattern := range o.patterns {
223237
if pattern.MatchString(rel) {
238+
o.excludedPathCounter[pattern]++
224239
return true, nil
225240
}
226241
}

pkg/result/processors/exclusion_paths.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ func (p *ExclusionPaths) Process(issues []result.Issue) ([]result.Issue, error)
7979
func (p *ExclusionPaths) Finish() {
8080
for pattern, count := range p.excludedPathCounter {
8181
if p.warnUnused && count == 0 {
82-
p.log.Warnf("The pattern %q match %d issues", pattern, count)
82+
p.log.Warnf("The pattern %q match no issues", pattern)
8383
} else {
8484
p.log.Infof("Skipped %d issues by pattern %q", count, pattern)
8585
}
8686
}
8787

8888
for pattern, count := range p.excludedPathExceptCounter {
8989
if p.warnUnused && count == 0 {
90-
p.log.Warnf("The pattern %q match %d issues", pattern, count)
90+
p.log.Warnf("The pattern %q match no issues", pattern)
9191
}
9292
}
9393
}

0 commit comments

Comments
 (0)