This repository has been archived by the owner on May 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 492
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lint: avoid false positives with custom errors-package
When using `errors.New(fmt.Sprintf(...))`, lint will alert that you should use `fmt.Errorf(...)`. Before this patch, this alert was also displayed when using a custom errors-package. There are valid use cases to use `errors.New(fmt.Sprintf(...))` in a custom errors-package context. This patch avoids the "false positive" alert when a custom errors-package is imported in the current file. Fixes #350 Change-Id: I7cc82a3435b184f8b4cad0752a75d44f33536dce GitHub-Last-Rev: ad257d2 GitHub-Pull-Request: #360 Reviewed-on: https://go-review.googlesource.com/96091 Reviewed-by: Andrew Bonventre <andybons@golang.org>
- Loading branch information
1 parent
fb4f8c1
commit c72d1a5
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Test for allowed errors.New(fmt.Sprintf()) when a custom errors package is imported. | ||
|
||
// Package foo ... | ||
package foo | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func f(x int) error { | ||
if x > 10 { | ||
return errors.New(fmt.Sprintf("something %d", x)) // OK | ||
} | ||
if x > 5 { | ||
return errors.New(g("blah")) // OK | ||
} | ||
if x > 4 { | ||
return errors.New("something else") // OK | ||
} | ||
return nil | ||
} | ||
|
||
func g(s string) string { return "prefix: " + s } |