diff options
| author | flip1995 <philipp.krones@embecosm.com> | 2021-01-15 10:41:29 +0100 |
|---|---|---|
| committer | flip1995 <philipp.krones@embecosm.com> | 2021-01-15 10:41:29 +0100 |
| commit | f18cf82ca8764d6b0b07549cdba25b91bd0243fa (patch) | |
| tree | e0484caad6b7c9d06752ff68ba7e2969dce40008 | |
| parent | 0c5ba9a883fe8c2afff27b69d1829a0d0befe39d (diff) | |
| download | rust-f18cf82ca8764d6b0b07549cdba25b91bd0243fa.tar.gz rust-f18cf82ca8764d6b0b07549cdba25b91bd0243fa.zip | |
Don't trigger needless_return lint in macros
| -rw-r--r-- | clippy_lints/src/returns.rs | 3 | ||||
| -rw-r--r-- | tests/ui/needless_return.fixed | 15 | ||||
| -rw-r--r-- | tests/ui/needless_return.rs | 15 |
3 files changed, 33 insertions, 0 deletions
diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 0e031e6151b..63548d8fdb4 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -217,6 +217,9 @@ fn check_final_expr<'tcx>( } fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) { + if ret_span.from_expansion() { + return; + } match inner_span { Some(inner_span) => { if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() { diff --git a/tests/ui/needless_return.fixed b/tests/ui/needless_return.fixed index d849e093da7..86bfc5b4bb2 100644 --- a/tests/ui/needless_return.fixed +++ b/tests/ui/needless_return.fixed @@ -86,6 +86,21 @@ fn borrows_but_not_last(value: bool) -> String { } } +macro_rules! needed_return { + ($e:expr) => { + if $e > 3 { + return; + } + }; +} + +fn test_return_in_macro() { + // This will return and the macro below won't be executed. Removing the `return` from the macro + // will change semantics. + needed_return!(10); + needed_return!(0); +} + fn main() { let _ = test_end_of_fn(); let _ = test_no_semicolon(); diff --git a/tests/ui/needless_return.rs b/tests/ui/needless_return.rs index 29f2bd1852a..51061370dfe 100644 --- a/tests/ui/needless_return.rs +++ b/tests/ui/needless_return.rs @@ -86,6 +86,21 @@ fn borrows_but_not_last(value: bool) -> String { } } +macro_rules! needed_return { + ($e:expr) => { + if $e > 3 { + return; + } + }; +} + +fn test_return_in_macro() { + // This will return and the macro below won't be executed. Removing the `return` from the macro + // will change semantics. + needed_return!(10); + needed_return!(0); +} + fn main() { let _ = test_end_of_fn(); let _ = test_no_semicolon(); |
