Skip to content

Commit

Permalink
dev: reformat code with gofumpt (#4500)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear authored Mar 13, 2024
1 parent 51a963f commit ea82373
Show file tree
Hide file tree
Showing 16 changed files with 146 additions and 76 deletions.
10 changes: 6 additions & 4 deletions pkg/fsutils/fsutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ func IsDir(filename string) bool {
return err == nil && fi.IsDir()
}

var cachedWd string
var cachedWdError error
var getWdOnce sync.Once
var useCache = true
var (
cachedWd string
cachedWdError error
getWdOnce sync.Once
useCache = true
)

func UseWdCache(use bool) {
useCache = use
Expand Down
15 changes: 10 additions & 5 deletions pkg/golinters/goanalysis/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ type runner struct {
}

func newRunner(prefix string, logger logutils.Log, pkgCache *pkgcache.Cache, loadGuard *load.Guard,
loadMode LoadMode, sw *timeutils.Stopwatch) *runner {
loadMode LoadMode, sw *timeutils.Stopwatch,
) *runner {
return &runner{
prefix: prefix,
log: logger,
Expand All @@ -80,7 +81,8 @@ func newRunner(prefix string, logger logutils.Log, pkgCache *pkgcache.Cache, loa
// singlechecker and the multi-analysis commands.
// It returns the appropriate exit code.
func (r *runner) run(analyzers []*analysis.Analyzer, initialPackages []*packages.Package) ([]Diagnostic,
[]error, map[*analysis.Pass]*packages.Package) {
[]error, map[*analysis.Pass]*packages.Package,
) {
debugf("Analyzing %d packages on load mode %s", len(initialPackages), r.loadMode)
defer r.pkgCache.Trim()

Expand Down Expand Up @@ -116,7 +118,8 @@ func (r *runner) markAllActions(a *analysis.Analyzer, pkg *packages.Package, mar
}

func (r *runner) makeAction(a *analysis.Analyzer, pkg *packages.Package,
initialPkgs map[*packages.Package]bool, actions map[actKey]*action, actAlloc *actionAllocator) *action {
initialPkgs map[*packages.Package]bool, actions map[actKey]*action, actAlloc *actionAllocator,
) *action {
k := actKey{a, pkg}
act, ok := actions[k]
if ok {
Expand Down Expand Up @@ -150,7 +153,8 @@ func (r *runner) makeAction(a *analysis.Analyzer, pkg *packages.Package,
}

func (r *runner) buildActionFactDeps(act *action, a *analysis.Analyzer, pkg *packages.Package,
initialPkgs map[*packages.Package]bool, actions map[actKey]*action, actAlloc *actionAllocator) {
initialPkgs map[*packages.Package]bool, actions map[actKey]*action, actAlloc *actionAllocator,
) {
// An analysis that consumes/produces facts
// must run on the package's dependencies too.
if len(a.FactTypes) == 0 {
Expand All @@ -175,7 +179,8 @@ func (r *runner) buildActionFactDeps(act *action, a *analysis.Analyzer, pkg *pac

//nolint:gocritic
func (r *runner) prepareAnalysis(pkgs []*packages.Package,
analyzers []*analysis.Analyzer) (map[*packages.Package]bool, []*action, []*action) {
analyzers []*analysis.Analyzer,
) (map[*packages.Package]bool, []*action, []*action) {
// Construct the action graph.

// Each graph node (action) is one unit of analysis.
Expand Down
6 changes: 4 additions & 2 deletions pkg/golinters/goanalysis/runners.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func getIssuesCacheKey(analyzers []*analysis.Analyzer) string {
}

func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages.Package]bool,
issues []result.Issue, lintCtx *linter.Context, analyzers []*analysis.Analyzer) {
issues []result.Issue, lintCtx *linter.Context, analyzers []*analysis.Analyzer,
) {
startedAt := time.Now()
perPkgIssues := map[*packages.Package][]result.Issue{}
for ind := range issues {
Expand Down Expand Up @@ -185,7 +186,8 @@ func saveIssuesToCache(allPkgs []*packages.Package, pkgsFromCache map[*packages.

//nolint:gocritic
func loadIssuesFromCache(pkgs []*packages.Package, lintCtx *linter.Context,
analyzers []*analysis.Analyzer) ([]result.Issue, map[*packages.Package]bool) {
analyzers []*analysis.Analyzer,
) ([]result.Issue, map[*packages.Package]bool) {
startedAt := time.Now()

lintResKey := getIssuesCacheKey(analyzers)
Expand Down
6 changes: 4 additions & 2 deletions pkg/golinters/nolintlint/nolintlint.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,10 @@ func NewLinter(needs Needs, excludes []string) (*Linter, error) {
}, nil
}

var leadingSpacePattern = regexp.MustCompile(`^//(\s*)`)
var trailingBlankExplanation = regexp.MustCompile(`\s*(//\s*)?$`)
var (
leadingSpacePattern = regexp.MustCompile(`^//(\s*)`)
trailingBlankExplanation = regexp.MustCompile(`\s*(//\s*)?$`)
)

//nolint:funlen,gocyclo
func (l Linter) Run(fset *token.FileSet, nodes ...ast.Node) ([]Issue, error) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/golinters/testpackage.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func NewTestpackage(cfg *config.TestpackageSettings) *goanalysis.Linter {
var a = testpackage.NewAnalyzer()
a := testpackage.NewAnalyzer()

var settings map[string]map[string]any
if cfg != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/lint/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ type ContextLoader struct {
}

func NewContextLoader(cfg *config.Config, log logutils.Log, goenv *goutil.Env,
lineCache *fsutils.LineCache, fileCache *fsutils.FileCache, pkgCache *pkgcache.Cache, loadGuard *load.Guard) *ContextLoader {
lineCache *fsutils.LineCache, fileCache *fsutils.FileCache, pkgCache *pkgcache.Cache, loadGuard *load.Guard,
) *ContextLoader {
return &ContextLoader{
cfg: cfg,
log: log,
Expand Down
6 changes: 4 additions & 2 deletions pkg/lint/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type Runner struct {

func NewRunner(log logutils.Log, cfg *config.Config, goenv *goutil.Env,
lineCache *fsutils.LineCache, fileCache *fsutils.FileCache,
dbManager *lintersdb.Manager, lintCtx *linter.Context) (*Runner, error) {
dbManager *lintersdb.Manager, lintCtx *linter.Context,
) (*Runner, error) {
// Beware that some processors need to add the path prefix when working with paths
// because they get invoked before the path prefixer (exclude and severity rules)
// or process other paths (skip files).
Expand Down Expand Up @@ -129,7 +130,8 @@ func (r *Runner) Run(ctx context.Context, linters []*linter.Config) ([]result.Is
}

func (r *Runner) runLinterSafe(ctx context.Context, lintCtx *linter.Context,
lc *linter.Config) (ret []result.Issue, err error) {
lc *linter.Config,
) (ret []result.Issue, err error) {
defer func() {
if panicData := recover(); panicData != nil {
if pe, ok := panicData.(*errorutil.PanicError); ok {
Expand Down
6 changes: 4 additions & 2 deletions pkg/logutils/out.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ import (
colorable "github.com/mattn/go-colorable"
)

var StdOut = color.Output // https://github.com/golangci/golangci-lint/issues/14
var StdErr = colorable.NewColorableStderr()
var (
StdOut = color.Output // https://github.com/golangci/golangci-lint/issues/14
StdErr = colorable.NewColorableStderr()
)
2 changes: 1 addition & 1 deletion pkg/printers/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

const defaultFileMode = 0644
const defaultFileMode = 0o644

type issuePrinter interface {
Print(issues []result.Issue) error
Expand Down
78 changes: 52 additions & 26 deletions pkg/result/processors/identifier_marker.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,54 +31,80 @@ var replacePatterns = []replacePattern{
{`^composites: (\S+) composite literal uses unkeyed fields$`, "composites: `${1}` composite literal uses unkeyed fields"},

// gosec
{`^(\S+): Blacklisted import (\S+): weak cryptographic primitive$`,
"${1}: Blacklisted import `${2}`: weak cryptographic primitive"},
{
`^(\S+): Blacklisted import (\S+): weak cryptographic primitive$`,
"${1}: Blacklisted import `${2}`: weak cryptographic primitive",
},
{`^TLS InsecureSkipVerify set true.$`, "TLS `InsecureSkipVerify` set true."},

// gosimple
{`should replace loop with (.*)$`, "should replace loop with `${1}`"},
{`should use a simple channel send/receive instead of select with a single case`,
"should use a simple channel send/receive instead of `select` with a single case"},
{`should omit comparison to bool constant, can be simplified to (.+)$`,
"should omit comparison to bool constant, can be simplified to `${1}`"},
{
`should use a simple channel send/receive instead of select with a single case`,
"should use a simple channel send/receive instead of `select` with a single case",
},
{
`should omit comparison to bool constant, can be simplified to (.+)$`,
"should omit comparison to bool constant, can be simplified to `${1}`",
},
{`should write (.+) instead of (.+)$`, "should write `${1}` instead of `${2}`"},
{`redundant return statement$`, "redundant `return` statement"},
{`should replace this if statement with an unconditional strings.TrimPrefix`,
"should replace this `if` statement with an unconditional `strings.TrimPrefix`"},
{
`should replace this if statement with an unconditional strings.TrimPrefix`,
"should replace this `if` statement with an unconditional `strings.TrimPrefix`",
},

// staticcheck
{`this value of (\S+) is never used$`, "this value of `${1}` is never used"},
{`should use time.Since instead of time.Now\(\).Sub$`,
"should use `time.Since` instead of `time.Now().Sub`"},
{`should check returned error before deferring response.Close\(\)$`,
"should check returned error before deferring `response.Close()`"},
{
`should use time.Since instead of time.Now\(\).Sub$`,
"should use `time.Since` instead of `time.Now().Sub`",
},
{
`should check returned error before deferring response.Close\(\)$`,
"should check returned error before deferring `response.Close()`",
},
{`no value of type uint is less than 0$`, "no value of type `uint` is less than `0`"},

// unused
{`(func|const|field|type|var) (\S+) is unused$`, "${1} `${2}` is unused"},

// typecheck
{`^unknown field (\S+) in struct literal$`, "unknown field `${1}` in struct literal"},
{`^invalid operation: (\S+) \(variable of type (\S+)\) has no field or method (\S+)$`,
"invalid operation: `${1}` (variable of type `${2}`) has no field or method `${3}`"},
{
`^invalid operation: (\S+) \(variable of type (\S+)\) has no field or method (\S+)$`,
"invalid operation: `${1}` (variable of type `${2}`) has no field or method `${3}`",
},
{`^undeclared name: (\S+)$`, "undeclared name: `${1}`"},
{`^cannot use addr \(variable of type (\S+)\) as (\S+) value in argument to (\S+)$`,
"cannot use addr (variable of type `${1}`) as `${2}` value in argument to `${3}`"},
{
`^cannot use addr \(variable of type (\S+)\) as (\S+) value in argument to (\S+)$`,
"cannot use addr (variable of type `${1}`) as `${2}` value in argument to `${3}`",
},
{`^other declaration of (\S+)$`, "other declaration of `${1}`"},
{`^(\S+) redeclared in this block$`, "`${1}` redeclared in this block"},

// golint
{`^exported (type|method|function|var|const) (\S+) should have comment or be unexported$`,
"exported ${1} `${2}` should have comment or be unexported"},
{`^comment on exported (type|method|function|var|const) (\S+) should be of the form "(\S+) ..."$`,
"comment on exported ${1} `${2}` should be of the form `${3} ...`"},
{
`^exported (type|method|function|var|const) (\S+) should have comment or be unexported$`,
"exported ${1} `${2}` should have comment or be unexported",
},
{
`^comment on exported (type|method|function|var|const) (\S+) should be of the form "(\S+) ..."$`,
"comment on exported ${1} `${2}` should be of the form `${3} ...`",
},
{`^should replace (.+) with (.+)$`, "should replace `${1}` with `${2}`"},
{`^if block ends with a return statement, so drop this else and outdent its block$`,
"`if` block ends with a `return` statement, so drop this `else` and outdent its block"},
{`^(struct field|var|range var|const|type|(?:func|method|interface method) (?:parameter|result)) (\S+) should be (\S+)$`,
"${1} `${2}` should be `${3}`"},
{`^don't use underscores in Go names; var (\S+) should be (\S+)$`,
"don't use underscores in Go names; var `${1}` should be `${2}`"},
{
`^if block ends with a return statement, so drop this else and outdent its block$`,
"`if` block ends with a `return` statement, so drop this `else` and outdent its block",
},
{
`^(struct field|var|range var|const|type|(?:func|method|interface method) (?:parameter|result)) (\S+) should be (\S+)$`,
"${1} `${2}` should be `${3}`",
},
{
`^don't use underscores in Go names; var (\S+) should be (\S+)$`,
"don't use underscores in Go names; var `${1}` should be `${2}`",
},
}

type IdentifierMarker struct {
Expand Down
66 changes: 44 additions & 22 deletions pkg/result/processors/identifier_marker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,39 +13,61 @@ func TestIdentifierMarker(t *testing.T) {
//nolint:lll
cases := []struct{ in, out string }{
{"unknown field Address in struct literal", "unknown field `Address` in struct literal"},
{"invalid operation: res (variable of type github.com/iotexproject/iotex-core/explorer/idl/explorer.GetBlkOrActResponse) has no field or method Address",
"invalid operation: `res` (variable of type `github.com/iotexproject/iotex-core/explorer/idl/explorer.GetBlkOrActResponse`) has no field or method `Address`"},
{"should use a simple channel send/receive instead of select with a single case",
"should use a simple channel send/receive instead of `select` with a single case"},
{
"invalid operation: res (variable of type github.com/iotexproject/iotex-core/explorer/idl/explorer.GetBlkOrActResponse) has no field or method Address",
"invalid operation: `res` (variable of type `github.com/iotexproject/iotex-core/explorer/idl/explorer.GetBlkOrActResponse`) has no field or method `Address`",
},
{
"should use a simple channel send/receive instead of select with a single case",
"should use a simple channel send/receive instead of `select` with a single case",
},
{"var testInputs is unused", "var `testInputs` is unused"},
{"undeclared name: stateIDLabel", "undeclared name: `stateIDLabel`"},
{"exported type Metrics should have comment or be unexported",
"exported type `Metrics` should have comment or be unexported"},
{`comment on exported function NewMetrics should be of the form "NewMetrics ..."`,
"comment on exported function `NewMetrics` should be of the form `NewMetrics ...`"},
{"cannot use addr (variable of type string) as github.com/iotexproject/iotex-core/pkg/keypair.PublicKey value in argument to action.FakeSeal",
"cannot use addr (variable of type `string`) as `github.com/iotexproject/iotex-core/pkg/keypair.PublicKey` value in argument to `action.FakeSeal`"},
{
"exported type Metrics should have comment or be unexported",
"exported type `Metrics` should have comment or be unexported",
},
{
`comment on exported function NewMetrics should be of the form "NewMetrics ..."`,
"comment on exported function `NewMetrics` should be of the form `NewMetrics ...`",
},
{
"cannot use addr (variable of type string) as github.com/iotexproject/iotex-core/pkg/keypair.PublicKey value in argument to action.FakeSeal",
"cannot use addr (variable of type `string`) as `github.com/iotexproject/iotex-core/pkg/keypair.PublicKey` value in argument to `action.FakeSeal`",
},
{"other declaration of out", "other declaration of `out`"},
{"should check returned error before deferring response.Close()", "should check returned error before deferring `response.Close()`"},
{"should use time.Since instead of time.Now().Sub", "should use `time.Since` instead of `time.Now().Sub`"},
{"TestFibZeroCount redeclared in this block", "`TestFibZeroCount` redeclared in this block"},
{"should replace i += 1 with i++", "should replace `i += 1` with `i++`"},
{"createEntry - result err is always nil", "`createEntry` - result `err` is always `nil`"},
{"should omit comparison to bool constant, can be simplified to !projectIntegration.Model.Storage",
"should omit comparison to bool constant, can be simplified to `!projectIntegration.Model.Storage`"},
{"if block ends with a return statement, so drop this else and outdent its block",
"`if` block ends with a `return` statement, so drop this `else` and outdent its block"},
{"should write pupData := ms.m[pupID] instead of pupData, _ := ms.m[pupID]",
"should write `pupData := ms.m[pupID]` instead of `pupData, _ := ms.m[pupID]`"},
{
"should omit comparison to bool constant, can be simplified to !projectIntegration.Model.Storage",
"should omit comparison to bool constant, can be simplified to `!projectIntegration.Model.Storage`",
},
{
"if block ends with a return statement, so drop this else and outdent its block",
"`if` block ends with a `return` statement, so drop this `else` and outdent its block",
},
{
"should write pupData := ms.m[pupID] instead of pupData, _ := ms.m[pupID]",
"should write `pupData := ms.m[pupID]` instead of `pupData, _ := ms.m[pupID]`",
},
{"no value of type uint is less than 0", "no value of type `uint` is less than `0`"},
{"redundant return statement", "redundant `return` statement"},
{"struct field Id should be ID", "struct field `Id` should be `ID`"},
{"don't use underscores in Go names; var Go_lint should be GoLint",
"don't use underscores in Go names; var `Go_lint` should be `GoLint`"},
{"G501: Blacklisted import crypto/md5: weak cryptographic primitive",
"G501: Blacklisted import `crypto/md5`: weak cryptographic primitive"},
{"S1017: should replace this if statement with an unconditional strings.TrimPrefix",
"S1017: should replace this `if` statement with an unconditional `strings.TrimPrefix`"},
{
"don't use underscores in Go names; var Go_lint should be GoLint",
"don't use underscores in Go names; var `Go_lint` should be `GoLint`",
},
{
"G501: Blacklisted import crypto/md5: weak cryptographic primitive",
"G501: Blacklisted import `crypto/md5`: weak cryptographic primitive",
},
{
"S1017: should replace this if statement with an unconditional strings.TrimPrefix",
"S1017: should replace this `if` statement with an unconditional `strings.TrimPrefix`",
},
}
p := NewIdentifierMarker()

Expand Down
6 changes: 4 additions & 2 deletions pkg/result/processors/max_per_file_from_linter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

type linterToCountMap map[string]int
type fileToLinterToCountMap map[string]linterToCountMap
type (
linterToCountMap map[string]int
fileToLinterToCountMap map[string]linterToCountMap
)

type MaxPerFileFromLinter struct {
flc fileToLinterToCountMap
Expand Down
6 changes: 4 additions & 2 deletions pkg/result/processors/nolint.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

var nolintDebugf = logutils.Debug(logutils.DebugKeyNolint)
var nolintRe = regexp.MustCompile(`^nolint( |:|$)`)
var (
nolintDebugf = logutils.Debug(logutils.DebugKeyNolint)
nolintRe = regexp.MustCompile(`^nolint( |:|$)`)
)

type ignoredRange struct {
linters []string
Expand Down
2 changes: 1 addition & 1 deletion pkg/result/processors/nolint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func TestNolintAliases(t *testing.T) {
}

func TestIgnoredRangeMatches(t *testing.T) {
var testcases = []struct {
testcases := []struct {
doc string
issue result.Issue
linters []string
Expand Down
6 changes: 4 additions & 2 deletions pkg/result/processors/uniq_by_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"github.com/golangci/golangci-lint/pkg/result"
)

type lineToCount map[int]int
type fileToLineToCount map[string]lineToCount
type (
lineToCount map[int]int
fileToLineToCount map[string]lineToCount
)

type UniqByLine struct {
flc fileToLineToCount
Expand Down
Loading

0 comments on commit ea82373

Please sign in to comment.