mirror of
https://github.com/mgechev/revive.git
synced 2025-03-17 20:57:58 +02:00
empty-block: ignore checking blocks of funcs and func literals (#17)
* Adds rule superfluous-else (an extension of indent-error-flow) * Fix superfluous-else rule struct namming. * Adds superfuous-else rule to the rules table * Modifies empty-block rule to ignore bodies of func literals and funcs * add test cases on functions with a receiver for completeness
This commit is contained in:
parent
32df8ca880
commit
c9bde6c503
@ -2,14 +2,19 @@
|
||||
|
||||
package fixtures
|
||||
|
||||
func f(x int) bool { // MATCH /this block is empty, you can remove it/
|
||||
func f(x int) bool {} // Must not match
|
||||
|
||||
}
|
||||
type foo struct {}
|
||||
|
||||
func (f foo) f(x *int) string {} // Must not match
|
||||
func (f *foo) g(y *int) string {} // Must not match
|
||||
|
||||
func g(f func() bool) string {
|
||||
{ // MATCH /this block is empty, you can remove it/
|
||||
}
|
||||
|
||||
a := func(e error){} // Must not match
|
||||
|
||||
if ok := f(); ok { // MATCH /this block is empty, you can remove it/
|
||||
// only a comment
|
||||
} else {
|
||||
|
@ -17,7 +17,7 @@ func (r *EmptyBlockRule) Apply(file *lint.File, arguments lint.Arguments) []lint
|
||||
failures = append(failures, failure)
|
||||
}
|
||||
|
||||
w := lintEmptyBlock{make(map[*ast.IfStmt]bool), onFailure}
|
||||
w := lintEmptyBlock{make([]*ast.BlockStmt, 0), onFailure}
|
||||
ast.Walk(w, file.AST)
|
||||
return failures
|
||||
}
|
||||
@ -28,16 +28,32 @@ func (r *EmptyBlockRule) Name() string {
|
||||
}
|
||||
|
||||
type lintEmptyBlock struct {
|
||||
ignore map[*ast.IfStmt]bool
|
||||
ignore []*ast.BlockStmt
|
||||
onFailure func(lint.Failure)
|
||||
}
|
||||
|
||||
func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
|
||||
fd, ok := node.(*ast.FuncDecl)
|
||||
if ok {
|
||||
w.ignore = append(w.ignore, fd.Body)
|
||||
return w
|
||||
}
|
||||
|
||||
fl, ok := node.(*ast.FuncLit)
|
||||
if ok {
|
||||
w.ignore = append(w.ignore, fl.Body)
|
||||
return w
|
||||
}
|
||||
|
||||
block, ok := node.(*ast.BlockStmt)
|
||||
if !ok {
|
||||
return w
|
||||
}
|
||||
|
||||
if mustIgnore(block, w.ignore) {
|
||||
return w
|
||||
}
|
||||
|
||||
if len(block.List) == 0 {
|
||||
w.onFailure(lint.Failure{
|
||||
Confidence: 1,
|
||||
@ -50,3 +66,12 @@ func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
|
||||
|
||||
return w
|
||||
}
|
||||
|
||||
func mustIgnore(block *ast.BlockStmt, blackList []*ast.BlockStmt) bool {
|
||||
for _, b := range blackList {
|
||||
if b == block {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user