Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

removes type-checking from empty-block rule #425

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions rule/empty-block.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package rule

import (
"go/ast"
"go/types"

"github.com/mgechev/revive/lint"
)
Expand All @@ -18,12 +17,7 @@ func (r *EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure
failures = append(failures, failure)
}

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}
w := lintEmptyBlock{make(map[*ast.BlockStmt]bool, 0), onFailure}
ast.Walk(w, file.AST)
return failures
}
Expand All @@ -35,7 +29,6 @@ func (r *EmptyBlockRule) Name() string {

type lintEmptyBlock struct {
ignore map[*ast.BlockStmt]bool
pkg *lint.Package
onFailure func(lint.Failure)
}

Expand All @@ -48,10 +41,14 @@ func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor {
w.ignore[n.Body] = true
return w
case *ast.RangeStmt:
t := w.pkg.TypeOf(n.X)

if _, ok := t.(*types.Chan); ok {
w.ignore[n.Body] = true
if len(n.Body.List) == 0 {
w.onFailure(lint.Failure{
Confidence: 0.9,
Node: n,
Category: "logic",
Failure: "this block is empty, you can remove it",
})
return nil // skip visiting the range subtree (it will produce a duplicated failure)
}
case *ast.BlockStmt:
if !w.ignore[n] && len(n.List) == 0 {
Expand Down
4 changes: 2 additions & 2 deletions testdata/empty-block.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ func g(f func() bool) {

}

// issue 386
// issue 386, then overwritten by issue 416
var c = make(chan int)
for range c {
for range c { // MATCH /this block is empty, you can remove it/
}

var s = "a string"
Expand Down