Skip to content

Commit 1194c63

Browse files
committed
Auto merge of #8932 - dswij:pr-8879, r=giraffate
`needless_return` checks for macro expr in return stmts closes #8879 Macro expressions in returns were not checked by `needless_return`. The test added in [this commit](6396a7a#diff-a869168cfafb7e6e5010feb76a16389d6c96d59e98113dee5c2b304a5160e43aR51-R55) seems to have regressed. changelog: [`needless_return`] checks for macro exprs in return statements
2 parents 97e5449 + 678dcdd commit 1194c63

File tree

5 files changed

+40
-14
lines changed

5 files changed

+40
-14
lines changed

clippy_lints/src/methods/option_map_or_none.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ pub(super) fn check<'tcx>(
9797
let func_snippet = snippet(cx, map_arg.span, "..");
9898
let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
9999
`and_then(..)` instead";
100-
return span_lint_and_sugg(
100+
span_lint_and_sugg(
101101
cx,
102102
OPTION_MAP_OR_NONE,
103103
expr.span,
@@ -110,7 +110,7 @@ pub(super) fn check<'tcx>(
110110
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
111111
`ok()` instead";
112112
let self_snippet = snippet(cx, recv.span, "..");
113-
return span_lint_and_sugg(
113+
span_lint_and_sugg(
114114
cx,
115115
RESULT_MAP_OR_INTO_OPTION,
116116
expr.span,

clippy_lints/src/returns.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2-
use clippy_utils::source::snippet_opt;
2+
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;
55
use rustc_ast::ast::Attribute;
@@ -226,14 +226,10 @@ fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Spa
226226
}
227227
match inner_span {
228228
Some(inner_span) => {
229-
if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() {
230-
return;
231-
}
232-
229+
let mut applicability = Applicability::MachineApplicable;
233230
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
234-
if let Some(snippet) = snippet_opt(cx, inner_span) {
235-
diag.span_suggestion(ret_span, "remove `return`", snippet, Applicability::MachineApplicable);
236-
}
231+
let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
232+
diag.span_suggestion(ret_span, "remove `return`", snippet, applicability);
237233
});
238234
},
239235
None => match replacement {
@@ -287,7 +283,7 @@ struct BorrowVisitor<'a, 'tcx> {
287283

288284
impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
289285
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
290-
if self.borrows {
286+
if self.borrows || expr.span.from_expansion() {
291287
return;
292288
}
293289

tests/ui/needless_return.fixed

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn test_closure() {
5353
}
5454

5555
fn test_macro_call() -> i32 {
56-
return the_answer!();
56+
the_answer!()
5757
}
5858

5959
fn test_void_fun() {
@@ -175,7 +175,7 @@ async fn async_test_closure() {
175175
}
176176

177177
async fn async_test_macro_call() -> i32 {
178-
return the_answer!();
178+
the_answer!()
179179
}
180180

181181
async fn async_test_void_fun() {
@@ -223,4 +223,10 @@ fn let_else() {
223223
let Some(1) = Some(1) else { return };
224224
}
225225

226+
fn needless_return_macro() -> String {
227+
let _ = "foo";
228+
let _ = "bar";
229+
format!("Hello {}", "world!")
230+
}
231+
226232
fn main() {}

tests/ui/needless_return.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,4 +223,10 @@ fn let_else() {
223223
let Some(1) = Some(1) else { return };
224224
}
225225

226+
fn needless_return_macro() -> String {
227+
let _ = "foo";
228+
let _ = "bar";
229+
return format!("Hello {}", "world!");
230+
}
231+
226232
fn main() {}

tests/ui/needless_return.stderr

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ error: unneeded `return` statement
4848
LL | let _ = || return true;
4949
| ^^^^^^^^^^^ help: remove `return`: `true`
5050

51+
error: unneeded `return` statement
52+
--> $DIR/needless_return.rs:56:5
53+
|
54+
LL | return the_answer!();
55+
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `the_answer!()`
56+
5157
error: unneeded `return` statement
5258
--> $DIR/needless_return.rs:60:5
5359
|
@@ -168,6 +174,12 @@ error: unneeded `return` statement
168174
LL | let _ = || return true;
169175
| ^^^^^^^^^^^ help: remove `return`: `true`
170176

177+
error: unneeded `return` statement
178+
--> $DIR/needless_return.rs:178:5
179+
|
180+
LL | return the_answer!();
181+
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `the_answer!()`
182+
171183
error: unneeded `return` statement
172184
--> $DIR/needless_return.rs:182:5
173185
|
@@ -204,5 +216,11 @@ error: unneeded `return` statement
204216
LL | return String::new();
205217
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`
206218

207-
error: aborting due to 34 previous errors
219+
error: unneeded `return` statement
220+
--> $DIR/needless_return.rs:229:5
221+
|
222+
LL | return format!("Hello {}", "world!");
223+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `format!("Hello {}", "world!")`
224+
225+
error: aborting due to 37 previous errors
208226

0 commit comments

Comments
 (0)