mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +02:00 
			
		
		
		
	Adds new rule empty-block (#14)
* Adds rule superfluous-else (an extension of indent-error-flow) * initial (non functional) version * empty-block working version * adds tests for empty-block rule * Adds empty-block to the rules table * code clean-up
This commit is contained in:
		
							
								
								
									
										52
									
								
								rule/empty-block.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								rule/empty-block.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,52 @@ | ||||
| package rule | ||||
|  | ||||
| import ( | ||||
| 	"go/ast" | ||||
|  | ||||
| 	"github.com/mgechev/revive/lint" | ||||
| ) | ||||
|  | ||||
| // EmptyBlockRule lints given else constructs. | ||||
| type EmptyBlockRule struct{} | ||||
|  | ||||
| // Apply applies the rule to given file. | ||||
| func (r *EmptyBlockRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure { | ||||
| 	var failures []lint.Failure | ||||
|  | ||||
| 	onFailure := func(failure lint.Failure) { | ||||
| 		failures = append(failures, failure) | ||||
| 	} | ||||
|  | ||||
| 	w := lintEmptyBlock{make(map[*ast.IfStmt]bool), onFailure} | ||||
| 	ast.Walk(w, file.AST) | ||||
| 	return failures | ||||
| } | ||||
|  | ||||
| // Name returns the rule name. | ||||
| func (r *EmptyBlockRule) Name() string { | ||||
| 	return "empty-block" | ||||
| } | ||||
|  | ||||
| type lintEmptyBlock struct { | ||||
| 	ignore    map[*ast.IfStmt]bool | ||||
| 	onFailure func(lint.Failure) | ||||
| } | ||||
|  | ||||
| func (w lintEmptyBlock) Visit(node ast.Node) ast.Visitor { | ||||
| 	block, ok := node.(*ast.BlockStmt) | ||||
| 	if !ok { | ||||
| 		return w | ||||
| 	} | ||||
|  | ||||
| 	if len(block.List) == 0 { | ||||
| 		w.onFailure(lint.Failure{ | ||||
| 			Confidence: 1, | ||||
| 			Node:       block, | ||||
| 			Category:   "logic", | ||||
| 			URL:        "#empty-block", | ||||
| 			Failure:    "this block is empty, you can remove it", | ||||
| 		}) | ||||
| 	} | ||||
|  | ||||
| 	return w | ||||
| } | ||||
		Reference in New Issue
	
	Block a user