Skip to content

Commit a7865d2

Browse files
authored
Update errorlint to HEAD (#1933)
1 parent 34ffdc2 commit a7865d2

12 files changed

+153
-86
lines changed

.golangci.example.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ linters-settings:
118118
exclude: /path/to/file.txt
119119

120120
errorlint:
121-
# Report non-wrapping error creation using fmt.Errorf
121+
# Check whether fmt.Errorf uses the %w verb for formatting errors. See the readme for caveats
122122
errorf: true
123+
# Check for plain type assertions and type switches
124+
asserts: true
125+
# Check for plain error comparisons
126+
comparison: true
123127

124128
exhaustive:
125129
# check switch statements in generated files also

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ require (
5757
github.com/nishanths/exhaustive v0.1.0
5858
github.com/nishanths/predeclared v0.2.1
5959
github.com/pkg/errors v0.9.1
60-
github.com/polyfloyd/go-errorlint v0.0.0-20201127212506-19bd8db6546f
60+
github.com/polyfloyd/go-errorlint v0.0.0-20210418123303-74da32850375
6161
github.com/ryancurrah/gomodguard v1.2.0
6262
github.com/ryanrolds/sqlclosecheck v0.3.0
6363
github.com/sanposhiho/wastedassign v0.2.0

go.sum

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ var defaultLintersSettings = LintersSettings{
6262
ExtraRules: false,
6363
},
6464
ErrorLint: ErrorLintSettings{
65-
Errorf: true,
65+
Errorf: true,
66+
Asserts: true,
67+
Comparison: true,
6668
},
6769
Ifshort: IfshortSettings{
6870
MaxDeclLines: 1,
@@ -165,7 +167,9 @@ type ErrcheckSettings struct {
165167
}
166168

167169
type ErrorLintSettings struct {
168-
Errorf bool `mapstructure:"errorf"`
170+
Errorf bool `mapstructure:"errorf"`
171+
Asserts bool `mapstructure:"asserts"`
172+
Comparison bool `mapstructure:"comparison"`
169173
}
170174

171175
type ExhaustiveSettings struct {

pkg/golinters/errorlint.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,21 @@ import (
1010

1111
func NewErrorLint(cfg *config.ErrorLintSettings) *goanalysis.Linter {
1212
a := errorlint.NewAnalyzer()
13+
1314
cfgMap := map[string]map[string]interface{}{}
15+
1416
if cfg != nil {
1517
cfgMap[a.Name] = map[string]interface{}{
16-
"errorf": cfg.Errorf,
18+
"errorf": cfg.Errorf,
19+
"asserts": cfg.Asserts,
20+
"comparison": cfg.Comparison,
1721
}
1822
}
23+
1924
return goanalysis.NewLinter(
20-
"errorlint",
21-
"go-errorlint is a source code linter for Go software "+
22-
"that can be used to find code that will cause problems "+
23-
"with the error wrapping scheme introduced in Go 1.13.",
25+
a.Name,
26+
"errorlint is a linter for that can be used to find code "+
27+
"that will cause problems with the error wrapping scheme introduced in Go 1.13.",
2428
[]*analysis.Analyzer{a},
2529
cfgMap,
2630
).WithLoadMode(goanalysis.LoadModeTypesInfo)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters-settings:
2+
errorlint:
3+
errorf: false
4+
asserts: true
5+
comparison: false
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters-settings:
2+
errorlint:
3+
errorf: false
4+
asserts: false
5+
comparison: true
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters-settings:
2+
errorlint:
3+
errorf: true
4+
asserts: false
5+
comparison: false

test/testdata/errorlint.go

Lines changed: 14 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,88 +3,29 @@ package testdata
33

44
import (
55
"errors"
6+
"fmt"
67
"log"
78
)
89

9-
var errFoo = errors.New("foo")
10+
var errLintFoo = errors.New("foo")
1011

11-
func doThing() error {
12-
return errFoo
13-
}
12+
type errLintBar struct{}
1413

15-
func compare() {
16-
err := doThing()
17-
if errors.Is(err, errFoo) {
18-
log.Println("ErrFoo")
19-
}
20-
if err == nil {
21-
log.Println("nil")
22-
}
23-
if err != nil {
24-
log.Println("nil")
25-
}
26-
if nil == err {
27-
log.Println("nil")
28-
}
29-
if nil != err {
30-
log.Println("nil")
31-
}
32-
if err == errFoo { // ERROR "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error"
33-
log.Println("errFoo")
34-
}
35-
if err != errFoo { // ERROR "comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error"
36-
log.Println("not errFoo")
37-
}
38-
if errFoo == err { // ERROR "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error"
39-
log.Println("errFoo")
40-
}
41-
if errFoo != err { // ERROR "comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error"
42-
log.Println("not errFoo")
43-
}
44-
switch err { // ERROR "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors"
45-
case errFoo:
46-
log.Println("errFoo")
47-
}
48-
switch doThing() { // ERROR "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors"
49-
case errFoo:
50-
log.Println("errFoo")
51-
}
14+
func (*errLintBar) Error() string {
15+
return "bar"
5216
}
5317

54-
type myError struct{}
55-
56-
func (*myError) Error() string {
57-
return "foo"
58-
}
18+
func errorLintAll() {
19+
err := func() error { return nil }()
20+
if err == errLintFoo { // ERROR "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error"
21+
log.Println("errCompare")
22+
}
5923

60-
func doAnotherThing() error {
61-
return &myError{}
62-
}
24+
err = errors.New("oops")
25+
fmt.Errorf("error: %v", err) // ERROR "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
6326

64-
func typeCheck() {
65-
err := doAnotherThing()
66-
var me *myError
67-
if errors.As(err, &me) {
68-
log.Println("myError")
69-
}
70-
_, ok := err.(*myError) // ERROR "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors"
71-
if ok {
72-
log.Println("myError")
73-
}
7427
switch err.(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
75-
case *myError:
76-
log.Println("myError")
77-
}
78-
switch doAnotherThing().(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
79-
case *myError:
80-
log.Println("myError")
81-
}
82-
switch t := err.(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
83-
case *myError:
84-
log.Println("myError", t)
85-
}
86-
switch t := doAnotherThing().(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
87-
case *myError:
88-
log.Println("myError", t)
28+
case *errLintBar:
29+
log.Println("errLintBar")
8930
}
9031
}

test/testdata/errorlint_asserts.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//args: -Eerrorlint
2+
//config_path: testdata/configs/errorlint_asserts.yml
3+
package testdata
4+
5+
import (
6+
"errors"
7+
"log"
8+
)
9+
10+
type myError struct{}
11+
12+
func (*myError) Error() string {
13+
return "foo"
14+
}
15+
16+
func errorLintDoAnotherThing() error {
17+
return &myError{}
18+
}
19+
20+
func errorLintAsserts() {
21+
err := errorLintDoAnotherThing()
22+
var me *myError
23+
if errors.As(err, &me) {
24+
log.Println("myError")
25+
}
26+
_, ok := err.(*myError) // ERROR "type assertion on error will fail on wrapped errors. Use errors.As to check for specific errors"
27+
if ok {
28+
log.Println("myError")
29+
}
30+
switch err.(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
31+
case *myError:
32+
log.Println("myError")
33+
}
34+
switch errorLintDoAnotherThing().(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
35+
case *myError:
36+
log.Println("myError")
37+
}
38+
switch t := err.(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
39+
case *myError:
40+
log.Println("myError", t)
41+
}
42+
switch t := errorLintDoAnotherThing().(type) { // ERROR "type switch on error will fail on wrapped errors. Use errors.As to check for specific errors"
43+
case *myError:
44+
log.Println("myError", t)
45+
}
46+
}

test/testdata/errorlint_comparison.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//args: -Eerrorlint
2+
//config_path: testdata/configs/errorlint_comparison.yml
3+
package testdata
4+
5+
import (
6+
"errors"
7+
"log"
8+
)
9+
10+
var errCompare = errors.New("foo")
11+
12+
func errorLintDoThing() error {
13+
return errCompare
14+
}
15+
16+
func errorLintComparison() {
17+
err := errorLintDoThing()
18+
if errors.Is(err, errCompare) {
19+
log.Println("errCompare")
20+
}
21+
if err == nil {
22+
log.Println("nil")
23+
}
24+
if err != nil {
25+
log.Println("nil")
26+
}
27+
if nil == err {
28+
log.Println("nil")
29+
}
30+
if nil != err {
31+
log.Println("nil")
32+
}
33+
if err == errCompare { // ERROR "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error"
34+
log.Println("errCompare")
35+
}
36+
if err != errCompare { // ERROR "comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error"
37+
log.Println("not errCompare")
38+
}
39+
if errCompare == err { // ERROR "comparing with == will fail on wrapped errors. Use errors.Is to check for a specific error"
40+
log.Println("errCompare")
41+
}
42+
if errCompare != err { // ERROR "comparing with != will fail on wrapped errors. Use errors.Is to check for a specific error"
43+
log.Println("not errCompare")
44+
}
45+
switch err { // ERROR "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors"
46+
case errCompare:
47+
log.Println("errCompare")
48+
}
49+
switch errorLintDoThing() { // ERROR "switch on an error will fail on wrapped errors. Use errors.Is to check for specific errors"
50+
case errCompare:
51+
log.Println("errCompare")
52+
}
53+
}

test/testdata/errorlint_errorf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//args: -Eerrorlint
2-
//config: linters-settings.errorlint.errorf=true
2+
//config_path: testdata/configs/errorlint_errorf.yml
33
package testdata
44

55
import (
@@ -13,7 +13,7 @@ func (customError) Error() string {
1313
return "oops"
1414
}
1515

16-
func wraps() {
16+
func errorLintErrorf() {
1717
err := errors.New("oops")
1818
fmt.Errorf("error: %w", err)
1919
fmt.Errorf("error: %v", err) // ERROR "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"

0 commit comments

Comments
 (0)