needless_return
checks for macro expr in return stmts
This commit is contained in:
parent
0d5ace3ed2
commit
b885035ef7
@ -1,5 +1,5 @@
|
|||||||
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
|
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 clippy_utils::{fn_def_id, path_to_local_id};
|
||||||
use if_chain::if_chain;
|
use if_chain::if_chain;
|
||||||
use rustc_ast::ast::Attribute;
|
use rustc_ast::ast::Attribute;
|
||||||
@ -226,14 +226,10 @@ fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Spa
|
|||||||
}
|
}
|
||||||
match inner_span {
|
match inner_span {
|
||||||
Some(inner_span) => {
|
Some(inner_span) => {
|
||||||
if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() {
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
|
span_lint_and_then(cx, NEEDLESS_RETURN, ret_span, "unneeded `return` statement", |diag| {
|
||||||
if let Some(snippet) = snippet_opt(cx, inner_span) {
|
let (snippet, _) = snippet_with_context(cx, inner_span, ret_span.ctxt(), "..", &mut applicability);
|
||||||
diag.span_suggestion(ret_span, "remove `return`", snippet, Applicability::MachineApplicable);
|
diag.span_suggestion(ret_span, "remove `return`", snippet, applicability);
|
||||||
}
|
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
None => match replacement {
|
None => match replacement {
|
||||||
@ -287,7 +283,7 @@ struct BorrowVisitor<'a, 'tcx> {
|
|||||||
|
|
||||||
impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
|
impl<'tcx> Visitor<'tcx> for BorrowVisitor<'_, 'tcx> {
|
||||||
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
||||||
if self.borrows {
|
if self.borrows || expr.span.from_expansion() {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ fn test_closure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn test_macro_call() -> i32 {
|
fn test_macro_call() -> i32 {
|
||||||
return the_answer!();
|
the_answer!()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_void_fun() {
|
fn test_void_fun() {
|
||||||
@ -175,7 +175,7 @@ async fn async_test_closure() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async fn async_test_macro_call() -> i32 {
|
async fn async_test_macro_call() -> i32 {
|
||||||
return the_answer!();
|
the_answer!()
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn async_test_void_fun() {
|
async fn async_test_void_fun() {
|
||||||
@ -223,4 +223,10 @@ fn let_else() {
|
|||||||
let Some(1) = Some(1) else { return };
|
let Some(1) = Some(1) else { return };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn needless_return_macro() -> String {
|
||||||
|
let _ = "foo";
|
||||||
|
let _ = "bar";
|
||||||
|
format!("Hello {}", "world!")
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -223,4 +223,10 @@ fn let_else() {
|
|||||||
let Some(1) = Some(1) else { return };
|
let Some(1) = Some(1) else { return };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn needless_return_macro() -> String {
|
||||||
|
let _ = "foo";
|
||||||
|
let _ = "bar";
|
||||||
|
return format!("Hello {}", "world!");
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -48,6 +48,12 @@ error: unneeded `return` statement
|
|||||||
LL | let _ = || return true;
|
LL | let _ = || return true;
|
||||||
| ^^^^^^^^^^^ help: remove `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
|
error: unneeded `return` statement
|
||||||
--> $DIR/needless_return.rs:60:5
|
--> $DIR/needless_return.rs:60:5
|
||||||
|
|
|
|
||||||
@ -168,6 +174,12 @@ error: unneeded `return` statement
|
|||||||
LL | let _ = || return true;
|
LL | let _ = || return true;
|
||||||
| ^^^^^^^^^^^ help: remove `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
|
error: unneeded `return` statement
|
||||||
--> $DIR/needless_return.rs:182:5
|
--> $DIR/needless_return.rs:182:5
|
||||||
|
|
|
|
||||||
@ -204,5 +216,11 @@ error: unneeded `return` statement
|
|||||||
LL | return String::new();
|
LL | return String::new();
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^ help: remove `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
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user