Skip to content

Commit 5d40965

Browse files
committed
Fix #1346
1 parent 1973e94 commit 5d40965

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

clippy_lints/src/returns.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,6 @@ impl ReturnPass {
5858

5959
// Check a the final expression in a block if it's a return.
6060
fn check_final_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr, span: Option<Span>) {
61-
fn attr_is_cfg(attr: &ast::Attribute) -> bool {
62-
if let ast::MetaItemKind::List(ref key, _) = attr.node.value.node {
63-
*key == "cfg"
64-
} else {
65-
false
66-
}
67-
}
68-
6961
match expr.node {
7062
// simple return is always "bad"
7163
ast::ExprKind::Ret(Some(ref inner)) => {
@@ -116,6 +108,7 @@ impl ReturnPass {
116108
let ast::StmtKind::Expr(ref retexpr) = retexpr.node,
117109
let Some(stmt) = it.next_back(),
118110
let ast::StmtKind::Local(ref local) = stmt.node,
111+
!local.attrs.iter().any(attr_is_cfg),
119112
let Some(ref initexpr) = local.init,
120113
let ast::PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
121114
let ast::ExprKind::Path(_, ref path) = retexpr.node,
@@ -151,3 +144,12 @@ impl EarlyLintPass for ReturnPass {
151144
self.check_let_return(cx, block);
152145
}
153146
}
147+
148+
fn attr_is_cfg(attr: &ast::Attribute) -> bool {
149+
if let ast::MetaItemKind::List(ref key, _) = attr.node.value.node {
150+
*key == "cfg"
151+
} else {
152+
false
153+
}
154+
}
155+

tests/run-pass/needless_return.rs

Lines changed: 0 additions & 9 deletions
This file was deleted.

tests/run-pass/returns.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#[deny(warnings)]
2+
fn cfg_return() -> i32 {
3+
#[cfg(unix)] return 1;
4+
#[cfg(not(unix))] return 2;
5+
}
6+
7+
#[deny(warnings)]
8+
fn cfg_let_and_return() -> i32 {
9+
#[cfg(unix)]
10+
let x = 1;
11+
#[cfg(not(unix))]
12+
let x = 2;
13+
x
14+
}
15+
16+
fn main() {
17+
cfg_return();
18+
cfg_let_and_return();
19+
}

0 commit comments

Comments
 (0)