Skip to content

Commit f18cf82

Browse files
committed
Don't trigger needless_return lint in macros
1 parent 0c5ba9a commit f18cf82

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

clippy_lints/src/returns.rs

+3
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ fn check_final_expr<'tcx>(
217217
}
218218

219219
fn emit_return_lint(cx: &LateContext<'_>, ret_span: Span, inner_span: Option<Span>, replacement: RetReplacement) {
220+
if ret_span.from_expansion() {
221+
return;
222+
}
220223
match inner_span {
221224
Some(inner_span) => {
222225
if in_external_macro(cx.tcx.sess, inner_span) || inner_span.from_expansion() {

tests/ui/needless_return.fixed

+15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ fn borrows_but_not_last(value: bool) -> String {
8686
}
8787
}
8888

89+
macro_rules! needed_return {
90+
($e:expr) => {
91+
if $e > 3 {
92+
return;
93+
}
94+
};
95+
}
96+
97+
fn test_return_in_macro() {
98+
// This will return and the macro below won't be executed. Removing the `return` from the macro
99+
// will change semantics.
100+
needed_return!(10);
101+
needed_return!(0);
102+
}
103+
89104
fn main() {
90105
let _ = test_end_of_fn();
91106
let _ = test_no_semicolon();

tests/ui/needless_return.rs

+15
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,21 @@ fn borrows_but_not_last(value: bool) -> String {
8686
}
8787
}
8888

89+
macro_rules! needed_return {
90+
($e:expr) => {
91+
if $e > 3 {
92+
return;
93+
}
94+
};
95+
}
96+
97+
fn test_return_in_macro() {
98+
// This will return and the macro below won't be executed. Removing the `return` from the macro
99+
// will change semantics.
100+
needed_return!(10);
101+
needed_return!(0);
102+
}
103+
89104
fn main() {
90105
let _ = test_end_of_fn();
91106
let _ = test_no_semicolon();

0 commit comments

Comments
 (0)