Skip to content

Commit 3421eae

Browse files
authored
refactor: fix linting issues (#1188)
* refactor: fix thelper issues test/utils_test.go:19:6 thelper test helper function should start from t.Helper() test/utils_test.go:42:6 thelper test helper function should start from t.Helper() test/utils_test.go:63:6 thelper test helper function should start from t.Helper() test/utils_test.go:146:6 thelper test helper function should start from t.Helper() * refactor: fix govet issues rule/error_strings.go:50:21 govet printf: non-constant format string in call to fmt.Errorf * refactor: fix gosimple issue rule/bare_return.go:52:9 gosimple S1016: should convert w (type lintBareReturnRule) to bareReturnFinder instead of using struct literal * refactor: fix errorlint issues lint/filefilter.go:70:86 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors lint/filefilter.go:113:104 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors lint/filefilter.go:125:89 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors lint/linter.go:166:72 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors lint/linter.go:171:73 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors config/config.go:174:57 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors config/config.go:179:64 errorlint non-wrapping format verb for fmt.Errorf. Use `%w` to format errors * refactor: fix revive issue about comment spacing cli/main.go:31:2 revive comment-spacings: no space between comment delimiter and comment text * refactor: fix revive issue about unused-receiver revivelib/core_test.go:77:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ revivelib/core_test.go:81:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ rule/context_as_argument.go:76:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ rule/var_naming.go:73:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ rule/modifies_value_receiver.go:59:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ rule/filename_format.go:43:7 revive unused-receiver: method receiver 'r' is not referenced in method's body, consider removing or renaming it as _ * refactor: fix revive issues about unused-parameter revivelib/core_test.go:81:24 revive unused-parameter: parameter 'file' seems to be unused, consider removing or renaming it as _ revivelib/core_test.go:81:41 revive unused-parameter: parameter 'arguments' seems to be unused, consider removing or renaming it as _ * refactor: fix gocritic issues about commentedOutCode test/utils_test.go:119:5 gocritic commentedOutCode: may want to remove commented-out code rule/unreachable_code.go:65:3 gocritic commentedOutCode: may want to remove commented-out code
1 parent 4dd63ef commit 3421eae

13 files changed

+29
-21
lines changed

cli/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
commit = defaultCommit
2929
date = defaultDate
3030
builtBy = defaultBuilder
31-
//AppFs is used to operations related with user config files
31+
// AppFs is used to operations related with user config files
3232
AppFs = afero.NewOsFs()
3333
)
3434

config/config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,12 @@ func parseConfig(path string, config *lint.Config) error {
171171
}
172172
err = toml.Unmarshal(file, config)
173173
if err != nil {
174-
return fmt.Errorf("cannot parse the config file: %v", err)
174+
return fmt.Errorf("cannot parse the config file: %w", err)
175175
}
176176
for k, r := range config.Rules {
177177
err := r.Initialize()
178178
if err != nil {
179-
return fmt.Errorf("error in config of rule [%s] : [%v]", k, err)
179+
return fmt.Errorf("error in config of rule [%s] : [%w]", k, err)
180180
}
181181
config.Rules[k] = r
182182
}

lint/filefilter.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,21 @@ func (ff *FileFilter) MatchFileName(name string) bool {
5555
return ff.rx.MatchString(name)
5656
}
5757

58-
var fileFilterInvalidGlobRegexp = regexp.MustCompile(`[^/]\*\*[^/]`)
59-
var escapeRegexSymbols = ".+{}()[]^$"
58+
var (
59+
fileFilterInvalidGlobRegexp = regexp.MustCompile(`[^/]\*\*[^/]`)
60+
escapeRegexSymbols = ".+{}()[]^$"
61+
)
6062

6163
func (ff *FileFilter) prepareRegexp() error {
6264
var err error
63-
var src = ff.raw
65+
src := ff.raw
6466
if src == "TEST" {
6567
src = "~_test\\.go"
6668
}
6769
if strings.HasPrefix(src, "~") {
6870
ff.rx, err = regexp.Compile(src[1:])
6971
if err != nil {
70-
return fmt.Errorf("invalid file filter [%s], regexp compile error: [%v]", ff.raw, err)
72+
return fmt.Errorf("invalid file filter [%s], regexp compile error: [%w]", ff.raw, err)
7173
}
7274
return nil
7375
}
@@ -110,7 +112,7 @@ func (ff *FileFilter) prepareRegexp() error {
110112
rxBuild.WriteByte('$')
111113
ff.rx, err = regexp.Compile(rxBuild.String())
112114
if err != nil {
113-
return fmt.Errorf("invalid file filter [%s], regexp compile error after glob expand: [%v]", ff.raw, err)
115+
return fmt.Errorf("invalid file filter [%s], regexp compile error after glob expand: [%w]", ff.raw, err)
114116
}
115117
return nil
116118
}
@@ -122,7 +124,7 @@ func (ff *FileFilter) prepareRegexp() error {
122124
fillRx = "^" + fillRx + "$"
123125
ff.rx, err = regexp.Compile(fillRx)
124126
if err != nil {
125-
return fmt.Errorf("invalid file filter [%s], regexp compile full path: [%v]", ff.raw, err)
127+
return fmt.Errorf("invalid file filter [%s], regexp compile full path: [%w]", ff.raw, err)
126128
}
127129
return nil
128130
}

lint/linter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error)
163163

164164
mod, err := os.ReadFile(modFileName)
165165
if err != nil {
166-
return "", nil, fmt.Errorf("failed to read %q, got %v", modFileName, err)
166+
return "", nil, fmt.Errorf("failed to read %q, got %w", modFileName, err)
167167
}
168168

169169
modAst, err := modfile.ParseLax(modFileName, mod, nil)
170170
if err != nil {
171-
return "", nil, fmt.Errorf("failed to parse %q, got %v", modFileName, err)
171+
return "", nil, fmt.Errorf("failed to parse %q, got %w", modFileName, err)
172172
}
173173

174174
if modAst.Go == nil {

revivelib/core_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ func TestReviveFormat(t *testing.T) {
7474

7575
type mockRule struct{}
7676

77-
func (r *mockRule) Name() string {
77+
func (*mockRule) Name() string {
7878
return "mock-rule"
7979
}
8080

81-
func (r *mockRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
81+
func (*mockRule) Apply(_ *lint.File, _ lint.Arguments) []lint.Failure {
8282
return nil
8383
}
8484

rule/bare_return.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (w lintBareReturnRule) checkFunc(results *ast.FieldList, body *ast.BlockStm
4949
return // nothing to do
5050
}
5151

52-
brf := bareReturnFinder{w.onFailure}
52+
brf := bareReturnFinder(w)
5353
ast.Walk(brf, body)
5454
}
5555

rule/context_as_argument.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func (r *ContextAsArgumentRule) configure(arguments lint.Arguments) error {
7373
return nil
7474
}
7575

76-
func (r *ContextAsArgumentRule) getAllowTypesFromArguments(args lint.Arguments) (map[string]struct{}, error) {
76+
func (*ContextAsArgumentRule) getAllowTypesFromArguments(args lint.Arguments) (map[string]struct{}, error) {
7777
allowTypesBefore := []string{}
7878
if len(args) >= 1 {
7979
argKV, ok := args[0].(map[string]any)

rule/error_strings.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (r *ErrorStringsRule) configure(arguments lint.Arguments) error {
4747
}
4848
}
4949
if len(invalidCustomFunctions) != 0 {
50-
return fmt.Errorf("found invalid custom function: " + strings.Join(invalidCustomFunctions, ","))
50+
return fmt.Errorf("found invalid custom function: %s", strings.Join(invalidCustomFunctions, ","))
5151
}
5252
return nil
5353
}

rule/filename_format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (r *FilenameFormatRule) Apply(file *lint.File, arguments lint.Arguments) []
4040
}}
4141
}
4242

43-
func (r *FilenameFormatRule) getMsgForNonASCIIChars(str string) string {
43+
func (*FilenameFormatRule) getMsgForNonASCIIChars(str string) string {
4444
result := ""
4545
for _, c := range str {
4646
if c <= unicode.MaxASCII {

rule/modifies_value_receiver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (*ModifiesValRecRule) Name() string {
5656
return "modifies-value-receiver"
5757
}
5858

59-
func (r *ModifiesValRecRule) skipType(t ast.Expr, pkg *lint.Package) bool {
59+
func (*ModifiesValRecRule) skipType(t ast.Expr, pkg *lint.Package) bool {
6060
rt := pkg.TypeOf(t)
6161
if rt == nil {
6262
return false

rule/unreachable_code.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ func (w lintUnreachableCode) Visit(node ast.Node) ast.Visitor {
6262
}
6363
loop:
6464
for i, stmt := range blk.List[:len(blk.List)-1] {
65-
// println("iterating ", len(blk.List))
6665
next := blk.List[i+1]
6766
if _, ok := next.(*ast.LabeledStmt); ok {
6867
continue // skip if next statement is labeled

rule/var_naming.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (r *VarNamingRule) configure(arguments lint.Arguments) error {
7070
return nil
7171
}
7272

73-
func (r *VarNamingRule) applyPackageCheckRules(walker *lintNames) {
73+
func (*VarNamingRule) applyPackageCheckRules(walker *lintNames) {
7474
// Package names need slightly different handling than other names.
7575
if strings.Contains(walker.fileAst.Name.Name, "_") && !strings.HasSuffix(walker.fileAst.Name.Name, "_test") {
7676
walker.onFailure(lint.Failure{

test/utils_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
)
1818

1919
func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.RuleConfig) {
20+
t.Helper()
21+
2022
baseDir := filepath.Join("..", "testdata", filepath.Dir(filename))
2123
filename = filepath.Base(filename) + ".go"
2224
fullFilePath := filepath.Join(baseDir, filename)
@@ -40,6 +42,8 @@ func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.Rul
4042
}
4143

4244
func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Rule, config map[string]lint.RuleConfig) error {
45+
t.Helper()
46+
4347
l := lint.New(os.ReadFile, 0)
4448

4549
filePath := filepath.Join(baseDir, fi.Name())
@@ -61,6 +65,8 @@ func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Ru
6165
}
6266

6367
func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, rules []lint.Rule, config map[string]lint.RuleConfig) error {
68+
t.Helper()
69+
6470
l := lint.New(os.ReadFile, 0)
6571

6672
ins := parseInstructions(t, filepath.Join(baseDir, fi.Name()), src)
@@ -110,7 +116,6 @@ func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, ru
110116
copy(failures[i:], failures[i+1:])
111117
failures = failures[:len(failures)-1]
112118

113-
// t.Logf("/%v/ matched at %s:%d", in.Match, fi.Name(), in.Line)
114119
ok = true
115120
break
116121
}
@@ -144,6 +149,8 @@ type JSONInstruction struct {
144149
// parseInstructions parses instructions from the comments in a Go source file.
145150
// It returns nil if none were parsed.
146151
func parseInstructions(t *testing.T, filename string, src []byte) []instruction {
152+
t.Helper()
153+
147154
fset := token.NewFileSet()
148155
f, err := parser.ParseFile(fset, filename, src, parser.ParseComments)
149156
if err != nil {

0 commit comments

Comments
 (0)