diff --git a/internal/cache/cache.go b/internal/cache/cache.go index cc6606a39c19..fefb3998bfd0 100644 --- a/internal/cache/cache.go +++ b/internal/cache/cache.go @@ -58,7 +58,7 @@ func Open(dir string) (*Cache, error) { return nil, err } if !info.IsDir() { - return nil, &os.PathError{Op: "open", Path: dir, Err: fmt.Errorf("not a directory")} + return nil, &os.PathError{Op: "open", Path: dir, Err: errors.New("not a directory")} } for i := 0; i < 256; i++ { name := filepath.Join(dir, fmt.Sprintf("%02x", i)) @@ -504,7 +504,7 @@ func (c *Cache) copyFile(file io.ReadSeeker, out OutputID, size int64) error { sum := h.Sum(nil) if !bytes.Equal(sum, out[:]) { _ = f.Truncate(0) - return fmt.Errorf("file content changed underfoot") + return errors.New("file content changed underfoot") } // Commit cache file entry. diff --git a/internal/cache/default.go b/internal/cache/default.go index 6695006298aa..4558fb2664a7 100644 --- a/internal/cache/default.go +++ b/internal/cache/default.go @@ -5,6 +5,7 @@ package cache import ( + "errors" "fmt" "log" "os" @@ -69,7 +70,7 @@ func DefaultDir() string { return } if defaultDir != "" { - defaultDirErr = fmt.Errorf("GOLANGCI_LINT_CACHE is not an absolute path") + defaultDirErr = errors.New("GOLANGCI_LINT_CACHE is not an absolute path") return } diff --git a/pkg/commands/run.go b/pkg/commands/run.go index c6b729122e27..9f9d5ea47bb1 100644 --- a/pkg/commands/run.go +++ b/pkg/commands/run.go @@ -281,7 +281,7 @@ func (e *Executor) initRun() { Run: e.executeRun, PreRunE: func(_ *cobra.Command, _ []string) error { if ok := e.acquireFileLock(); !ok { - return fmt.Errorf("parallel golangci-lint is running") + return errors.New("parallel golangci-lint is running") } return nil }, diff --git a/pkg/config/linters_settings_gocritic_test.go b/pkg/config/linters_settings_gocritic_test.go index 98ad608e0a87..6b961a7379dd 100644 --- a/pkg/config/linters_settings_gocritic_test.go +++ b/pkg/config/linters_settings_gocritic_test.go @@ -1,7 +1,7 @@ package config import ( - "fmt" + "log" "sort" "testing" @@ -10,45 +10,50 @@ import ( "github.com/golangci/golangci-lint/pkg/logutils" ) -func TestUtils(t *testing.T) { +func Test_intersectStringSlice(t *testing.T) { s1 := []string{"diagnostic", "experimental", "opinionated"} s2 := []string{"opinionated", "experimental"} + s3 := intersectStringSlice(s1, s2) + sort.Strings(s3) + assert.Equal(t, s3, []string{"experimental", "opinionated"}) } +func Test_filterByDisableTags(t *testing.T) { + disabledTags := []string{"experimental", "opinionated"} + enabledChecks := []string{"appendAssign", "sortSlice", "caseOrder", "dupImport"} + + filterEnabledChecks := filterByDisableTags(enabledChecks, disabledTags, &tLog{}) + + sort.Strings(filterEnabledChecks) + + assert.Equal(t, []string{"appendAssign", "caseOrder"}, filterEnabledChecks) +} + type tLog struct{} func (l *tLog) Fatalf(format string, args ...interface{}) { - fmt.Printf(fmt.Sprintf(format, args...) + "\n") + log.Printf(format, args...) } func (l *tLog) Panicf(format string, args ...interface{}) { - fmt.Printf(fmt.Sprintf(format, args...) + "\n") + log.Printf(format, args...) } func (l *tLog) Errorf(format string, args ...interface{}) { - fmt.Printf(fmt.Sprintf(format, args...) + "\n") + log.Printf(format, args...) } func (l *tLog) Warnf(format string, args ...interface{}) { - fmt.Printf(fmt.Sprintf(format, args...) + "\n") + log.Printf(format, args...) } func (l *tLog) Infof(format string, args ...interface{}) { - fmt.Printf(fmt.Sprintf(format, args...) + "\n") + log.Printf(format, args...) } func (l *tLog) Child(name string) logutils.Log { return nil } func (l *tLog) SetLevel(level logutils.LogLevel) {} - -func TestFilterByDisableTags(t *testing.T) { - testLog := &tLog{} - disabledTags := []string{"experimental", "opinionated"} - enabledChecks := []string{"appendAssign", "sortSlice", "caseOrder", "dupImport"} - filterEnabledChecks := filterByDisableTags(enabledChecks, disabledTags, testLog) - sort.Strings(filterEnabledChecks) - assert.Equal(t, []string{"appendAssign", "caseOrder"}, filterEnabledChecks) -} diff --git a/pkg/config/reader.go b/pkg/config/reader.go index c2c73e6f6f08..2dfd3c06c9b0 100644 --- a/pkg/config/reader.go +++ b/pkg/config/reader.go @@ -79,7 +79,7 @@ func (r *FileReader) parseConfig() error { r.log.Infof("Used config file %s", usedConfigFile) usedConfigDir := filepath.Dir(usedConfigFile) if usedConfigDir, err = filepath.Abs(usedConfigDir); err != nil { - return fmt.Errorf("can't get config directory") + return errors.New("can't get config directory") } r.cfg.cfgDir = usedConfigDir @@ -216,7 +216,7 @@ func (r *FileReader) parseConfigOption() (string, error) { configFile := cfg.Run.Config if cfg.Run.NoConfig && configFile != "" { - return "", fmt.Errorf("can't combine option --config and --no-config") + return "", errors.New("can't combine option --config and --no-config") } if cfg.Run.NoConfig { @@ -225,7 +225,7 @@ func (r *FileReader) parseConfigOption() (string, error) { configFile, err := homedir.Expand(configFile) if err != nil { - return "", fmt.Errorf("failed to expand configuration path") + return "", errors.New("failed to expand configuration path") } return configFile, nil diff --git a/pkg/golinters/goanalysis/linter.go b/pkg/golinters/goanalysis/linter.go index eb27130cc9f4..50a4ca088ee9 100644 --- a/pkg/golinters/goanalysis/linter.go +++ b/pkg/golinters/goanalysis/linter.go @@ -108,7 +108,7 @@ func (lnt *Linter) configureAnalyzer(a *analysis.Analyzer, cfg map[string]interf if f == nil { validFlagNames := allFlagNames(&a.Flags) if len(validFlagNames) == 0 { - return fmt.Errorf("analyzer doesn't have settings") + return errors.New("analyzer doesn't have settings") } return fmt.Errorf("analyzer doesn't have setting %q, valid settings: %v", diff --git a/pkg/lint/lintersdb/validator.go b/pkg/lint/lintersdb/validator.go index 2f00351857a0..52a70d85900d 100644 --- a/pkg/lint/lintersdb/validator.go +++ b/pkg/lint/lintersdb/validator.go @@ -1,6 +1,7 @@ package lintersdb import ( + "errors" "fmt" "strings" @@ -47,7 +48,7 @@ func (v Validator) validatePresets(cfg *config.Linters) error { } if len(cfg.Presets) != 0 && cfg.EnableAll { - return fmt.Errorf("--presets is incompatible with --enable-all") + return errors.New("--presets is incompatible with --enable-all") } return nil @@ -55,12 +56,12 @@ func (v Validator) validatePresets(cfg *config.Linters) error { func (v Validator) validateAllDisableEnableOptions(cfg *config.Linters) error { if cfg.EnableAll && cfg.DisableAll { - return fmt.Errorf("--enable-all and --disable-all options must not be combined") + return errors.New("--enable-all and --disable-all options must not be combined") } if cfg.DisableAll { if len(cfg.Enable) == 0 && len(cfg.Presets) == 0 { - return fmt.Errorf("all linters were disabled, but no one linter was enabled: must enable at least one") + return errors.New("all linters were disabled, but no one linter was enabled: must enable at least one") } if len(cfg.Disable) != 0 { diff --git a/pkg/result/processors/autogenerated_exclude.go b/pkg/result/processors/autogenerated_exclude.go index 57388f64fa48..a11b68f77c94 100644 --- a/pkg/result/processors/autogenerated_exclude.go +++ b/pkg/result/processors/autogenerated_exclude.go @@ -1,7 +1,6 @@ package processors import ( - "fmt" "go/parser" "go/token" "path/filepath" @@ -103,7 +102,7 @@ func (p *AutogeneratedExclude) getOrCreateFileSummary(i *result.Issue) (*ageFile p.fileSummaryCache[i.FilePath()] = fs if i.FilePath() == "" { - return nil, fmt.Errorf("no file path for issue") + return nil, errors.New("no file path for issue") } doc, err := getDoc(i.FilePath()) diff --git a/pkg/result/processors/nolint.go b/pkg/result/processors/nolint.go index 01f597e94d5b..43115b9cf2a0 100644 --- a/pkg/result/processors/nolint.go +++ b/pkg/result/processors/nolint.go @@ -1,7 +1,7 @@ package processors import ( - "fmt" + "errors" "go/ast" "go/parser" "go/token" @@ -105,7 +105,7 @@ func (p *Nolint) getOrCreateFileData(i *result.Issue) (*fileData, error) { p.cache[i.FilePath()] = fd if i.FilePath() == "" { - return nil, fmt.Errorf("no file path for issue") + return nil, errors.New("no file path for issue") } // TODO: migrate this parsing to go/analysis facts diff --git a/scripts/expand_website_templates/main.go b/scripts/expand_website_templates/main.go index 4e8e2dcc3f21..3b78bbdefd65 100644 --- a/scripts/expand_website_templates/main.go +++ b/scripts/expand_website_templates/main.go @@ -50,7 +50,7 @@ func main() { if err := rewriteDocs(replacements); err != nil { log.Fatalf("Failed to rewrite docs: %s", err) } - log.Printf("Successfully expanded templates") + log.Print("Successfully expanded templates") } func updateStateFile(replacements map[string]string) error { diff --git a/test/bench/bench_test.go b/test/bench/bench_test.go index 4b43ff89b31a..a6e83212bb93 100644 --- a/test/bench/bench_test.go +++ b/test/bench/bench_test.go @@ -2,6 +2,7 @@ package bench import ( "bytes" + "errors" "fmt" "go/build" "log" @@ -123,7 +124,7 @@ func getLinterMemoryMB(b *testing.B, progName string) (int, error) { } } if progPID == 0 { - return 0, fmt.Errorf("no process") + return 0, errors.New("no process") } allProgPIDs := []int{progPID} diff --git a/test/errchk.go b/test/errchk.go index d94a055b40ec..76bb96ce1112 100644 --- a/test/errchk.go +++ b/test/errchk.go @@ -96,7 +96,7 @@ func errorCheck(outStr string, wantAuto bool, defaultWantedLinter string, fullsh } if len(out) > 0 { - errs = append(errs, fmt.Errorf("unmatched errors")) + errs = append(errs, errors.New("unmatched errors")) for _, errLine := range out { errs = append(errs, fmt.Errorf("%s", errLine)) } diff --git a/test/testdata_etc/abspath/with_issue.go b/test/testdata_etc/abspath/with_issue.go index e68f3ab0bcda..4ca09a58321f 100644 --- a/test/testdata_etc/abspath/with_issue.go +++ b/test/testdata_etc/abspath/with_issue.go @@ -6,6 +6,6 @@ func f() { if true { return } else { - fmt.Printf("") + fmt.Print("") } }