From 5d40965b504f7fd4eb65bf3b44df112e41e28d18 Mon Sep 17 00:00:00 2001 From: sinkuu Date: Sun, 20 Nov 2016 10:15:40 +0900 Subject: [PATCH] Fix #1346 --- clippy_lints/src/returns.rs | 18 ++++++++++-------- tests/run-pass/needless_return.rs | 9 --------- tests/run-pass/returns.rs | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 17 deletions(-) delete mode 100644 tests/run-pass/needless_return.rs create mode 100644 tests/run-pass/returns.rs diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 334e7142cff..b19d7b9b4c1 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -58,14 +58,6 @@ fn check_block_return(&mut self, cx: &EarlyContext, block: &ast::Block) { // Check a the final expression in a block if it's a return. fn check_final_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr, span: Option) { - fn attr_is_cfg(attr: &ast::Attribute) -> bool { - if let ast::MetaItemKind::List(ref key, _) = attr.node.value.node { - *key == "cfg" - } else { - false - } - } - match expr.node { // simple return is always "bad" ast::ExprKind::Ret(Some(ref inner)) => { @@ -116,6 +108,7 @@ fn check_let_return(&mut self, cx: &EarlyContext, block: &ast::Block) { let ast::StmtKind::Expr(ref retexpr) = retexpr.node, let Some(stmt) = it.next_back(), let ast::StmtKind::Local(ref local) = stmt.node, + !local.attrs.iter().any(attr_is_cfg), let Some(ref initexpr) = local.init, let ast::PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node, let ast::ExprKind::Path(_, ref path) = retexpr.node, @@ -151,3 +144,12 @@ fn check_block(&mut self, cx: &EarlyContext, block: &ast::Block) { self.check_let_return(cx, block); } } + +fn attr_is_cfg(attr: &ast::Attribute) -> bool { + if let ast::MetaItemKind::List(ref key, _) = attr.node.value.node { + *key == "cfg" + } else { + false + } +} + diff --git a/tests/run-pass/needless_return.rs b/tests/run-pass/needless_return.rs deleted file mode 100644 index 5a0225faee5..00000000000 --- a/tests/run-pass/needless_return.rs +++ /dev/null @@ -1,9 +0,0 @@ -#[deny(warnings)] -fn cfg_return() -> i32 { - #[cfg(msvc)] return 1; - #[cfg(not(msvc))] return 2; -} - -fn main() { - cfg_return(); -} diff --git a/tests/run-pass/returns.rs b/tests/run-pass/returns.rs new file mode 100644 index 00000000000..882d3aa7f32 --- /dev/null +++ b/tests/run-pass/returns.rs @@ -0,0 +1,19 @@ +#[deny(warnings)] +fn cfg_return() -> i32 { + #[cfg(unix)] return 1; + #[cfg(not(unix))] return 2; +} + +#[deny(warnings)] +fn cfg_let_and_return() -> i32 { + #[cfg(unix)] + let x = 1; + #[cfg(not(unix))] + let x = 2; + x +} + +fn main() { + cfg_return(); + cfg_let_and_return(); +} -- 2.44.0