Skip to content

Commit a03530a

Browse files
committed
feat: add warn-unused for fmt command
1 parent a395a3e commit a03530a

File tree

4 files changed

+29
-6
lines changed

4 files changed

+29
-6
lines changed

.golangci.next.reference.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4016,6 +4016,9 @@ formatters:
40164016
chain-split-dots: false
40174017

40184018
exclusions:
4019+
# Log a warning if an exclusion path is unused.
4020+
# Default: false
4021+
warn-unused: true
40194022
# Mode of the generated files analysis.
40204023
#
40214024
# - `strict`: sources are excluded by strictly following the Go generated file convention.

jsonschema/golangci.next.jsonschema.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4624,6 +4624,10 @@
46244624
"items": {
46254625
"type": "string"
46264626
}
4627+
},
4628+
"warn-unused": {
4629+
"type": "boolean",
4630+
"default": false
46274631
}
46284632
}
46294633
}

pkg/config/formatters.go

Lines changed: 3 additions & 2 deletions
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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ func (c *Runner) Run(paths []string) error {
6666
}
6767
}
6868

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

@@ -169,6 +177,9 @@ type RunnerOptions struct {
169177
generated string
170178
diff bool
171179
stdin bool
180+
181+
warnUnused bool
182+
excludedPathCounter map[*regexp.Regexp]int
172183
}
173184

174185
func NewRunnerOptions(cfg *config.Config, diff, stdin bool) (RunnerOptions, error) {
@@ -178,10 +189,12 @@ func NewRunnerOptions(cfg *config.Config, diff, stdin bool) (RunnerOptions, erro
178189
}
179190

180191
opts := RunnerOptions{
181-
basePath: basePath,
182-
generated: cfg.Formatters.Exclusions.Generated,
183-
diff: diff,
184-
stdin: stdin,
192+
basePath: basePath,
193+
generated: cfg.Formatters.Exclusions.Generated,
194+
diff: diff,
195+
stdin: stdin,
196+
excludedPathCounter: make(map[*regexp.Regexp]int),
197+
warnUnused: cfg.Formatters.Exclusions.WarnUnused,
185198
}
186199

187200
for _, pattern := range cfg.Formatters.Exclusions.Paths {
@@ -191,6 +204,7 @@ func NewRunnerOptions(cfg *config.Config, diff, stdin bool) (RunnerOptions, erro
191204
}
192205

193206
opts.patterns = append(opts.patterns, exp)
207+
opts.excludedPathCounter[exp] = 0
194208
}
195209

196210
return opts, nil
@@ -208,6 +222,7 @@ func (o RunnerOptions) MatchAnyPattern(path string) (bool, error) {
208222

209223
for _, pattern := range o.patterns {
210224
if pattern.MatchString(rel) {
225+
o.excludedPathCounter[pattern]++
211226
return true, nil
212227
}
213228
}

0 commit comments

Comments
 (0)