1
0
mirror of https://github.com/mgechev/revive.git synced 2025-05-31 22:49:41 +02:00

fix: avoid false positive for blank identifier (#1376)

The fact that the error variable is not used is out of the scope of the rule

This pattern that can be found in benchmarks and examples.
It should be allowed.
This commit is contained in:
ccoVeille 2025-05-25 16:36:22 +02:00 committed by GitHub
parent a56cf7290e
commit a4737e935a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 1 deletions

View File

@ -61,6 +61,17 @@ func (w lintErrors) Visit(_ ast.Node) ast.Visitor {
}
id := spec.Names[0]
if id.Name == "_" {
// avoid false positive for blank identifier
// The fact that the error variable is not used
// is out of the scope of the rule
// This pattern that can be found in benchmarks and examples
// should be allowed.
continue
}
prefix := "err"
if id.IsExported() {
prefix = "Err"

View File

@ -19,9 +19,19 @@ var (
e1 = fmt.Errorf("blah %d", 4) // MATCH /error var e1 should have name of the form errFoo/
// E2 ...
E2 = fmt.Errorf("blah %d", 5) // MATCH /error var E2 should have name of the form ErrFoo/
// check there is no false positive for blank identifier.
// This pattern that can be found in benchmarks and examples should be allowed.
// The fact that the error variable is not used is out of the scope of this rule.
_ = errors.New("ok")
)
func f() {
var whatever = errors.New("ok") // ok
// the linter does not check local variables, this one is valid
var whatever = errors.New("ok")
_ = whatever
// same as above with a blank identifier
_ = errors.New("ok")
}