@@ -45,6 +45,19 @@ declare_clippy_lint! {
4545pub struct Pass ;
4646
4747impl Pass {
48+ fn lint ( cx : & LateContext < ' _ , ' _ > , outer_span : syntax_pos:: Span , inner_span : syntax_pos:: Span , msg : & str ) {
49+ span_lint_and_then ( cx, IMPLICIT_RETURN , outer_span, "missing return statement" , |db| {
50+ if let Some ( snippet) = snippet_opt ( cx, inner_span) {
51+ db. span_suggestion_with_applicability (
52+ outer_span,
53+ msg,
54+ format ! ( "return {}" , snippet) ,
55+ Applicability :: MachineApplicable ,
56+ ) ;
57+ }
58+ } ) ;
59+ }
60+
4861 fn expr_match ( cx : & LateContext < ' _ , ' _ > , expr : & rustc:: hir:: Expr ) {
4962 match & expr. node {
5063 // loops could be using `break` instead of `return`
@@ -55,23 +68,19 @@ impl Pass {
5568 // only needed in the case of `break` with `;` at the end
5669 else if let Some ( stmt) = block. stmts . last ( ) {
5770 if let rustc:: hir:: StmtKind :: Semi ( expr, ..) = & stmt. node {
58- Self :: expr_match ( cx, expr) ;
71+ // make sure it's a break, otherwise we want to skip
72+ if let ExprKind :: Break ( .., break_expr) = & expr. node {
73+ if let Some ( break_expr) = break_expr {
74+ Self :: lint ( cx, expr. span , break_expr. span , "change `break` to `return` as shown" ) ;
75+ }
76+ }
5977 }
6078 }
6179 } ,
6280 // use `return` instead of `break`
6381 ExprKind :: Break ( .., break_expr) => {
6482 if let Some ( break_expr) = break_expr {
65- span_lint_and_then ( cx, IMPLICIT_RETURN , expr. span , "missing return statement" , |db| {
66- if let Some ( snippet) = snippet_opt ( cx, break_expr. span ) {
67- db. span_suggestion_with_applicability (
68- expr. span ,
69- "change `break` to `return` as shown" ,
70- format ! ( "return {}" , snippet) ,
71- Applicability :: MachineApplicable ,
72- ) ;
73- }
74- } ) ;
83+ Self :: lint ( cx, expr. span , break_expr. span , "change `break` to `return` as shown" ) ;
7584 }
7685 } ,
7786 ExprKind :: If ( .., if_expr, else_expr) => {
@@ -89,16 +98,7 @@ impl Pass {
8998 // skip if it already has a return statement
9099 ExprKind :: Ret ( ..) => ( ) ,
91100 // everything else is missing `return`
92- _ => span_lint_and_then ( cx, IMPLICIT_RETURN , expr. span , "missing return statement" , |db| {
93- if let Some ( snippet) = snippet_opt ( cx, expr. span ) {
94- db. span_suggestion_with_applicability (
95- expr. span ,
96- "add `return` as shown" ,
97- format ! ( "return {}" , snippet) ,
98- Applicability :: MachineApplicable ,
99- ) ;
100- }
101- } ) ,
101+ _ => Self :: lint ( cx, expr. span , expr. span , "add `return` as shown" ) ,
102102 }
103103 }
104104}
0 commit comments