Skip to content

Commit bdc6ece

Browse files
committed
Fix #[expect] for clippy::needless_return
1 parent 54ad99b commit bdc6ece

File tree

4 files changed

+109
-56
lines changed

4 files changed

+109
-56
lines changed

clippy_lints/src/returns.rs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::source::{snippet_opt, snippet_with_context};
33
use clippy_utils::{fn_def_id, path_to_local_id};
44
use if_chain::if_chain;
@@ -94,9 +94,10 @@ impl<'tcx> LateLintPass<'tcx> for Return {
9494
if !in_external_macro(cx.sess(), retexpr.span);
9595
if !local.span.from_expansion();
9696
then {
97-
span_lint_and_then(
97+
span_lint_hir_and_then(
9898
cx,
9999
LET_AND_RETURN,
100+
retexpr.hir_id,
100101
retexpr.span,
101102
"returning the result of a `let` binding from a block",
102103
|err| {
@@ -185,6 +186,7 @@ fn check_final_expr<'tcx>(
185186
if !borrows {
186187
emit_return_lint(
187188
cx,
189+
inner.map_or(expr.hir_id, |inner| inner.hir_id),
188190
span.expect("`else return` is not possible"),
189191
inner.as_ref().map(|i| i.span),
190192
replacement,
@@ -220,50 +222,81 @@ fn check_final_expr<'tcx>(
220222
}
221223
}
222224

223-
fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) {
225+
fn emit_return_lint(
226+
cx: &LateContext<'_>,
227+
emission_place: HirId,
228+
ret_span: Span,
229+
inner_span: Option<Span>,
230+
replacement: RetReplacement,
231+
) {
224232
if ret_span.from_expansion() {
225233
return;
226234
}
227235
match inner_span {
228236
Some(inner_span) => {
229237
let mut applicability = Applicability::MachineApplicable;
230-
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
231-
let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
232-
diag.span_suggestion(ret_span, "remove `return`", snippet, applicability);
233-
});
238+
span_lint_hir_and_then(
239+
cx,
240+
NEEDLESS_RETURN,
241+
emission_place,
242+
ret_span,
243+
"unneeded `return` statement",
244+
|diag| {
245+
let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
246+
diag.span_suggestion(ret_span, "remove `return`", snippet, applicability);
247+
},
248+
);
234249
},
235250
None => match replacement {
236251
RetReplacement::Empty => {
237-
span_lint_and_sugg(
252+
span_lint_hir_and_then(
238253
cx,
239254
NEEDLESS_RETURN,
255+
emission_place,
240256
ret_span,
241257
"unneeded `return` statement",
242-
"remove `return`",
243-
String::new(),
244-
Applicability::MachineApplicable,
258+
|diag| {
259+
diag.span_suggestion(
260+
ret_span,
261+
"remove `return`",
262+
String::new(),
263+
Applicability::MachineApplicable,
264+
);
265+
},
245266
);
246267
},
247268
RetReplacement::Block => {
248-
span_lint_and_sugg(
269+
span_lint_hir_and_then(
249270
cx,
250271
NEEDLESS_RETURN,
272+
emission_place,
251273
ret_span,
252274
"unneeded `return` statement",
253-
"replace `return` with an empty block",
254-
"{}".to_string(),
255-
Applicability::MachineApplicable,
275+
|diag| {
276+
diag.span_suggestion(
277+
ret_span,
278+
"replace `return` with an empty block",
279+
"{}".to_string(),
280+
Applicability::MachineApplicable,
281+
);
282+
},
256283
);
257284
},
258285
RetReplacement::Unit => {
259-
span_lint_and_sugg(
286+
span_lint_hir_and_then(
260287
cx,
261288
NEEDLESS_RETURN,
289+
emission_place,
262290
ret_span,
263291
"unneeded `return` statement",
264-
"replace `return` with a unit value",
265-
"()".to_string(),
266-
Applicability::MachineApplicable,
292+
|diag| {
293+
diag.span_suggestion(
294+
ret_span,
295+
"replace `return` with a unit value",
296+
"()".to_string(),
297+
Applicability::MachineApplicable,
298+
);
299+
},
267300
);
268301
},
269302
},

tests/ui/needless_return.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22

3+
#![feature(lint_reasons)]
34
#![feature(let_else)]
45
#![allow(unused)]
56
#![allow(
@@ -227,4 +228,13 @@ fn needless_return_macro() -> String {
227228
format!("Hello {}", "world!")
228229
}
229230

231+
fn check_expect() -> bool {
232+
if true {
233+
// no error!
234+
return true;
235+
}
236+
#[expect(clippy::needless_return)]
237+
return true;
238+
}
239+
230240
fn main() {}

tests/ui/needless_return.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// run-rustfix
22

3+
#![feature(lint_reasons)]
34
#![feature(let_else)]
45
#![allow(unused)]
56
#![allow(
@@ -227,4 +228,13 @@ fn needless_return_macro() -> String {
227228
return format!("Hello {}", "world!");
228229
}
229230

231+
fn check_expect() -> bool {
232+
if true {
233+
// no error!
234+
return true;
235+
}
236+
#[expect(clippy::needless_return)]
237+
return true;
238+
}
239+
230240
fn main() {}

0 commit comments

Comments
 (0)