Skip to content

needless_return checks for macro expr in return stmts #8932

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions clippy_lints/src/methods/option_map_or_none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub(super) fn check<'tcx>(
let func_snippet = snippet(cx, map_arg.span, "..");
let msg = "called `map_or(None, ..)` on an `Option` value. This can be done more directly by calling \
`and_then(..)` instead";
return span_lint_and_sugg(
span_lint_and_sugg(
cx,
OPTION_MAP_OR_NONE,
expr.span,
Expand All @@ -110,7 +110,7 @@ pub(super) fn check<'tcx>(
let msg = "called `map_or(None, Some)` on a `Result` value. This can be done more directly by calling \
`ok()` instead";
let self_snippet = snippet(cx, recv.span, "..");
return span_lint_and_sugg(
span_lint_and_sugg(
cx,
RESULT_MAP_OR_INTO_OPTION,
expr.span,
Expand Down
14 changes: 5 additions & 9 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::source::snippet_opt;
use clippy_utils::source::{snippet_opt, snippet_with_context};
use clippy_utils::{fn_def_id, path_to_local_id};
use if_chain::if_chain;
use rustc_ast::ast::Attribute;
Expand Down Expand Up @@ -226,14 +226,10 @@ fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Spa
}
match inner_span {
Some(inner_span) => {
if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() {
return;
}

let mut applicability = Applicability::MachineApplicable;
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
if let Some(snippet) = snippet_opt(cx, inner_span) {
diag.span_suggestion(ret_span, "remove `return`", snippet, Applicability::MachineApplicable);
}
let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
diag.span_suggestion(ret_span, "remove `return`", snippet, applicability);
});
},
None => match replacement {
Expand Down Expand Up @@ -287,7 +283,7 @@ struct BorrowVisitor<'a, 'tcx> {

impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
if self.borrows {
if self.borrows || expr.span.from_expansion() {
return;
}

Expand Down
10 changes: 8 additions & 2 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn test_closure() {
}

fn test_macro_call() -> i32 {
return the_answer!();
the_answer!()
}

fn test_void_fun() {
Expand Down Expand Up @@ -175,7 +175,7 @@ async fn async_test_closure() {
}

async fn async_test_macro_call() -> i32 {
return the_answer!();
the_answer!()
}

async fn async_test_void_fun() {
Expand Down Expand Up @@ -223,4 +223,10 @@ fn let_else() {
let Some(1) = Some(1) else { return };
}

fn needless_return_macro() -> String {
let _ = "foo";
let _ = "bar";
format!("Hello {}", "world!")
}

fn main() {}
6 changes: 6 additions & 0 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,10 @@ fn let_else() {
let Some(1) = Some(1) else { return };
}

fn needless_return_macro() -> String {
let _ = "foo";
let _ = "bar";
return format!("Hello {}", "world!");
}

fn main() {}
20 changes: 19 additions & 1 deletion tests/ui/needless_return.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ error: unneeded `return` statement
LL | let _ = || return true;
| ^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:56:5
|
LL | return the_answer!();
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `the_answer!()`

error: unneeded `return` statement
--> $DIR/needless_return.rs:60:5
|
Expand Down Expand Up @@ -168,6 +174,12 @@ error: unneeded `return` statement
LL | let _ = || return true;
| ^^^^^^^^^^^ help: remove `return`: `true`

error: unneeded `return` statement
--> $DIR/needless_return.rs:178:5
|
LL | return the_answer!();
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `the_answer!()`

error: unneeded `return` statement
--> $DIR/needless_return.rs:182:5
|
Expand Down Expand Up @@ -204,5 +216,11 @@ error: unneeded `return` statement
LL | return String::new();
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `String::new()`

error: aborting due to 34 previous errors
error: unneeded `return` statement
--> $DIR/needless_return.rs:229:5
|
LL | return format!("Hello {}", "world!");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: remove `return`: `format!("Hello {}", "world!")`

error: aborting due to 37 previous errors