mirror of
https://github.com/mgechev/revive.git
synced 2025-11-25 22:12:38 +02:00
@@ -295,6 +295,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
|
|||||||
| [`call-to-gc`](./RULES_DESCRIPTIONS.md#call-to-gc) | n/a | Warns on explicit call to the garbage collector | no | no |
|
| [`call-to-gc`](./RULES_DESCRIPTIONS.md#call-to-gc) | n/a | Warns on explicit call to the garbage collector | no | no |
|
||||||
| [`duplicated-imports`](./RULES_DESCRIPTIONS#duplicated-imports) | n/a | Looks for packages that are imported two or more times | no | no |
|
| [`duplicated-imports`](./RULES_DESCRIPTIONS#duplicated-imports) | n/a | Looks for packages that are imported two or more times | no | no |
|
||||||
| [`import-shadowing`](./RULES_DESCRIPTIONS.md#import-shadowing) | n/a | Spots identifiers that shadow an import | no | no |
|
| [`import-shadowing`](./RULES_DESCRIPTIONS.md#import-shadowing) | n/a | Spots identifiers that shadow an import | no | no |
|
||||||
|
| [`bare-return`](./RULES_DESCRIPTIONS#bare-return) | n/a | Warns on bare returns | no | no |
|
||||||
|
|
||||||
## Configurable rules
|
## Configurable rules
|
||||||
|
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ List of all available rules.
|
|||||||
- [add-constant](#add-constant)
|
- [add-constant](#add-constant)
|
||||||
- [argument-limit](#argument-limit)
|
- [argument-limit](#argument-limit)
|
||||||
- [atomic](#atomic)
|
- [atomic](#atomic)
|
||||||
|
- [bare-return](#bare-return)
|
||||||
- [blank-imports](#blank-imports)
|
- [blank-imports](#blank-imports)
|
||||||
- [bool-literal-in-expr](#bool-literal-in-expr)
|
- [bool-literal-in-expr](#bool-literal-in-expr)
|
||||||
- [call-to-gc](#call-to-gc)
|
- [call-to-gc](#call-to-gc)
|
||||||
@@ -93,6 +94,12 @@ _Description_: Check for commonly mistaken usages of the `sync/atomic` package
|
|||||||
|
|
||||||
_Configuration_: N/A
|
_Configuration_: N/A
|
||||||
|
|
||||||
|
## bare-return
|
||||||
|
|
||||||
|
_Description_: Warns on bare (a.k.a. naked) returns
|
||||||
|
|
||||||
|
_Configuration_: N/A
|
||||||
|
|
||||||
## blank-imports
|
## blank-imports
|
||||||
|
|
||||||
_Description_: Blank import should be only in a main or test package, or have a comment justifying it.
|
_Description_: Blank import should be only in a main or test package, or have a comment justifying it.
|
||||||
|
|||||||
@@ -75,6 +75,7 @@ var allRules = append([]lint.Rule{
|
|||||||
&rule.CallToGCRule{},
|
&rule.CallToGCRule{},
|
||||||
&rule.DuplicatedImportsRule{},
|
&rule.DuplicatedImportsRule{},
|
||||||
&rule.ImportShadowingRule{},
|
&rule.ImportShadowingRule{},
|
||||||
|
&rule.BareReturnRule{},
|
||||||
}, defaultRules...)
|
}, defaultRules...)
|
||||||
|
|
||||||
var allFormatters = []lint.Formatter{
|
var allFormatters = []lint.Formatter{
|
||||||
|
|||||||
33
fixtures/bare-return.go
Normal file
33
fixtures/bare-return.go
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
package fixtures
|
||||||
|
|
||||||
|
func bare1() (int, int, error) {
|
||||||
|
go func() (a int) {
|
||||||
|
return // MATCH /avoid using bare returns, please add return expressions/
|
||||||
|
}(5)
|
||||||
|
}
|
||||||
|
|
||||||
|
func bare2(a, b int) (int, error, int) {
|
||||||
|
defer func() (a int) {
|
||||||
|
return // MATCH /avoid using bare returns, please add return expressions/
|
||||||
|
}(5)
|
||||||
|
}
|
||||||
|
|
||||||
|
func bare3(a string, b int) (a int, b float32, c string, d string) {
|
||||||
|
go func() (a int, b int) {
|
||||||
|
return a, b
|
||||||
|
}(5, 6)
|
||||||
|
|
||||||
|
defer func() (a int) {
|
||||||
|
return a
|
||||||
|
}(5)
|
||||||
|
|
||||||
|
return // MATCH /avoid using bare returns, please add return expressions/
|
||||||
|
}
|
||||||
|
|
||||||
|
func bare4(a string, b int) string {
|
||||||
|
return a
|
||||||
|
}
|
||||||
|
|
||||||
|
func bare5(a string, b int) {
|
||||||
|
return
|
||||||
|
}
|
||||||
77
rule/bare-return.go
Normal file
77
rule/bare-return.go
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
package rule
|
||||||
|
|
||||||
|
import (
|
||||||
|
"go/ast"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/lint"
|
||||||
|
)
|
||||||
|
|
||||||
|
// BareReturnRule lints given else constructs.
|
||||||
|
type BareReturnRule struct{}
|
||||||
|
|
||||||
|
// Apply applies the rule to given file.
|
||||||
|
func (r *BareReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
|
||||||
|
var failures []lint.Failure
|
||||||
|
|
||||||
|
onFailure := func(failure lint.Failure) {
|
||||||
|
failures = append(failures, failure)
|
||||||
|
}
|
||||||
|
|
||||||
|
w := lintBareReturnRule{onFailure: onFailure}
|
||||||
|
ast.Walk(w, file.AST)
|
||||||
|
return failures
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name returns the rule name.
|
||||||
|
func (r *BareReturnRule) Name() string {
|
||||||
|
return "bare-return"
|
||||||
|
}
|
||||||
|
|
||||||
|
type lintBareReturnRule struct {
|
||||||
|
onFailure func(lint.Failure)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w lintBareReturnRule) Visit(node ast.Node) ast.Visitor {
|
||||||
|
switch n := node.(type) {
|
||||||
|
case *ast.FuncDecl:
|
||||||
|
w.checkFunc(n.Type.Results, n.Body)
|
||||||
|
case *ast.FuncLit: // to cope with deferred functions and go-routines
|
||||||
|
w.checkFunc(n.Type.Results, n.Body)
|
||||||
|
}
|
||||||
|
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkFunc will verify if the given function has named result and bare returns
|
||||||
|
func (w lintBareReturnRule) checkFunc(results *ast.FieldList, body *ast.BlockStmt) {
|
||||||
|
hasNamedResults := results != nil && len(results.List) > 0 && results.List[0].Names != nil
|
||||||
|
if !hasNamedResults || body == nil {
|
||||||
|
return // nothing to do
|
||||||
|
}
|
||||||
|
|
||||||
|
brf := bareReturnFinder{w.onFailure}
|
||||||
|
ast.Walk(brf, body)
|
||||||
|
}
|
||||||
|
|
||||||
|
type bareReturnFinder struct {
|
||||||
|
onFailure func(lint.Failure)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w bareReturnFinder) Visit(node ast.Node) ast.Visitor {
|
||||||
|
rs, ok := node.(*ast.ReturnStmt)
|
||||||
|
if !ok {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(rs.Results) > 0 {
|
||||||
|
return w
|
||||||
|
}
|
||||||
|
|
||||||
|
w.onFailure(lint.Failure{
|
||||||
|
Confidence: 1,
|
||||||
|
Node: rs,
|
||||||
|
Failure: "avoid using bare returns, please add return expressions",
|
||||||
|
})
|
||||||
|
|
||||||
|
return w
|
||||||
|
}
|
||||||
11
test/bare-return_test.go
Normal file
11
test/bare-return_test.go
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/mgechev/revive/rule"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestBareReturn(t *testing.T) {
|
||||||
|
testRule(t, "bare-return", &rule.BareReturnRule{})
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user