Skip to content

Commit e8ed573

Browse files
authored
refactor: enable gocritic linter; fix lint issues (#1375)
1 parent 51ac5c2 commit e8ed573

17 files changed

+79
-70
lines changed

‎.golangci.yml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ version: "2"
66
linters:
77
default: none
88
enable:
9+
- gocritic
910
- govet
1011
- ineffassign
1112
- misspell
@@ -15,6 +16,11 @@ linters:
1516
- unused
1617

1718
settings:
19+
gocritic:
20+
enable-all: true
21+
disabled-checks:
22+
- hugeParam
23+
- rangeValCopy
1824
govet:
1925
enable-all: true
2026
disable:
@@ -73,3 +79,11 @@ linters:
7379
- name: use-any
7480
- name: var-declaration
7581
- name: var-naming
82+
83+
issues:
84+
# Show all issues from a linter.
85+
max-issues-per-linter: 0
86+
# Show all issues with the same text.
87+
max-same-issues: 0
88+
# Show all issues for a line.
89+
uniq-by-line: false

‎cli/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ func getVersion(builtBy, date, commit, version string) string {
190190
bi, ok := debug.ReadBuildInfo()
191191
if ok {
192192
version = strings.TrimPrefix(bi.Main.Version, "v")
193-
if len(buildInfo) == 0 {
193+
if buildInfo == "" {
194194
return fmt.Sprintf("version %s\n", version)
195195
}
196196
}

‎cli/main_test.go‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ func TestXDGConfigDirIsPreferredFirst(t *testing.T) {
2525

2626
xdgDirPath := filepath.FromSlash("/tmp-iofs/xdg/config")
2727
homeDirPath := filepath.FromSlash("/tmp-iofs/home/tester")
28-
AppFs.MkdirAll(xdgDirPath, 0755)
29-
AppFs.MkdirAll(homeDirPath, 0755)
28+
AppFs.MkdirAll(xdgDirPath, 0o755)
29+
AppFs.MkdirAll(homeDirPath, 0o755)
3030

31-
afero.WriteFile(AppFs, filepath.Join(xdgDirPath, "revive.toml"), []byte("\n"), 0644)
31+
afero.WriteFile(AppFs, filepath.Join(xdgDirPath, "revive.toml"), []byte("\n"), 0o644)
3232
t.Setenv("XDG_CONFIG_HOME", xdgDirPath)
3333

34-
afero.WriteFile(AppFs, filepath.Join(homeDirPath, "revive.toml"), []byte("\n"), 0644)
34+
afero.WriteFile(AppFs, filepath.Join(homeDirPath, "revive.toml"), []byte("\n"), 0o644)
3535
setHome(t, homeDirPath)
3636

3737
got := buildDefaultConfigPath()
@@ -45,9 +45,9 @@ func TestXDGConfigDirIsPreferredFirst(t *testing.T) {
4545
func TestHomeConfigDir(t *testing.T) {
4646
t.Cleanup(func() { AppFs = afero.NewMemMapFs() })
4747
homeDirPath := filepath.FromSlash("/tmp-iofs/home/tester")
48-
AppFs.MkdirAll(homeDirPath, 0755)
48+
AppFs.MkdirAll(homeDirPath, 0o755)
4949

50-
afero.WriteFile(AppFs, filepath.Join(homeDirPath, "revive.toml"), []byte("\n"), 0644)
50+
afero.WriteFile(AppFs, filepath.Join(homeDirPath, "revive.toml"), []byte("\n"), 0o644)
5151
setHome(t, homeDirPath)
5252

5353
got := buildDefaultConfigPath()
@@ -69,9 +69,9 @@ func setHome(t *testing.T, dir string) {
6969
func TestXDGConfigDir(t *testing.T) {
7070
t.Cleanup(func() { AppFs = afero.NewMemMapFs() })
7171
xdgDirPath := filepath.FromSlash("/tmp-iofs/xdg/config")
72-
AppFs.MkdirAll(xdgDirPath, 0755)
72+
AppFs.MkdirAll(xdgDirPath, 0o755)
7373

74-
afero.WriteFile(AppFs, filepath.Join(xdgDirPath, "revive.toml"), []byte("\n"), 0644)
74+
afero.WriteFile(AppFs, filepath.Join(xdgDirPath, "revive.toml"), []byte("\n"), 0o644)
7575
t.Setenv("XDG_CONFIG_HOME", xdgDirPath)
7676

7777
got := buildDefaultConfigPath()

‎lint/file.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func (f *File) disabledIntervals(rules []Rule, mustSpecifyDisableReason bool, fa
222222

223223
for _, name := range tempNames {
224224
name = strings.Trim(name, "\n")
225-
if len(name) > 0 {
225+
if name != "" {
226226
ruleNames = append(ruleNames, name)
227227
}
228228
}

‎lint/filefilter.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func ParseFileFilter(rawFilter string) (*FileFilter, error) {
3131
rawFilter = strings.TrimSpace(rawFilter)
3232
result := new(FileFilter)
3333
result.raw = rawFilter
34-
result.matchesNothing = len(result.raw) == 0
34+
result.matchesNothing = result.raw == ""
3535
result.matchesAll = result.raw == "*" || result.raw == "~"
3636
if !result.matchesAll && !result.matchesNothing {
3737
if err := result.prepareRegexp(); err != nil {

‎lint/name.go‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ func Name(name string, allowlist, blocklist []string) (should string) {
2828
w, i := 0, 0 // index of start of word, scan
2929
for i+1 <= len(runes) {
3030
eow := false // whether we hit the end of a word
31-
if i+1 == len(runes) {
31+
switch {
32+
case i+1 == len(runes):
3233
eow = true
33-
} else if runes[i+1] == '_' {
34+
case runes[i+1] == '_':
3435
// underscore; shift the remainder forward over any run of underscores
3536
eow = true
3637
n := 1
@@ -45,7 +46,7 @@ func Name(name string, allowlist, blocklist []string) (should string) {
4546

4647
copy(runes[i+1:], runes[i+n+1:])
4748
runes = runes[:len(runes)-n]
48-
} else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
49+
case unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]):
4950
// lower->non-lower
5051
eow = true
5152
}

‎lint/package.go‎

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,17 @@ import (
1717

1818
// Package represents a package in the project.
1919
type Package struct {
20-
fset *token.FileSet
20+
fset *token.FileSet
21+
22+
mu sync.RWMutex
2123
files map[string]*File
2224
goVersion *goversion.Version
23-
2425
typesPkg *types.Package
2526
typesInfo *types.Info
26-
2727
// sortable is the set of types in the package that implement sort.Interface.
2828
sortable map[string]bool
2929
// main is whether this is a "main" package.
3030
main int
31-
sync.RWMutex
3231
}
3332

3433
var (
@@ -47,16 +46,16 @@ var (
4746

4847
// Files return package's files.
4948
func (p *Package) Files() map[string]*File {
50-
p.RLock()
51-
defer p.RUnlock()
49+
p.mu.RLock()
50+
defer p.mu.RUnlock()
5251

5352
return p.files
5453
}
5554

5655
// IsMain returns if that's the main package.
5756
func (p *Package) IsMain() bool {
58-
p.Lock()
59-
defer p.Unlock()
57+
p.mu.Lock()
58+
defer p.mu.Unlock()
6059

6160
switch p.main {
6261
case trueValue:
@@ -76,32 +75,32 @@ func (p *Package) IsMain() bool {
7675

7776
// TypesPkg yields information on this package
7877
func (p *Package) TypesPkg() *types.Package {
79-
p.RLock()
80-
defer p.RUnlock()
78+
p.mu.RLock()
79+
defer p.mu.RUnlock()
8180

8281
return p.typesPkg
8382
}
8483

8584
// TypesInfo yields type information of this package identifiers
8685
func (p *Package) TypesInfo() *types.Info {
87-
p.RLock()
88-
defer p.RUnlock()
86+
p.mu.RLock()
87+
defer p.mu.RUnlock()
8988

9089
return p.typesInfo
9190
}
9291

9392
// Sortable yields a map of sortable types in this package
9493
func (p *Package) Sortable() map[string]bool {
95-
p.RLock()
96-
defer p.RUnlock()
94+
p.mu.RLock()
95+
defer p.mu.RUnlock()
9796

9897
return p.sortable
9998
}
10099

101100
// TypeCheck performs type checking for given package.
102101
func (p *Package) TypeCheck() error {
103-
p.Lock()
104-
defer p.Unlock()
102+
p.mu.Lock()
103+
defer p.mu.Unlock()
105104

106105
alreadyTypeChecked := p.typesInfo != nil || p.typesPkg != nil
107106
if alreadyTypeChecked {
@@ -157,8 +156,8 @@ func check(config *types.Config, n string, fset *token.FileSet, astFiles []*ast.
157156

158157
// TypeOf returns the type of expression.
159158
func (p *Package) TypeOf(expr ast.Expr) types.Type {
160-
p.RLock()
161-
defer p.RUnlock()
159+
p.mu.RLock()
160+
defer p.mu.RUnlock()
162161

163162
if p.typesInfo == nil {
164163
return nil
@@ -177,8 +176,8 @@ const (
177176
)
178177

179178
func (p *Package) scanSortable() {
180-
p.Lock()
181-
defer p.Unlock()
179+
p.mu.Lock()
180+
defer p.mu.Unlock()
182181

183182
sortableFlags := map[string]sortableMethodsFlags{}
184183
for _, f := range p.files {
@@ -216,8 +215,8 @@ func (p *Package) lint(rules []Rule, config Config, failures chan Failure) error
216215

217216
// IsAtLeastGoVersion returns true if the Go version for this package is v or higher, false otherwise
218217
func (p *Package) IsAtLeastGoVersion(v *goversion.Version) bool {
219-
p.RLock()
220-
defer p.RUnlock()
218+
p.mu.RLock()
219+
defer p.mu.RUnlock()
221220

222221
return p.goVersion.GreaterThanOrEqual(v)
223222
}

‎revivelib/core.go‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (r *Revive) Lint(patterns ...*LintPattern) (<-chan lint.Failure, error) {
116116
func (r *Revive) Format(
117117
formatterName string,
118118
failuresChan <-chan lint.Failure,
119-
) (string, int, error) {
119+
) (output string, exitCode int, err error) {
120120
conf := r.config
121121
formatChan := make(chan lint.Failure)
122122
exitChan := make(chan bool)
@@ -126,19 +126,12 @@ func (r *Revive) Format(
126126
return "", 0, fmt.Errorf("formatting - getting formatter: %w", err)
127127
}
128128

129-
var (
130-
output string
131-
formatErr error
132-
)
133-
134129
go func() {
135-
output, formatErr = formatter.Format(formatChan, *conf)
130+
output, err = formatter.Format(formatChan, *conf)
136131

137132
exitChan <- true
138133
}()
139134

140-
exitCode := 0
141-
142135
for failure := range failuresChan {
143136
if failure.Confidence < conf.Confidence {
144137
continue
@@ -162,8 +155,8 @@ func (r *Revive) Format(
162155
close(formatChan)
163156
<-exitChan
164157

165-
if formatErr != nil {
166-
return "", exitCode, fmt.Errorf("formatting: %w", formatErr)
158+
if err != nil {
159+
return "", exitCode, fmt.Errorf("formatting: %w", err)
167160
}
168161

169162
return output, exitCode, nil
@@ -188,7 +181,7 @@ func normalizeSplit(strs []string) []string {
188181

189182
for _, s := range strs {
190183
t := strings.Trim(s, " \t")
191-
if len(t) > 0 {
184+
if t != "" {
192185
res = append(res, t)
193186
}
194187
}

‎rule/dot_imports.go‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package rule
33
import (
44
"fmt"
55
"go/ast"
6+
"strconv"
67

78
"github.com/mgechev/revive/lint"
89
)
@@ -94,7 +95,7 @@ func (w lintImports) Visit(_ ast.Node) ast.Visitor {
9495
type allowPackages map[string]struct{}
9596

9697
func (ap allowPackages) add(pkg string) {
97-
ap[fmt.Sprintf(`"%s"`, pkg)] = struct{}{} // import path strings are with double quotes
98+
ap[strconv.Quote(pkg)] = struct{}{} // import path strings are with double quotes
9899
}
99100

100101
func (ap allowPackages) isAllowedPackage(pkg string) bool {

‎rule/import_alias_naming.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type ImportAliasNamingRule struct {
1515

1616
const defaultImportAliasNamingAllowRule = "^[a-z][a-z0-9]{0,}$"
1717

18+
//nolint:gocritic // regexpSimplify: backward compatibility
1819
var defaultImportAliasNamingAllowRegexp = regexp.MustCompile(defaultImportAliasNamingAllowRule)
1920

2021
// Configure validates the rule configuration, and configures the rule accordingly.

0 commit comments

Comments
 (0)