Skip to content

Commit 7e2df19

Browse files
committed
feat: Stop linting arguments that are .Error() expressions
This pattern is distinct enough to not warant extra caution. It can now be used as an opt-out pattern to explicityly not wrap an error.
1 parent 361d7f2 commit 7e2df19

File tree

2 files changed

+3
-17
lines changed

2 files changed

+3
-17
lines changed

errorlint/lint.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func LintFmtErrorfCalls(fset *token.FileSet, info types.Info, multipleWraps bool
4747
args := call.Args[1:]
4848
wrapCount := 0
4949
for i := 0; i < len(args) && i < len(formatVerbs); i++ {
50-
if !implementsError(info.Types[args[i]].Type) && !isErrorStringCall(info, args[i]) {
50+
if !implementsError(info.Types[args[i]].Type) {
5151
continue
5252
}
5353
verb := formatVerbs[i]
@@ -78,20 +78,6 @@ func LintFmtErrorfCalls(fset *token.FileSet, info types.Info, multipleWraps bool
7878
return lints
7979
}
8080

81-
// isErrorStringCall tests whether the expression is a string expression that
82-
// is the result of an `(error).Error()` method call.
83-
func isErrorStringCall(info types.Info, expr ast.Expr) bool {
84-
if info.Types[expr].Type.String() == "string" {
85-
if call, ok := expr.(*ast.CallExpr); ok {
86-
if callSel, ok := call.Fun.(*ast.SelectorExpr); ok {
87-
fun := info.Uses[callSel.Sel].(*types.Func)
88-
return fun.Type().String() == "func() string" && fun.Name() == "Error"
89-
}
90-
}
91-
}
92-
return false
93-
}
94-
9581
// printfFormatStringVerbs returns a normalized list of all the verbs that are used per argument to
9682
// the printf function. The index of each returned element corresponds to the index of the
9783
// respective argument.

errorlint/testdata/src/fmterrorf/fmterrorf.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ func ErrorMultipleWrapsWithCustomError() error {
4343

4444
func ErrorStringFormat() error {
4545
err := errors.New("oops")
46-
return fmt.Errorf("error: %s", err.Error()) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
46+
return fmt.Errorf("error: %s", err.Error())
4747
}
4848

4949
func ErrorStringFormatCustomError() error {
5050
err := MyError{}
51-
return fmt.Errorf("error: %s", err.Error()) // want "non-wrapping format verb for fmt.Errorf. Use `%w` to format errors"
51+
return fmt.Errorf("error: %s", err.Error())
5252
}
5353

5454
func NotAnError() error {

0 commit comments

Comments
 (0)