Skip to content

feat: add reassign linter #3064

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 11 commits into from
Aug 23, 2022
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
9 changes: 9 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,13 @@ linters-settings:
- CamelCase
- UnitAbbreviations

reassign:
# Patterns for global variable names that are checked for reassignment.
# See https://github.com/curioswitch/go-reassign#usage
# Default: ["EOF", "Err.*"]
patterns:
- ".*"

revive:
# Maximum number of open files at the same time.
# See https://github.com/mgechev/revive#command-line-flags
Expand Down Expand Up @@ -1951,6 +1958,7 @@ linters:
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- scopelint
Expand Down Expand Up @@ -2055,6 +2063,7 @@ linters:
- prealloc
- predeclared
- promlinter
- reassign
- revive
- rowserrcheck
- scopelint
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/breml/errchkjson v0.3.0
github.com/butuzov/ireturn v0.1.1
github.com/charithe/durationcheck v0.0.9
github.com/curioswitch/go-reassign v0.1.2
github.com/daixiang0/gci v0.6.3
github.com/denis-tingaikin/go-header v0.4.3
github.com/esimonov/ifshort v1.0.4
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ type LintersSettings struct {
Prealloc PreallocSettings
Predeclared PredeclaredSettings
Promlinter PromlinterSettings
Reassign ReassignSettings
Revive ReviveSettings
RowsErrCheck RowsErrCheckSettings
Staticcheck StaticCheckSettings
Expand Down Expand Up @@ -532,6 +533,10 @@ type PromlinterSettings struct {
DisabledLinters []string `mapstructure:"disabled-linters"`
}

type ReassignSettings struct {
Patterns []string `mapstructure:"patterns"`
}

type ReviveSettings struct {
MaxOpenFiles int `mapstructure:"max-open-files"`
IgnoreGeneratedHeader bool `mapstructure:"ignore-generated-header"`
Expand Down
32 changes: 32 additions & 0 deletions pkg/golinters/reassign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package golinters

import (
"fmt"
"strings"

"github.com/curioswitch/go-reassign"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
)

func NewReassign(settings *config.ReassignSettings) *goanalysis.Linter {
a := reassign.NewAnalyzer()

var cfg map[string]map[string]interface{}
if settings != nil && len(settings.Patterns) > 0 {
cfg = map[string]map[string]interface{}{
a.Name: {
reassign.FlagPattern: fmt.Sprintf("^(%s)$", strings.Join(settings.Patterns, "|")),
},
}
}

return goanalysis.NewLinter(
a.Name,
a.Doc,
[]*analysis.Analyzer{a},
cfg,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
7 changes: 7 additions & 0 deletions pkg/lint/lintersdb/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
preallocCfg *config.PreallocSettings
predeclaredCfg *config.PredeclaredSettings
promlinterCfg *config.PromlinterSettings
reassignCfg *config.ReassignSettings
reviveCfg *config.ReviveSettings
rowserrcheckCfg *config.RowsErrCheckSettings
staticcheckCfg *config.StaticCheckSettings
Expand Down Expand Up @@ -227,6 +228,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
parallelTestCfg = &m.cfg.LintersSettings.ParallelTest
predeclaredCfg = &m.cfg.LintersSettings.Predeclared
promlinterCfg = &m.cfg.LintersSettings.Promlinter
reassignCfg = &m.cfg.LintersSettings.Reassign
reviveCfg = &m.cfg.LintersSettings.Revive
rowserrcheckCfg = &m.cfg.LintersSettings.RowsErrCheck
staticcheckCfg = &m.cfg.LintersSettings.Staticcheck
Expand Down Expand Up @@ -683,6 +685,11 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithURL("https://github.com/yeya24/promlinter"),

linter.NewConfig(golinters.NewReassign(reassignCfg)).
WithSince("1.49.0").
WithPresets(linter.PresetBugs).
WithURL("https://github.com/curioswitch/go-reassign"),

linter.NewConfig(golinters.NewRevive(reviveCfg)).
WithSince("v1.37.0").
WithPresets(linter.PresetStyle, linter.PresetMetaLinter).
Expand Down
5 changes: 5 additions & 0 deletions test/testdata/configs/reassign_patterns.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters-settings:
reassign:
patterns:
- DefaultClient
- DefaultTransport
13 changes: 13 additions & 0 deletions test/testdata/reassign.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//golangcitest:args -Ereassign
package testdata

import (
"io"
"net/http"
)

func reassignTest() {
http.DefaultClient = nil
http.DefaultTransport = nil
io.EOF = nil // want `reassigning variable EOF in other package io`
}
14 changes: 14 additions & 0 deletions test/testdata/reassign_patterns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//golangcitest:args -Ereassign
//golangcitest:config_path testdata/configs/reassign_patterns.yml
package testdata

import (
"io"
"net/http"
)

func reassignTestPatterns() {
http.DefaultClient = nil // want `reassigning variable DefaultClient in other package http`
http.DefaultTransport = nil // want `reassigning variable DefaultTransport in other package http`
io.EOF = nil
}