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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

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

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

Lines changed: 10 additions & 8 deletions
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

Lines changed: 1 addition & 1 deletion
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

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

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

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

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

0 commit comments

Comments
 (0)