Skip to content

feat: add warn-unused option for fmt command #5668

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

Merged
merged 2 commits into from
Apr 4, 2025
Merged
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
3 changes: 3 additions & 0 deletions .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4022,6 +4022,9 @@ formatters:
chain-split-dots: false

exclusions:
# Log a warning if an exclusion path is unused.
# Default: false
warn-unused: true
# Mode of the generated files analysis.
#
# - `strict`: sources are excluded by strictly following the Go generated file convention.
Expand Down
4 changes: 4 additions & 0 deletions jsonschema/golangci.next.jsonschema.json
Original file line number Diff line number Diff line change
Expand Up @@ -4634,6 +4634,10 @@
"items": {
"type": "string"
}
},
"warn-unused": {
"type": "boolean",
"default": false
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/config/formatters.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (f *Formatters) Validate() error {
}

type FormatterExclusions struct {
Generated string `mapstructure:"generated"`
Paths []string `mapstructure:"paths"`
Generated string `mapstructure:"generated"`
Paths []string `mapstructure:"paths"`
WarnUnused bool `mapstructure:"warn-unused"`
}
25 changes: 20 additions & 5 deletions pkg/goformat/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ func (c *Runner) Run(paths []string) error {
}
}

for pattern, count := range c.opts.excludedPathCounter {
if c.opts.warnUnused && count == 0 {
c.log.Warnf("The pattern %q match no issues", pattern)
} else {
c.log.Infof("Skipped %d issues by pattern %q", count, pattern)
}
}

return nil
}

Expand Down Expand Up @@ -181,6 +189,9 @@ type RunnerOptions struct {
diff bool
colors bool
stdin bool

warnUnused bool
excludedPathCounter map[*regexp.Regexp]int
}

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

opts := RunnerOptions{
basePath: basePath,
generated: cfg.Formatters.Exclusions.Generated,
diff: diff || diffColored,
colors: diffColored,
stdin: stdin,
basePath: basePath,
generated: cfg.Formatters.Exclusions.Generated,
diff: diff || diffColored,
colors: diffColored,
stdin: stdin,
excludedPathCounter: make(map[*regexp.Regexp]int),
warnUnused: cfg.Formatters.Exclusions.WarnUnused,
}

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

opts.patterns = append(opts.patterns, exp)
opts.excludedPathCounter[exp] = 0
}

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

for _, pattern := range o.patterns {
if pattern.MatchString(rel) {
o.excludedPathCounter[pattern]++
return true, nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/result/processors/exclusion_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ func (p *ExclusionPaths) Process(issues []result.Issue) ([]result.Issue, error)
func (p *ExclusionPaths) Finish() {
for pattern, count := range p.excludedPathCounter {
if p.warnUnused && count == 0 {
p.log.Warnf("The pattern %q match %d issues", pattern, count)
p.log.Warnf("The pattern %q match no issues", pattern)
} else {
p.log.Infof("Skipped %d issues by pattern %q", count, pattern)
}
}

for pattern, count := range p.excludedPathExceptCounter {
if p.warnUnused && count == 0 {
p.log.Warnf("The pattern %q match %d issues", pattern, count)
p.log.Warnf("The pattern %q match no issues", pattern)
}
}
}
Expand Down
Loading