mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +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:
		| @@ -2,6 +2,7 @@ package rule | ||||
|  | ||||
| import ( | ||||
| 	"go/ast" | ||||
| 	"go/types" | ||||
|  | ||||
| 	"github.com/mgechev/revive/lint" | ||||
| ) | ||||
| @@ -17,7 +18,12 @@ func (r *EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) []lint.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) | ||||
| 	return failures | ||||
| } | ||||
| @@ -28,49 +34,35 @@ func (r *EmptyBlockRule) Name() string { | ||||
| } | ||||
|  | ||||
| type lintEmptyBlock struct { | ||||
| 	ignore    []*ast.BlockStmt | ||||
| 	ignore    map[*ast.BlockStmt]bool | ||||
| 	pkg       *lint.Package | ||||
| 	onFailure func(lint.Failure) | ||||
| } | ||||
|  | ||||
| func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor { | ||||
| 	fd, ok := node.(*ast.FuncDecl) | ||||
| 	if ok { | ||||
| 		w.ignore = append(w.ignore, fd.Body) | ||||
| 	switch n := node.(type) { | ||||
| 	case *ast.FuncDecl: | ||||
| 		w.ignore[n.Body] = true | ||||
| 		return w | ||||
| 	} | ||||
|  | ||||
| 	fl, ok := node.(*ast.FuncLit) | ||||
| 	if ok { | ||||
| 		w.ignore = append(w.ignore, fl.Body) | ||||
| 	case *ast.FuncLit: | ||||
| 		w.ignore[n.Body] = true | ||||
| 		return w | ||||
| 	} | ||||
| 	case *ast.RangeStmt: | ||||
| 		t := w.pkg.TypeOf(n.X) | ||||
|  | ||||
| 	block, ok := node.(*ast.BlockStmt) | ||||
| 	if !ok { | ||||
| 		return w | ||||
| 	} | ||||
|  | ||||
| 	if mustIgnore(block, w.ignore) { | ||||
| 		return w | ||||
| 	} | ||||
|  | ||||
| 	if len(block.List) == 0 { | ||||
| 		w.onFailure(lint.Failure{ | ||||
| 			Confidence: 1, | ||||
| 			Node:       block, | ||||
| 			Category:   "logic", | ||||
| 			Failure:    "this block is empty, you can remove it", | ||||
| 		}) | ||||
| 		if _, ok := t.(*types.Chan); ok { | ||||
| 			w.ignore[n.Body] = true | ||||
| 		} | ||||
| 	case *ast.BlockStmt: | ||||
| 		if !w.ignore[n] && len(n.List) == 0 { | ||||
| 			w.onFailure(lint.Failure{ | ||||
| 				Confidence: 1, | ||||
| 				Node:       n, | ||||
| 				Category:   "logic", | ||||
| 				Failure:    "this block is empty, you can remove it", | ||||
| 			}) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return w | ||||
| } | ||||
|  | ||||
| func mustIgnore(block *ast.BlockStmt, blackList []*ast.BlockStmt) bool { | ||||
| 	for _, b := range blackList { | ||||
| 		if b == block { | ||||
| 			return true | ||||
| 		} | ||||
| 	} | ||||
| 	return false | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user