Skip to content

Commit d1dbdcf

Browse files
committed
refactor needless_return
1 parent e120fb1 commit d1dbdcf

File tree

1 file changed

+47
-64
lines changed

1 file changed

+47
-64
lines changed

clippy_lints/src/returns.rs

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,28 @@ enum RetReplacement {
7272
Unit,
7373
}
7474

75+
impl RetReplacement {
76+
fn sugg_help(&self) -> &'static str {
77+
match *self {
78+
Self::Empty => "remove `return`",
79+
Self::Block => "replace `return` with an empty block",
80+
Self::Unit => "replace `return` with a unit value",
81+
82+
}
83+
}
84+
}
85+
86+
impl ToString for RetReplacement {
87+
fn to_string(&self) -> String {
88+
match *self {
89+
Self::Empty => "",
90+
Self::Block => "{}",
91+
Self::Unit => "()",
92+
}
93+
.to_string()
94+
}
95+
}
96+
7597
declare_lint_pass!(Return => [LET_AND_RETURN, NEEDLESS_RETURN]);
7698

7799
impl<'tcx> LateLintPass<'tcx> for Return {
@@ -221,74 +243,35 @@ fn emit_return_lint(
221243
if ret_span.from_expansion() {
222244
return;
223245
}
224-
match inner_span {
225-
Some(inner_span) => {
226-
let mut applicability = Applicability::MachineApplicable;
227-
span_lint_hir_and_then(
228-
cx,
229-
NEEDLESS_RETURN,
230-
emission_place,
231-
ret_span,
232-
"unneeded `return` statement",
233-
|diag| {
234-
let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
235-
diag.span_suggestion(ret_span, "remove `return`", snippet, applicability);
236-
},
237-
);
238-
},
239-
None => match replacement {
240-
RetReplacement::Empty => {
241-
span_lint_hir_and_then(
242-
cx,
243-
NEEDLESS_RETURN,
244-
emission_place,
245-
ret_span,
246-
"unneeded `return` statement",
247-
|diag| {
248-
diag.span_suggestion(
249-
ret_span,
250-
"remove `return`",
251-
String::new(),
252-
Applicability::MachineApplicable,
253-
);
254-
},
255-
);
256-
},
257-
RetReplacement::Block => {
258-
span_lint_hir_and_then(
259-
cx,
260-
NEEDLESS_RETURN,
261-
emission_place,
262-
ret_span,
263-
"unneeded `return` statement",
264-
|diag| {
265-
diag.span_suggestion(
266-
ret_span,
267-
"replace `return` with an empty block",
268-
"{}".to_string(),
269-
Applicability::MachineApplicable,
270-
);
271-
},
272-
);
246+
if let Some(inner_span) = inner_span {
247+
let mut applicability = Applicability::MachineApplicable;
248+
span_lint_hir_and_then(
249+
cx,
250+
NEEDLESS_RETURN,
251+
emission_place,
252+
ret_span,
253+
"unneeded `return` statement",
254+
|diag| {
255+
let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
256+
diag.span_suggestion(ret_span, "remove `return`", snippet, applicability);
273257
},
274-
RetReplacement::Unit => {
275-
span_lint_hir_and_then(
276-
cx,
277-
NEEDLESS_RETURN,
278-
emission_place,
258+
);
259+
} else {
260+
span_lint_hir_and_then(
261+
cx,
262+
NEEDLESS_RETURN,
263+
emission_place,
264+
ret_span,
265+
"unneeded `return` statement",
266+
|diag| {
267+
diag.span_suggestion(
279268
ret_span,
280-
"unneeded `return` statement",
281-
|diag| {
282-
diag.span_suggestion(
283-
ret_span,
284-
"replace `return` with a unit value",
285-
"()".to_string(),
286-
Applicability::MachineApplicable,
287-
);
288-
},
269+
replacement.sugg_help(),
270+
replacement.to_string(),
271+
Applicability::MachineApplicable,
289272
);
290273
},
291-
},
274+
)
292275
}
293276
}
294277

0 commit comments

Comments
 (0)