mirror of
				https://github.com/mgechev/revive.git
				synced 2025-10-30 23:37:49 +02:00 
			
		
		
		
	refactor (rule/get-return): replace AST walker by iteration over declarations (#1168)
This commit is contained in:
		| @@ -15,12 +15,28 @@ type GetReturnRule struct{} | ||||
| func (*GetReturnRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure { | ||||
| 	var failures []lint.Failure | ||||
|  | ||||
| 	onFailure := func(failure lint.Failure) { | ||||
| 		failures = append(failures, failure) | ||||
| 	for _, decl := range file.AST.Decls { | ||||
| 		fd, ok := decl.(*ast.FuncDecl) | ||||
| 		if !ok { | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		if !isGetter(fd.Name.Name) { | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		if hasResults(fd.Type.Results) { | ||||
| 			continue | ||||
| 		} | ||||
|  | ||||
| 		failures = append(failures, lint.Failure{ | ||||
| 			Confidence: 0.8, | ||||
| 			Node:       fd, | ||||
| 			Category:   "logic", | ||||
| 			Failure:    fmt.Sprintf("function '%s' seems to be a getter but it does not return any result", fd.Name.Name), | ||||
| 		}) | ||||
| 	} | ||||
|  | ||||
| 	w := lintReturnRule{onFailure} | ||||
| 	ast.Walk(w, file.AST) | ||||
| 	return failures | ||||
| } | ||||
|  | ||||
| @@ -29,10 +45,6 @@ func (*GetReturnRule) Name() string { | ||||
| 	return "get-return" | ||||
| } | ||||
|  | ||||
| type lintReturnRule struct { | ||||
| 	onFailure func(lint.Failure) | ||||
| } | ||||
|  | ||||
| const getterPrefix = "GET" | ||||
|  | ||||
| var lenGetterPrefix = len(getterPrefix) | ||||
| @@ -57,24 +69,3 @@ func isGetter(name string) bool { | ||||
| func hasResults(rs *ast.FieldList) bool { | ||||
| 	return rs != nil && len(rs.List) > 0 | ||||
| } | ||||
|  | ||||
| func (w lintReturnRule) Visit(node ast.Node) ast.Visitor { | ||||
| 	fd, ok := node.(*ast.FuncDecl) | ||||
| 	if !ok { | ||||
| 		return w | ||||
| 	} | ||||
|  | ||||
| 	if !isGetter(fd.Name.Name) { | ||||
| 		return w | ||||
| 	} | ||||
| 	if !hasResults(fd.Type.Results) { | ||||
| 		w.onFailure(lint.Failure{ | ||||
| 			Confidence: 0.8, | ||||
| 			Node:       fd, | ||||
| 			Category:   "logic", | ||||
| 			Failure:    fmt.Sprintf("function '%s' seems to be a getter but it does not return any result", fd.Name.Name), | ||||
| 		}) | ||||
| 	} | ||||
|  | ||||
| 	return w | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user