Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 54ad99b

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

File tree

3 files changed

+45
-18
lines changed

3 files changed

+45
-18
lines changed

clippy_lints/src/implicit_return.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::{
2-
diagnostics::span_lint_and_sugg,
2+
diagnostics::span_lint_hir_and_then,
33
get_async_fn_body, is_async_fn,
44
source::{snippet_with_applicability, snippet_with_context, walk_span_to_context},
55
visitors::expr_visitor_no_bodies,
@@ -43,31 +43,38 @@ declare_clippy_lint! {
4343

4444
declare_lint_pass!(ImplicitReturn => [IMPLICIT_RETURN]);
4545

46-
fn lint_return(cx: &LateContext<'_>, span: Span) {
46+
fn lint_return(cx: &LateContext<'_>, emission_place: HirId, span: Span) {
4747
let mut app = Applicability::MachineApplicable;
4848
let snip = snippet_with_applicability(cx, span, "..", &mut app);
49-
span_lint_and_sugg(
49+
span_lint_hir_and_then(
5050
cx,
5151
IMPLICIT_RETURN,
52+
emission_place,
5253
span,
5354
"missing `return` statement",
54-
"add `return` as shown",
55-
format!("return {}", snip),
56-
app,
55+
|diag| {
56+
diag.span_suggestion(span, "add `return` as shown", format!("return {}", snip), app);
57+
},
5758
);
5859
}
5960

60-
fn lint_break(cx: &LateContext<'_>, break_span: Span, expr_span: Span) {
61+
fn lint_break(cx: &LateContext<'_>, emission_place: HirId, break_span: Span, expr_span: Span) {
6162
let mut app = Applicability::MachineApplicable;
6263
let snip = snippet_with_context(cx, expr_span, break_span.ctxt(), "..", &mut app).0;
63-
span_lint_and_sugg(
64+
span_lint_hir_and_then(
6465
cx,
6566
IMPLICIT_RETURN,
67+
emission_place,
6668
break_span,
6769
"missing `return` statement",
68-
"change `break` to `return` as shown",
69-
format!("return {}", snip),
70-
app,
70+
|diag| {
71+
diag.span_suggestion(
72+
break_span,
73+
"change `break` to `return` as shown",
74+
format!("return {}", snip),
75+
app,
76+
);
77+
},
7178
);
7279
}
7380

@@ -152,7 +159,7 @@ fn lint_implicit_returns(
152159
// At this point sub_expr can be `None` in async functions which either diverge, or return
153160
// the unit type.
154161
if let Some(sub_expr) = sub_expr {
155-
lint_break(cx, e.span, sub_expr.span);
162+
lint_break(cx, e.hir_id, e.span, sub_expr.span);
156163
}
157164
} else {
158165
// the break expression is from a macro call, add a return to the loop
@@ -166,10 +173,10 @@ fn lint_implicit_returns(
166173
if add_return {
167174
#[expect(clippy::option_if_let_else)]
168175
if let Some(span) = call_site_span {
169-
lint_return(cx, span);
176+
lint_return(cx, expr.hir_id, span);
170177
LintLocation::Parent
171178
} else {
172-
lint_return(cx, expr.span);
179+
lint_return(cx, expr.hir_id, expr.span);
173180
LintLocation::Inner
174181
}
175182
} else {
@@ -198,10 +205,10 @@ fn lint_implicit_returns(
198205
{
199206
#[expect(clippy::option_if_let_else)]
200207
if let Some(span) = call_site_span {
201-
lint_return(cx, span);
208+
lint_return(cx, expr.hir_id, span);
202209
LintLocation::Parent
203210
} else {
204-
lint_return(cx, expr.span);
211+
lint_return(cx, expr.hir_id, expr.span);
205212
LintLocation::Inner
206213
}
207214
},

tests/ui/implicit_return.fixed

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
2+
#![feature(lint_reasons)]
33
#![warn(clippy::implicit_return)]
44
#![allow(clippy::needless_return, clippy::needless_bool, unused, clippy::never_loop)]
55

@@ -128,3 +128,13 @@ async fn foo() -> bool {
128128
}
129129

130130
fn main() {}
131+
132+
fn check_expect() -> bool {
133+
if true {
134+
// no error!
135+
return true;
136+
}
137+
138+
#[expect(clippy::implicit_return)]
139+
true
140+
}

tests/ui/implicit_return.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
2+
#![feature(lint_reasons)]
33
#![warn(clippy::implicit_return)]
44
#![allow(clippy::needless_return, clippy::needless_bool, unused, clippy::never_loop)]
55

@@ -128,3 +128,13 @@ async fn foo() -> bool {
128128
}
129129

130130
fn main() {}
131+
132+
fn check_expect() -> bool {
133+
if true {
134+
// no error!
135+
return true;
136+
}
137+
138+
#[expect(clippy::implicit_return)]
139+
true
140+
}

0 commit comments

Comments
 (0)