Skip to content

Commit d9a1bdb

Browse files
lopezatorjirfag
authored andcommitted
gocritic: fix code to pass newly added gocritic checks
Fix code to pass newly added gocritic checks, mainly pointer receivers and import shadows
1 parent 21a8185 commit d9a1bdb

File tree

17 files changed

+78
-76
lines changed

17 files changed

+78
-76
lines changed

pkg/commands/config.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func (e *Executor) initConfig() {
3434

3535
}
3636

37-
func (e Executor) executePathCmd(cmd *cobra.Command, args []string) {
37+
func (e *Executor) executePathCmd(cmd *cobra.Command, args []string) {
3838
usedConfigFile := viper.ConfigFileUsed()
3939
if usedConfigFile == "" {
4040
e.log.Warnf("No config file detected")

pkg/commands/executor.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,6 @@ func NewExecutor(version, commit, date string) *Executor {
8282
return e
8383
}
8484

85-
func (e Executor) Execute() error {
85+
func (e *Executor) Execute() error {
8686
return e.rootCmd.Execute()
8787
}

pkg/commands/help.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ func (e *Executor) initHelp() {
3232
helpCmd.AddCommand(lintersHelpCmd)
3333
}
3434

35-
func printLinterConfigs(lcs []linter.Config) {
35+
func printLinterConfigs(lcs []*linter.Config) {
3636
for _, lc := range lcs {
3737
altNamesStr := ""
3838
if len(lc.AlternativeNames) != 0 {
@@ -43,12 +43,12 @@ func printLinterConfigs(lcs []linter.Config) {
4343
}
4444
}
4545

46-
func (e Executor) executeLintersHelp(cmd *cobra.Command, args []string) {
46+
func (e *Executor) executeLintersHelp(cmd *cobra.Command, args []string) {
4747
if len(args) != 0 {
4848
e.log.Fatalf("Usage: golangci-lint help linters")
4949
}
5050

51-
var enabledLCs, disabledLCs []linter.Config
51+
var enabledLCs, disabledLCs []*linter.Config
5252
for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() {
5353
if lc.EnabledByDefault {
5454
enabledLCs = append(enabledLCs, lc)

pkg/commands/linters.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (e *Executor) initLinters() {
2020
e.initRunConfiguration(lintersCmd)
2121
}
2222

23-
func IsLinterInConfigsList(name string, linters []linter.Config) bool {
23+
func IsLinterInConfigsList(name string, linters []*linter.Config) bool {
2424
for _, lc := range linters {
2525
if lc.Name() == name {
2626
return true
@@ -43,7 +43,7 @@ func (e *Executor) executeLinters(cmd *cobra.Command, args []string) {
4343
color.Green("Enabled by your configuration linters:\n")
4444
printLinterConfigs(enabledLCs)
4545

46-
var disabledLCs []linter.Config
46+
var disabledLCs []*linter.Config
4747
for _, lc := range e.DBManager.GetAllSupportedLinterConfigs() {
4848
if !IsLinterInConfigsList(lc.Name(), enabledLCs) {
4949
disabledLCs = append(disabledLCs, lc)

pkg/commands/run.go

+10-8
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ import (
2727
func getDefaultExcludeHelp() string {
2828
parts := []string{"Use or not use default excludes:"}
2929
for _, ep := range config.DefaultExcludePatterns {
30-
parts = append(parts, fmt.Sprintf(" # %s: %s", ep.Linter, ep.Why))
31-
parts = append(parts, fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)))
32-
parts = append(parts, "")
30+
parts = append(parts,
31+
fmt.Sprintf(" # %s: %s", ep.Linter, ep.Why),
32+
fmt.Sprintf(" - %s", color.YellowString(ep.Pattern)),
33+
"",
34+
)
3335
}
3436
return strings.Join(parts, "\n")
3537
}
@@ -184,7 +186,7 @@ func (e *Executor) initRunConfiguration(cmd *cobra.Command) {
184186
initFlagSet(fs, e.cfg, e.DBManager, true)
185187
}
186188

187-
func (e Executor) getConfigForCommandLine() (*config.Config, error) {
189+
func (e *Executor) getConfigForCommandLine() (*config.Config, error) {
188190
// We use another pflag.FlagSet here to not set `changed` flag
189191
// on cmd.Flags() options. Otherwise string slice options will be duplicated.
190192
fs := pflag.NewFlagSet("config flag set", pflag.ContinueOnError)
@@ -412,10 +414,10 @@ func (e *Executor) setupExitCode(ctx context.Context) {
412414
}
413415
}
414416

415-
func watchResources(ctx context.Context, done chan struct{}, log logutils.Log) {
417+
func watchResources(ctx context.Context, done chan struct{}, logger logutils.Log) {
416418
startedAt := time.Now()
417419

418-
rssValues := []uint64{}
420+
var rssValues []uint64
419421
ticker := time.NewTicker(100 * time.Millisecond)
420422
defer ticker.Stop()
421423

@@ -448,8 +450,8 @@ func watchResources(ctx context.Context, done chan struct{}, log logutils.Log) {
448450

449451
const MB = 1024 * 1024
450452
maxMB := float64(max) / MB
451-
log.Infof("Memory: %d samples, avg is %.1fMB, max is %.1fMB",
453+
logger.Infof("Memory: %d samples, avg is %.1fMB, max is %.1fMB",
452454
len(rssValues), float64(avg)/MB, maxMB)
453-
log.Infof("Execution took %s", time.Since(startedAt))
455+
logger.Infof("Execution took %s", time.Since(startedAt))
454456
close(done)
455457
}

pkg/fsutils/fsutils.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ func EvalSymlinks(path string) (string, error) {
6565
return er.path, er.err
6666
}
6767

68-
func ShortestRelPath(path string, wd string) (string, error) {
68+
func ShortestRelPath(path, wd string) (string, error) {
6969
if wd == "" { // get it if user don't have cached working dir
7070
var err error
7171
wd, err = Getwd()

pkg/golinters/gofmt.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
gofmtAPI "github.com/golangci/gofmt/gofmt"
1010
goimportsAPI "github.com/golangci/gofmt/goimports"
1111
"golang.org/x/tools/imports"
12-
"sourcegraph.com/sourcegraph/go-diff/diff"
12+
diffpkg "sourcegraph.com/sourcegraph/go-diff/diff"
1313

1414
"github.com/golangci/golangci-lint/pkg/lint/linter"
1515
"github.com/golangci/golangci-lint/pkg/logutils"
@@ -37,7 +37,7 @@ func (g Gofmt) Desc() string {
3737
"this tool runs with -s option to check for code simplification"
3838
}
3939

40-
func getFirstDeletedAndAddedLineNumberInHunk(h *diff.Hunk) (int, int, error) {
40+
func getFirstDeletedAndAddedLineNumberInHunk(h *diffpkg.Hunk) (int, int, error) {
4141
lines := bytes.Split(h.Body, []byte{'\n'})
4242
lineNumber := int(h.OrigStartLine - 1)
4343
firstAddedLineNumber := -1
@@ -59,7 +59,7 @@ func getFirstDeletedAndAddedLineNumberInHunk(h *diff.Hunk) (int, int, error) {
5959
}
6060

6161
func (g Gofmt) extractIssuesFromPatch(patch string, log logutils.Log) ([]result.Issue, error) {
62-
diffs, err := diff.ParseMultiFileDiff([]byte(patch))
62+
diffs, err := diffpkg.ParseMultiFileDiff([]byte(patch))
6363
if err != nil {
6464
return nil, fmt.Errorf("can't parse patch: %s", err)
6565
}

pkg/golinters/golint.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,11 @@ func (g Golint) lintPkg(minConfidence float64, files []*ast.File, fset *token.Fi
5757
}
5858

5959
issues := make([]result.Issue, 0, len(ps)) // This is worst case
60-
for _, p := range ps {
61-
if p.Confidence >= minConfidence {
60+
for idx := range ps {
61+
if ps[idx].Confidence >= minConfidence {
6262
issues = append(issues, result.Issue{
63-
Pos: p.Position,
64-
Text: markIdentifiers(p.Text),
63+
Pos: ps[idx].Position,
64+
Text: markIdentifiers(ps[idx].Text),
6565
FromLinter: g.Name(),
6666
})
6767
// TODO: use p.Link and p.Category

pkg/golinters/megacheck.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (m Megacheck) canAnalyze(lintCtx *linter.Context) bool {
8888
}
8989

9090
var errPkgs []string
91-
var errors []packages.Error
91+
var errs []packages.Error
9292
for _, p := range lintCtx.NotCompilingPackages {
9393
if p.Name == "main" {
9494
// megacheck crashes on not compiling packages but main packages
@@ -97,19 +97,19 @@ func (m Megacheck) canAnalyze(lintCtx *linter.Context) bool {
9797
}
9898

9999
errPkgs = append(errPkgs, p.String())
100-
errors = append(errors, libpackages.ExtractErrors(p, lintCtx.ASTCache)...)
100+
errs = append(errs, libpackages.ExtractErrors(p, lintCtx.ASTCache)...)
101101
}
102102

103103
if len(errPkgs) == 0 { // only main packages do not compile
104104
return true
105105
}
106106

107107
warnText := fmt.Sprintf("Can't run megacheck because of compilation errors in packages %s", errPkgs)
108-
if len(errors) != 0 {
109-
warnText += fmt.Sprintf(": %s", prettifyCompilationError(errors[0]))
110-
if len(errors) > 1 {
108+
if len(errs) != 0 {
109+
warnText += fmt.Sprintf(": %s", prettifyCompilationError(errs[0]))
110+
if len(errs) > 1 {
111111
const runCmd = "golangci-lint run --no-config --disable-all -E typecheck"
112-
warnText += fmt.Sprintf(" and %d more errors: run `%s` to see all errors", len(errors)-1, runCmd)
112+
warnText += fmt.Sprintf(" and %d more errors: run `%s` to see all errors", len(errs)-1, runCmd)
113113
}
114114
}
115115
lintCtx.Log.Warnf("%s", warnText)

pkg/lint/linter/config.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,46 @@ type Config struct {
2323
OriginalURL string // URL of original (not forked) repo, needed for autogenerated README
2424
}
2525

26-
func (lc Config) WithTypeInfo() Config {
26+
func (lc *Config) WithTypeInfo() *Config {
2727
lc.NeedsTypeInfo = true
2828
return lc
2929
}
3030

31-
func (lc Config) WithSSA() Config {
31+
func (lc *Config) WithSSA() *Config {
3232
lc.NeedsTypeInfo = true
3333
lc.NeedsSSARepr = true
3434
return lc
3535
}
3636

37-
func (lc Config) WithPresets(presets ...string) Config {
37+
func (lc *Config) WithPresets(presets ...string) *Config {
3838
lc.InPresets = presets
3939
return lc
4040
}
4141

42-
func (lc Config) WithSpeed(speed int) Config {
42+
func (lc *Config) WithSpeed(speed int) *Config {
4343
lc.Speed = speed
4444
return lc
4545
}
4646

47-
func (lc Config) WithURL(url string) Config {
47+
func (lc *Config) WithURL(url string) *Config {
4848
lc.OriginalURL = url
4949
return lc
5050
}
5151

52-
func (lc Config) WithAlternativeNames(names ...string) Config {
52+
func (lc *Config) WithAlternativeNames(names ...string) *Config {
5353
lc.AlternativeNames = names
5454
return lc
5555
}
5656

57-
func (lc Config) GetSpeed() int {
57+
func (lc *Config) GetSpeed() int {
5858
return lc.Speed
5959
}
6060

61-
func (lc Config) AllNames() []string {
61+
func (lc *Config) AllNames() []string {
6262
return append([]string{lc.Name()}, lc.AlternativeNames...)
6363
}
6464

65-
func (lc Config) Name() string {
65+
func (lc *Config) Name() string {
6666
return lc.Linter.Name()
6767
}
6868

pkg/lint/lintersdb/enabled_set.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func NewEnabledSet(m *Manager, v *Validator, log logutils.Log, cfg *config.Confi
2626
}
2727

2828
// nolint:gocyclo
29-
func (es EnabledSet) build(lcfg *config.Linters, enabledByDefaultLinters []linter.Config) map[string]*linter.Config {
29+
func (es EnabledSet) build(lcfg *config.Linters, enabledByDefaultLinters []*linter.Config) map[string]*linter.Config {
3030
resultLintersSet := map[string]*linter.Config{}
3131
switch {
3232
case len(lcfg.Presets) != 0:
@@ -43,7 +43,7 @@ func (es EnabledSet) build(lcfg *config.Linters, enabledByDefaultLinters []linte
4343
for _, p := range lcfg.Presets {
4444
for _, lc := range es.m.GetAllLinterConfigsForPreset(p) {
4545
lc := lc
46-
resultLintersSet[lc.Name()] = &lc
46+
resultLintersSet[lc.Name()] = lc
4747
}
4848
}
4949

@@ -121,23 +121,23 @@ func (es EnabledSet) optimizeLintersSet(linters map[string]*linter.Config) {
121121
linters[mega.Name()] = &lc
122122
}
123123

124-
func (es EnabledSet) Get() ([]linter.Config, error) {
124+
func (es EnabledSet) Get() ([]*linter.Config, error) {
125125
if err := es.v.validateEnabledDisabledLintersConfig(&es.cfg.Linters); err != nil {
126126
return nil, err
127127
}
128128

129129
resultLintersSet := es.build(&es.cfg.Linters, es.m.GetAllEnabledByDefaultLinters())
130130

131-
var resultLinters []linter.Config
131+
var resultLinters []*linter.Config
132132
for _, lc := range resultLintersSet {
133-
resultLinters = append(resultLinters, *lc)
133+
resultLinters = append(resultLinters, lc)
134134
}
135135

136136
es.verbosePrintLintersStatus(resultLinters)
137137
return resultLinters, nil
138138
}
139139

140-
func (es EnabledSet) verbosePrintLintersStatus(lcs []linter.Config) {
140+
func (es EnabledSet) verbosePrintLintersStatus(lcs []*linter.Config) {
141141
var linterNames []string
142142
for _, lc := range lcs {
143143
linterNames = append(linterNames, lc.Name())

pkg/lint/lintersdb/enabled_set_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ func TestGetEnabledLintersSet(t *testing.T) {
8585
for _, c := range cases {
8686
c := c
8787
t.Run(c.name, func(t *testing.T) {
88-
defaultLinters := []linter.Config{}
88+
var defaultLinters []*linter.Config
8989
for _, ln := range c.def {
90-
defaultLinters = append(defaultLinters, *m.GetLinterConfig(ln))
90+
defaultLinters = append(defaultLinters, m.GetLinterConfig(ln))
9191
}
9292
els := es.build(&c.cfg, defaultLinters)
9393
var enabledLinters []string

pkg/lint/lintersdb/manager.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88
)
99

1010
type Manager struct {
11-
nameToLC map[string]linter.Config
11+
nameToLC map[string]*linter.Config
1212
}
1313

1414
func NewManager() *Manager {
1515
m := &Manager{}
16-
nameToLC := make(map[string]linter.Config)
16+
nameToLC := make(map[string]*linter.Config)
1717
for _, lc := range m.GetAllSupportedLinterConfigs() {
1818
for _, name := range lc.AllNames() {
1919
nameToLC[name] = lc
@@ -43,22 +43,22 @@ func (m Manager) GetLinterConfig(name string) *linter.Config {
4343
return nil
4444
}
4545

46-
return &lc
46+
return lc
4747
}
4848

49-
func enableLinterConfigs(lcs []linter.Config, isEnabled func(lc *linter.Config) bool) []linter.Config {
50-
var ret []linter.Config
49+
func enableLinterConfigs(lcs []*linter.Config, isEnabled func(lc *linter.Config) bool) []*linter.Config {
50+
var ret []*linter.Config
5151
for _, lc := range lcs {
5252
lc := lc
53-
lc.EnabledByDefault = isEnabled(&lc)
53+
lc.EnabledByDefault = isEnabled(lc)
5454
ret = append(ret, lc)
5555
}
5656

5757
return ret
5858
}
5959

60-
func (Manager) GetAllSupportedLinterConfigs() []linter.Config {
61-
lcs := []linter.Config{
60+
func (Manager) GetAllSupportedLinterConfigs() []*linter.Config {
61+
lcs := []*linter.Config{
6262
linter.NewConfig(golinters.Govet{}).
6363
WithTypeInfo().
6464
WithPresets(linter.PresetBugs).
@@ -226,8 +226,8 @@ func (Manager) GetAllSupportedLinterConfigs() []linter.Config {
226226
})
227227
}
228228

229-
func (m Manager) GetAllEnabledByDefaultLinters() []linter.Config {
230-
var ret []linter.Config
229+
func (m Manager) GetAllEnabledByDefaultLinters() []*linter.Config {
230+
var ret []*linter.Config
231231
for _, lc := range m.GetAllSupportedLinterConfigs() {
232232
if lc.EnabledByDefault {
233233
ret = append(ret, lc)
@@ -237,18 +237,18 @@ func (m Manager) GetAllEnabledByDefaultLinters() []linter.Config {
237237
return ret
238238
}
239239

240-
func linterConfigsToMap(lcs []linter.Config) map[string]*linter.Config {
240+
func linterConfigsToMap(lcs []*linter.Config) map[string]*linter.Config {
241241
ret := map[string]*linter.Config{}
242242
for _, lc := range lcs {
243243
lc := lc // local copy
244-
ret[lc.Name()] = &lc
244+
ret[lc.Name()] = lc
245245
}
246246

247247
return ret
248248
}
249249

250-
func (m Manager) GetAllLinterConfigsForPreset(p string) []linter.Config {
251-
ret := []linter.Config{}
250+
func (m Manager) GetAllLinterConfigsForPreset(p string) []*linter.Config {
251+
var ret []*linter.Config
252252
for _, lc := range m.GetAllSupportedLinterConfigs() {
253253
for _, ip := range lc.InPresets {
254254
if p == ip {

0 commit comments

Comments
 (0)