Skip to content

Commit

Permalink
Don't trigger needless_return lint in macros
Browse files Browse the repository at this point in the history
  • Loading branch information
flip1995 committed Jan 15, 2021
1 parent 0c5ba9a commit f18cf82
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
3 changes: 3 additions & 0 deletions clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/needless_return.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 15 additions & 0 deletions tests/ui/needless_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f18cf82

Please sign in to comment.