1
0
mirror of https://github.com/mgechev/revive.git synced 2025-10-30 23:37:49 +02:00

Adds new rule empty-block (#14)

* Adds rule superfluous-else (an extension of indent-error-flow)

* initial (non functional) version

* empty-block working version

* adds tests for empty-block rule

* Adds empty-block to the rules table

* code clean-up
This commit is contained in:
chavacava
2018-06-08 21:41:49 +02:00
committed by Minko Gechev
parent dc6767b811
commit 1fa5046357
6 changed files with 100 additions and 0 deletions

52
rule/empty-block.go Normal file
View File

@@ -0,0 +1,52 @@
package rule
import (
"go/ast"
"github.com/mgechev/revive/lint"
)
// EmptyBlockRule lints given else constructs.
type EmptyBlockRule struct{}
// Apply applies the rule to given file.
func (r *EmptyBlockRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
var failures []lint.Failure
onFailure := func(failure lint.Failure) {
failures = append(failures, failure)
}
w := lintEmptyBlock{make(map[*ast.IfStmt]bool), onFailure}
ast.Walk(w, file.AST)
return failures
}
// Name returns the rule name.
func (r *EmptyBlockRule) Name() string {
return "empty-block"
}
type lintEmptyBlock struct {
ignore map[*ast.IfStmt]bool
onFailure func(lint.Failure)
}
func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
block, ok := node.(*ast.BlockStmt)
if !ok {
return w
}
if len(block.List) == 0 {
w.onFailure(lint.Failure{
Confidence: 1,
Node: block,
Category: "logic",
URL: "#empty-block",
Failure: "this block is empty, you can remove it",
})
}
return w
}