@@ -45,6 +45,19 @@ declare_clippy_lint! {
45
45
pub struct Pass ;
46
46
47
47
impl 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
+
48
61
fn expr_match ( cx : & LateContext < ' _ , ' _ > , expr : & rustc:: hir:: Expr ) {
49
62
match & expr. node {
50
63
// loops could be using `break` instead of `return`
@@ -55,23 +68,19 @@ impl Pass {
55
68
// only needed in the case of `break` with `;` at the end
56
69
else if let Some ( stmt) = block. stmts . last ( ) {
57
70
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
+ }
59
77
}
60
78
}
61
79
} ,
62
80
// use `return` instead of `break`
63
81
ExprKind :: Break ( .., break_expr) => {
64
82
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" ) ;
75
84
}
76
85
} ,
77
86
ExprKind :: If ( .., if_expr, else_expr) => {
@@ -89,16 +98,7 @@ impl Pass {
89
98
// skip if it already has a return statement
90
99
ExprKind :: Ret ( ..) => ( ) ,
91
100
// 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" ) ,
102
102
}
103
103
}
104
104
}
0 commit comments