mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +02:00 
			
		
		
		
	removes type-checking from empty-block rule
This commit is contained in:
		| @@ -2,7 +2,6 @@ package rule | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"go/ast" | 	"go/ast" | ||||||
| 	"go/types" |  | ||||||
|  |  | ||||||
| 	"github.com/mgechev/revive/lint" | 	"github.com/mgechev/revive/lint" | ||||||
| ) | ) | ||||||
| @@ -18,12 +17,7 @@ func (r *EmptyBlockRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure | |||||||
| 		failures = append(failures, failure) | 		failures = append(failures, failure) | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	err := file.Pkg.TypeCheck() | 	w := lintEmptyBlock{make(map[*ast.BlockStmt]bool, 0), onFailure} | ||||||
| 	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 | ||||||
| } | } | ||||||
| @@ -35,7 +29,6 @@ func (r *EmptyBlockRule) Name() string { | |||||||
|  |  | ||||||
| type lintEmptyBlock struct { | type lintEmptyBlock struct { | ||||||
| 	ignore    map[*ast.BlockStmt]bool | 	ignore    map[*ast.BlockStmt]bool | ||||||
| 	pkg       *lint.Package |  | ||||||
| 	onFailure func(lint.Failure) | 	onFailure func(lint.Failure) | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -48,10 +41,14 @@ func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor { | |||||||
| 		w.ignore[n.Body] = true | 		w.ignore[n.Body] = true | ||||||
| 		return w | 		return w | ||||||
| 	case *ast.RangeStmt: | 	case *ast.RangeStmt: | ||||||
| 		t := w.pkg.TypeOf(n.X) | 		if len(n.Body.List) == 0 { | ||||||
|  | 			w.onFailure(lint.Failure{ | ||||||
| 		if _, ok := t.(*types.Chan); ok { | 				Confidence: 0.9, | ||||||
| 			w.ignore[n.Body] = true | 				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: | 	case *ast.BlockStmt: | ||||||
| 		if !w.ignore[n] && len(n.List) == 0 { | 		if !w.ignore[n] && len(n.List) == 0 { | ||||||
|   | |||||||
							
								
								
									
										4
									
								
								testdata/empty-block.go
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										4
									
								
								testdata/empty-block.go
									
									
									
									
										vendored
									
									
								
							| @@ -35,9 +35,9 @@ func g(f func() bool) { | |||||||
|  |  | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// issue 386 | 	// issue 386, then overwritten by issue 416 | ||||||
| 	var c = make(chan int) | 	var c = make(chan int) | ||||||
| 	for range c { | 	for range c { // MATCH /this block is empty, you can remove it/ | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	var s = "a string" | 	var s = "a string" | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user