Skip to content

Commit 9f6abf7

Browse files
author
Denis Krivak
committed
Update golangci-lint settings and fix issues.
1 parent 4cbd594 commit 9f6abf7

File tree

7 files changed

+72
-53
lines changed

7 files changed

+72
-53
lines changed

.golangci.yml

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,59 +8,73 @@ skip-dirs:
88
linters:
99
disable-all: true
1010
enable:
11-
- deadcode
12-
- errcheck
13-
- gosimple
14-
- govet
15-
- ineffassign
16-
- staticcheck
17-
- structcheck
18-
- typecheck
19-
- unused
20-
- varcheck
11+
- asciicheck
2112
- bodyclose
13+
- cyclop
2214
- dogsled
23-
- dupl
24-
- funlen
15+
- durationcheck
16+
- errcheck
17+
- errname
18+
- errorlint
19+
- exhaustive
20+
- exportloopref
21+
- exportloopref
2522
- gochecknoinits
23+
- gocognit
2624
- goconst
2725
- gocritic
2826
- gocyclo
2927
- godot
28+
- goerr113
3029
- gofmt
3130
- gofumpt
3231
- goimports
33-
- golint
34-
- gomnd
35-
- gomodguard
3632
- goprintffuncname
3733
- gosec
34+
- gosimple
35+
- govet
36+
- importas
37+
- ineffassign
3838
- lll
39-
- maligned
4039
- misspell
4140
- nakedret
4241
- nestif
42+
- noctx
43+
- nolintlint
4344
- prealloc
45+
- revive
4446
- rowserrcheck
45-
- scopelint
47+
- sqlclosecheck
48+
- sqlclosecheck
49+
- staticcheck
4650
- stylecheck
51+
- typecheck
4752
- unconvert
4853
- unparam
54+
- unused
55+
- wastedassign
4956
- whitespace
57+
- wrapcheck
5058

5159
linters-settings:
5260
godot:
5361
scope: toplevel
5462

5563
issues:
5664
exclude-use-default: false
65+
exclude:
66+
- "do not define dynamic errors, use wrapped static errors instead"
5767
exclude-rules:
5868
- path: _test\.go
5969
linters:
6070
- dupl
6171
- errcheck
6272
- funlen
73+
- gocognit
74+
- cyclop
6375
- gosec
64-
- path: cmd/godot/main\.go
76+
- noctx
77+
- path: main\.go
6578
linters:
79+
- cyclop
6680
- gomnd

checks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ var (
2121
// Abbreviations to exclude from capital letters check.
2222
abbreviations = []string{"i.e.", "i. e.", "e.g.", "e. g.", "etc."}
2323

24-
// Special tags in comments like "// nolint:", or "// +k8s:".
24+
// Special tags in comments like "//nolint:", or "//+k8s:".
2525
tags = regexp.MustCompile(`^\+?[a-z0-9]+:`)
2626

2727
// Special hashtags in comments like "// #nosec".
@@ -40,7 +40,7 @@ type position struct {
4040
// checkComments checks every comment accordings to the rules from
4141
// `settings` argument.
4242
func checkComments(comments []comment, settings Settings) []Issue {
43-
var issues []Issue // nolint: prealloc
43+
var issues []Issue
4444
for _, c := range comments {
4545
if settings.Period {
4646
if iss := checkCommentForPeriod(c); iss != nil {

cmd/godot/main.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// godot is a linter that checks if all top-level comments contain a
2+
// period at the end of the last sentence if needed.
13
package main
24

35
import (
@@ -24,7 +26,6 @@ var defaultSettings = godot.Settings{
2426
Capital: false,
2527
}
2628

27-
// nolint: lll
2829
const usage = `Usage:
2930
godot [OPTION] [FILES]
3031
Options:
@@ -34,7 +35,7 @@ Options:
3435
-h, --help show this message
3536
-v, --version show version`
3637

37-
// nolint:maligned
38+
//nolint:maligned
3839
type arguments struct {
3940
config string
4041
fix bool
@@ -44,7 +45,7 @@ type arguments struct {
4445
version bool
4546
}
4647

47-
// nolint: funlen
48+
//nolint:funlen
4849
func main() {
4950
// Read command line arguments
5051
args, err := readArgs()
@@ -112,15 +113,15 @@ func main() {
112113
}
113114

114115
func readArgs() (args arguments, err error) {
115-
if len(os.Args) < 2 { // nolint: gomnd
116+
if len(os.Args) < 2 { //nolint:gomnd
116117
return arguments{}, fmt.Errorf("not enough arguments")
117118
}
118119

119120
// Split `--arg=x` arguments
120121
input := make([]string, 0, len(os.Args)-1)
121122
for i := 1; i < len(os.Args); i++ {
122123
splitted := strings.Split(os.Args[i], "=")
123-
if len(splitted) > 2 { // nolint: gomnd
124+
if len(splitted) > 2 { //nolint:gomnd
124125
return arguments{}, fmt.Errorf("invalid argument '%s'", os.Args[i])
125126
}
126127
input = append(input, splitted...)
@@ -172,15 +173,15 @@ func getSettings(file string) (godot.Settings, error) {
172173
file = defaultConfigFile
173174
}
174175

175-
data, err := os.ReadFile(file) // nolint: gosec
176+
data, err := os.ReadFile(file) //nolint:gosec
176177
if err != nil {
177178
return godot.Settings{}, fmt.Errorf(
178-
"read config file %s: %v", defaultConfigFile, err,
179+
"read config file %s: %w", defaultConfigFile, err,
179180
)
180181
}
181182
if err := yaml.Unmarshal(data, &settings); err != nil {
182183
return godot.Settings{}, fmt.Errorf(
183-
"parse config file %s: %v", defaultConfigFile, err,
184+
"parse config file %s: %w", defaultConfigFile, err,
184185
)
185186
}
186187
return settings, nil

getters.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func newParsedFile(file *ast.File, fset *token.FileSet) (*parsedFile, error) {
4444
// from "go/format" won't help here if the original file is not gofmt-ed.
4545
pf.lines, err = readFile(file, fset)
4646
if err != nil {
47-
return nil, fmt.Errorf("read file: %v", err)
47+
return nil, fmt.Errorf("read file: %w", err)
4848
}
4949

5050
// Dirty hack. For some cases Go generates temporary files during
@@ -82,7 +82,7 @@ func (pf *parsedFile) getComments(scope Scope, exclude []*regexp.Regexp) []comme
8282
pf.getBlockComments(exclude),
8383
pf.getTopLevelComments(exclude)...,
8484
)
85-
default:
85+
case DeclScope:
8686
// Top level declaration comments and comments from the inside
8787
// of top level blocks
8888
comments = append(pf.getBlockComments(exclude), decl...)
@@ -118,7 +118,7 @@ func (pf *parsedFile) getBlockComments(exclude []*regexp.Regexp) []comment {
118118
// Skip comments that are not top-level for this block
119119
// (the block itself is top level, so comments inside this block
120120
// would be on column 2)
121-
// nolint: gomnd
121+
//nolint:gomnd
122122
if pf.fset.Position(c.Pos()).Column != 2 {
123123
continue
124124
}
@@ -136,7 +136,7 @@ func (pf *parsedFile) getBlockComments(exclude []*regexp.Regexp) []comment {
136136

137137
// getTopLevelComments gets all top level comments.
138138
func (pf *parsedFile) getTopLevelComments(exclude []*regexp.Regexp) []comment {
139-
var comments []comment // nolint: prealloc
139+
var comments []comment //nolint:prealloc
140140
for _, c := range pf.file.Comments {
141141
if c == nil || len(c.List) == 0 {
142142
continue
@@ -157,7 +157,7 @@ func (pf *parsedFile) getTopLevelComments(exclude []*regexp.Regexp) []comment {
157157

158158
// getDeclarationComments gets top level declaration comments.
159159
func (pf *parsedFile) getDeclarationComments(exclude []*regexp.Regexp) []comment {
160-
var comments []comment // nolint: prealloc
160+
var comments []comment //nolint:prealloc
161161
for _, decl := range pf.file.Decls {
162162
var cg *ast.CommentGroup
163163
switch d := decl.(type) {
@@ -184,7 +184,7 @@ func (pf *parsedFile) getDeclarationComments(exclude []*regexp.Regexp) []comment
184184

185185
// getAllComments gets every single comment from the file.
186186
func (pf *parsedFile) getAllComments(exclude []*regexp.Regexp) []comment {
187-
var comments []comment //nolint: prealloc
187+
var comments []comment //nolint:prealloc
188188
for _, c := range pf.file.Comments {
189189
if c == nil || len(c.List) == 0 {
190190
continue
@@ -205,6 +205,8 @@ func (pf *parsedFile) getAllComments(exclude []*regexp.Regexp) []comment {
205205
// special lines (e.g., tags or indented code examples), they are replaced
206206
// with `specialReplacer` to skip checks for them.
207207
// The result can be multiline.
208+
//
209+
//nolint:cyclop
208210
func getText(comment *ast.CommentGroup, exclude []*regexp.Regexp) (s string) {
209211
if len(comment.List) == 1 &&
210212
strings.HasPrefix(comment.List[0].Text, "/*") &&
@@ -246,7 +248,7 @@ func readFile(file *ast.File, fset *token.FileSet) ([]string, error) {
246248
fname := fset.File(file.Package)
247249
f, err := os.ReadFile(fname.Name())
248250
if err != nil {
249-
return nil, err
251+
return nil, err //nolint:wrapcheck
250252
}
251253
return strings.Split(string(f), "\n"), nil
252254
}

getters_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package godot
22

33
import (
4+
"errors"
45
"go/ast"
56
"go/parser"
67
"go/token"
@@ -82,7 +83,7 @@ func TestGetComments(t *testing.T) {
8283
if pf != nil {
8384
t.Fatalf("Unexpected file content")
8485
}
85-
if err != errUnsuitableInput {
86+
if !errors.Is(err, errUnsuitableInput) {
8687
t.Fatalf(
8788
"Unexpected error:\n expected: %v\n got: %v",
8889
errUnsuitableInput, err,

godot.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package godot
44

55
import (
6+
"errors"
67
"fmt"
78
"go/ast"
89
"go/token"
@@ -37,18 +38,18 @@ type comment struct {
3738
// Run runs this linter on the provided code.
3839
func Run(file *ast.File, fset *token.FileSet, settings Settings) ([]Issue, error) {
3940
pf, err := newParsedFile(file, fset)
40-
if err == errEmptyInput || err == errUnsuitableInput {
41+
if errors.Is(err, errEmptyInput) || errors.Is(err, errUnsuitableInput) {
4142
return nil, nil
4243
}
4344
if err != nil {
44-
return nil, fmt.Errorf("parse input file: %v", err)
45+
return nil, fmt.Errorf("parse input file: %w", err)
4546
}
4647

4748
exclude := make([]*regexp.Regexp, len(settings.Exclude))
4849
for i := 0; i < len(settings.Exclude); i++ {
4950
exclude[i], err = regexp.Compile(settings.Exclude[i])
5051
if err != nil {
51-
return nil, fmt.Errorf("invalid regexp: %v", err)
52+
return nil, fmt.Errorf("invalid regexp: %w", err)
5253
}
5354
}
5455

@@ -62,17 +63,17 @@ func Run(file *ast.File, fset *token.FileSet, settings Settings) ([]Issue, error
6263
// Fix fixes all issues and returns new version of file content.
6364
func Fix(path string, file *ast.File, fset *token.FileSet, settings Settings) ([]byte, error) {
6465
// Read file
65-
content, err := os.ReadFile(path) // nolint: gosec
66+
content, err := os.ReadFile(path) //nolint:gosec
6667
if err != nil {
67-
return nil, fmt.Errorf("read file: %v", err)
68+
return nil, fmt.Errorf("read file: %w", err)
6869
}
6970
if len(content) == 0 {
7071
return nil, nil
7172
}
7273

7374
issues, err := Run(file, fset, settings)
7475
if err != nil {
75-
return nil, fmt.Errorf("run linter: %v", err)
76+
return nil, fmt.Errorf("run linter: %w", err)
7677
}
7778

7879
// slice -> map
@@ -99,17 +100,17 @@ func Fix(path string, file *ast.File, fset *token.FileSet, settings Settings) ([
99100
func Replace(path string, file *ast.File, fset *token.FileSet, settings Settings) error {
100101
info, err := os.Stat(path)
101102
if err != nil {
102-
return fmt.Errorf("check file: %v", err)
103+
return fmt.Errorf("check file: %w", err)
103104
}
104105
mode := info.Mode()
105106

106107
fixed, err := Fix(path, file, fset, settings)
107108
if err != nil {
108-
return fmt.Errorf("fix issues: %v", err)
109+
return fmt.Errorf("fix issues: %w", err)
109110
}
110111

111112
if err := os.WriteFile(path, fixed, mode); err != nil {
112-
return fmt.Errorf("write file: %v", err)
113+
return fmt.Errorf("write file: %w", err)
113114
}
114115
return nil
115116
}

0 commit comments

Comments
 (0)