1
0
mirror of https://github.com/mgechev/revive.git synced 2025-01-08 03:13:27 +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

View File

@ -196,6 +196,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
| `cyclomatic` | int | Sets restriction for maximum Cyclomatic complexity. | no | no |
| `max-public-structs` | int | The maximum number of public structs in a file. | no | no |
| `file-header` | string | Header which each file should have. | no | no |
| `empty-block` | n/a | Warns on empty code blocks | no | no |
| `superfluous-else` | n/a | Prevents redundant else statements (extends `indent-error-flow`) | no | no |
## Available Formatters

View File

@ -47,6 +47,7 @@ var allRules = append([]lint.Rule{
&rule.ArgumentsLimitRule{},
&rule.CyclomaticRule{},
&rule.FileHeaderRule{},
&rule.EmptyBlockRule{},
}, defaultRules...)
var allFormatters = []lint.Formatter{

33
fixtures/empty-block.go Normal file
View File

@ -0,0 +1,33 @@
// Test of empty-blocks.
package fixtures
func f(x int) bool { // MATCH /this block is empty, you can remove it/
}
func g(f func() bool) string {
{ // MATCH /this block is empty, you can remove it/
}
if ok := f(); ok { // MATCH /this block is empty, you can remove it/
// only a comment
} else {
println("it's NOT empty!")
}
if ok := f(); ok {
println("it's NOT empty!")
} else { // MATCH /this block is empty, you can remove it/
}
for i := 0; i < 10; i++ { // MATCH /this block is empty, you can remove it/
}
for { // MATCH /this block is empty, you can remove it/
}
}

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
}

12
test/empty-block_test.go Normal file
View File

@ -0,0 +1,12 @@
package test
import (
"testing"
"github.com/mgechev/revive/rule"
)
// TestEmptyBlock rule.
func TestEmptyBlock(t *testing.T) {
testRule(t, "empty-block", &rule.EmptyBlockRule{})
}

View File

@ -12,3 +12,4 @@
[rule.range]
[rule.receiver-naming]
[rule.indent-error-flow]
[rule.empty-block]