From 1fa5046357720e4c3f87771d0f695796cf9f52c4 Mon Sep 17 00:00:00 2001 From: chavacava Date: Fri, 8 Jun 2018 21:41:49 +0200 Subject: [PATCH] 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 --- README.md | 1 + config.go | 1 + fixtures/empty-block.go | 33 +++++++++++++++++++++++++ rule/empty-block.go | 52 ++++++++++++++++++++++++++++++++++++++++ test/empty-block_test.go | 12 ++++++++++ untyped.toml | 1 + 6 files changed, 100 insertions(+) create mode 100644 fixtures/empty-block.go create mode 100644 rule/empty-block.go create mode 100644 test/empty-block_test.go diff --git a/README.md b/README.md index c316788..561d01f 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/config.go b/config.go index 52b629c..351a55a 100644 --- a/config.go +++ b/config.go @@ -47,6 +47,7 @@ var allRules = append([]lint.Rule{ &rule.ArgumentsLimitRule{}, &rule.CyclomaticRule{}, &rule.FileHeaderRule{}, + &rule.EmptyBlockRule{}, }, defaultRules...) var allFormatters = []lint.Formatter{ diff --git a/fixtures/empty-block.go b/fixtures/empty-block.go new file mode 100644 index 0000000..79b9dde --- /dev/null +++ b/fixtures/empty-block.go @@ -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/ + + } + +} diff --git a/rule/empty-block.go b/rule/empty-block.go new file mode 100644 index 0000000..11e4af3 --- /dev/null +++ b/rule/empty-block.go @@ -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 +} diff --git a/test/empty-block_test.go b/test/empty-block_test.go new file mode 100644 index 0000000..1de0680 --- /dev/null +++ b/test/empty-block_test.go @@ -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{}) +} diff --git a/untyped.toml b/untyped.toml index 67307bb..5c7ff44 100644 --- a/untyped.toml +++ b/untyped.toml @@ -12,3 +12,4 @@ [rule.range] [rule.receiver-naming] [rule.indent-error-flow] +[rule.empty-block]