Repository: mgechev/revive (go)

Issue:
panic: unable to type check
**Describe the bug**
 We've seen a transient panic in revive in our CI job (it did not occur when the job was retried).

```
revive -exclude vendor/... -formatter friendly -config .revive.toml ./...
 panic: unable to type check [redacted file A]:[redacted file A]:3:8: could not import [redacted pkg B] (can't find import: [redacted pkg B])
 goroutine 123 [running]:
 github.com/mgechev/revive/rule.(*EmptyBlockRule).Apply(0x9e06d0, 0xc000f70b80, 0x0, 0x0, 0x0, 0x9e06d0, 0x0, 0x0)
    /go/src/github.com/mgechev/revive/rule/empty-block.go:23 +0x20e
 github.com/mgechev/revive/lint.(*File).lint(0xc000f70b80, 0xc0003b6800, 0x27, 0x40, 0x0, 0x3fd0000000000000, 0xc000030a40, 0x7, 0xc00007dfb0, 0x1, ...)
    /go/src/github.com/mgechev/revive/lint/file.go:108 +0x3b6
 github.com/mgechev/revive/lint.(*Package).lint.func1(0xc0003b6800, 0x27, 0x40, 0x0, 0x3fd0000000000000, 0xc000030a40, 0x7, 0xc00007dfb0, 0x1, 0x1, ...)
    /go/src/github.com/mgechev/revive/lint/package.go:173 +0xb5
 created by github.com/mgechev/revive/lint.(*Package).lint
    /go/src/github.com/mgechev/revive/lint/package.go:172 +0x179
```

https://github.com/mgechev/revive/blob/1da965b65f8a0e3aa83cd9d6d38153a32f308960/rule/empty-block.go#L23

**To Reproduce**

I couldn't reproduce the issue - it worked when I retried the job. Could it have been caused by some network issue when getting the package from our internal git repo?

**Expected behavior**
Expected to see better error message describing why it couldn't find the import.

**Desktop (please complete the following information):**
 - Version of Go: 1.14.2

Pull request description:
removes type-checking from empty-block rule
Closes #416 

Updated tests but as described in #424 we are currently incapable of testing on confidence level.
I've did some manual testing.

```
# set confidence = 1 in config, then
$ ./revive -config ./defaults.toml testdata/empty-block.go 
testdata/empty-block.go:13:2: this block is empty, you can remove it
testdata/empty-block.go:18:19: this block is empty, you can remove it
testdata/empty-block.go:26:9: this block is empty, you can remove it
testdata/empty-block.go:30:26: this block is empty, you can remove it
testdata/empty-block.go:34:6: this block is empty, you can remove it

# set confidence = 0.9 in config, then
$ ./revive -config ./defaults.toml testdata/empty-block.go 
testdata/empty-block.go:13:2: this block is empty, you can remove it
testdata/empty-block.go:18:19: this block is empty, you can remove it
testdata/empty-block.go:26:9: this block is empty, you can remove it
testdata/empty-block.go:30:26: this block is empty, you can remove it
testdata/empty-block.go:34:6: this block is empty, you can remove it
testdata/empty-block.go:40:2: this block is empty, you can remove it
testdata/empty-block.go:44:2: this block is empty, you can remove it
```


Patch:
diff --git a/rule/empty-block.go b/rule/empty-block.go
index 4eb0ce8..fbec4d9 100644
--- a/rule/empty-block.go
+++ b/rule/empty-block.go
@@ -2,7 +2,6 @@ package rule
 
 import (
 	"go/ast"
-	"go/types"
 
 	"github.com/mgechev/revive/lint"
 )
@@ -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
 }
@@ -35,7 +29,6 @@ func (r *EmptyBlockRule) Name() string {
 
 type lintEmptyBlock struct {
 	ignore    map[*ast.BlockStmt]bool
-	pkg       *lint.Package
 	onFailure func(lint.Failure)
 }
 
@@ -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 {


Test patch:
diff --git a/testdata/empty-block.go b/testdata/empty-block.go
index 81b7262..d750157 100644
--- a/testdata/empty-block.go
+++ b/testdata/empty-block.go
@@ -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"