1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-15 01:04:40 +02:00

fix issue 386: Incorrectly identifies channel draining as "empty code… (#415)

* fix issue 386: Incorrectly identifies channel draining as "empty code block"

* updates doc of empty-block rule
This commit is contained in:
SalvadorC
2020-05-11 02:43:56 +02:00
committed by GitHub
parent 1b8a7aa7cd
commit 1da965b65f
3 changed files with 44 additions and 43 deletions

View File

@ -316,7 +316,7 @@ List of all available rules. The rules ported from `golint` are left unchanged a
| [`cyclomatic`](./RULES_DESCRIPTIONS.md#cyclomatic) | int | Sets restriction for maximum Cyclomatic complexity. | no | no | | [`cyclomatic`](./RULES_DESCRIPTIONS.md#cyclomatic) | int | Sets restriction for maximum Cyclomatic complexity. | no | no |
| [`max-public-structs`](./RULES_DESCRIPTIONS.md#max-public-structs) | int | The maximum number of public structs in a file. | no | no | | [`max-public-structs`](./RULES_DESCRIPTIONS.md#max-public-structs) | int | The maximum number of public structs in a file. | no | no |
| [`file-header`](./RULES_DESCRIPTIONS.md#file-header) | string | Header which each file should have. | no | no | | [`file-header`](./RULES_DESCRIPTIONS.md#file-header) | string | Header which each file should have. | no | no |
| [`empty-block`](./RULES_DESCRIPTIONS.md#empty-block) | n/a | Warns on empty code blocks | no | no | | [`empty-block`](./RULES_DESCRIPTIONS.md#empty-block) | n/a | Warns on empty code blocks | no | yes |
| [`superfluous-else`](./RULES_DESCRIPTIONS.md#superfluous-else) | n/a | Prevents redundant else statements (extends [`indent-error-flow`](./RULES_DESCRIPTIONS.md#indent-error-flow)) | no | no | | [`superfluous-else`](./RULES_DESCRIPTIONS.md#superfluous-else) | n/a | Prevents redundant else statements (extends [`indent-error-flow`](./RULES_DESCRIPTIONS.md#indent-error-flow)) | no | no |
| [`confusing-naming`](./RULES_DESCRIPTIONS.md#confusing-naming) | n/a | Warns on methods with names that differ only by capitalization | no | no | | [`confusing-naming`](./RULES_DESCRIPTIONS.md#confusing-naming) | n/a | Warns on methods with names that differ only by capitalization | no | no |
| [`get-return`](./RULES_DESCRIPTIONS.md#get-return) | n/a | Warns on getters that do not yield any result | no | no | | [`get-return`](./RULES_DESCRIPTIONS.md#get-return) | n/a | Warns on getters that do not yield any result | no | no |

View File

@ -2,6 +2,7 @@ package rule
import ( import (
"go/ast" "go/ast"
"go/types"
"github.com/mgechev/revive/lint" "github.com/mgechev/revive/lint"
) )
@ -17,7 +18,12 @@ func (r *EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure
failures = append(failures, failure) failures = append(failures, failure)
} }
w := lintEmptyBlock{make([]*ast.BlockStmt, 0), onFailure} err := file.Pkg.TypeCheck()
if err != nil {
panic("unable to type check " + file.Name + ":" + err.Error())
}
w := lintEmptyBlock{make(map[*ast.BlockStmt]bool, 0), file.Pkg, onFailure}
ast.Walk(w, file.AST) ast.Walk(w, file.AST)
return failures return failures
} }
@ -28,49 +34,35 @@ func (r *EmptyBlockRule) Name() string {
} }
type lintEmptyBlock struct { type lintEmptyBlock struct {
ignore []*ast.BlockStmt ignore map[*ast.BlockStmt]bool
pkg *lint.Package
onFailure func(lint.Failure) onFailure func(lint.Failure)
} }
func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor { func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
fd, ok := node.(*ast.FuncDecl) switch n := node.(type) {
if ok { case *ast.FuncDecl:
w.ignore = append(w.ignore, fd.Body) w.ignore[n.Body] = true
return w return w
} case *ast.FuncLit:
w.ignore[n.Body] = true
fl, ok := node.(*ast.FuncLit)
if ok {
w.ignore = append(w.ignore, fl.Body)
return w return w
} case *ast.RangeStmt:
t := w.pkg.TypeOf(n.X)
block, ok := node.(*ast.BlockStmt) if _, ok := t.(*types.Chan); ok {
if !ok { w.ignore[n.Body] = true
return w
} }
case *ast.BlockStmt:
if mustIgnore(block, w.ignore) { if !w.ignore[n] && len(n.List) == 0 {
return w
}
if len(block.List) == 0 {
w.onFailure(lint.Failure{ w.onFailure(lint.Failure{
Confidence: 1, Confidence: 1,
Node: block, Node: n,
Category: "logic", Category: "logic",
Failure: "this block is empty, you can remove it", Failure: "this block is empty, you can remove it",
}) })
} }
}
return w return w
} }
func mustIgnore(block *ast.BlockStmt, blackList []*ast.BlockStmt) bool {
for _, b := range blackList {
if b == block {
return true
}
}
return false
}

View File

@ -2,18 +2,18 @@
package fixtures package fixtures
func f(x int) bool {} // Must not match func f(x int) {} // Must not match
type foo struct{} type foo struct{}
func (f foo) f(x *int) string {} // Must not match func (f foo) f(x *int) {} // Must not match
func (f *foo) g(y *int) string {} // Must not match func (f *foo) g(y *int) {} // Must not match
func g(f func() bool) string { func g(f func() bool) {
{ // MATCH /this block is empty, you can remove it/ { // MATCH /this block is empty, you can remove it/
} }
a := func(e error){} // Must not match _ = func(e error) {} // Must not match
if ok := f(); ok { // 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 // only a comment
@ -35,4 +35,13 @@ func g(f func() bool) string {
} }
// issue 386
var c = make(chan int)
for range c {
}
var s = "a string"
for range s { // MATCH /this block is empty, you can remove it/
}
} }