mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 13:13:34 +00:00
Fix #1346
This commit is contained in:
parent
1973e94550
commit
5d40965b50
3 changed files with 29 additions and 17 deletions
|
@ -58,14 +58,6 @@ impl ReturnPass {
|
||||||
|
|
||||||
// Check a the final expression in a block if it's a return.
|
// 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<Span>) {
|
fn check_final_expr(&mut self, cx: &EarlyContext, expr: &ast::Expr, span: Option<Span>) {
|
||||||
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 {
|
match expr.node {
|
||||||
// simple return is always "bad"
|
// simple return is always "bad"
|
||||||
ast::ExprKind::Ret(Some(ref inner)) => {
|
ast::ExprKind::Ret(Some(ref inner)) => {
|
||||||
|
@ -116,6 +108,7 @@ impl ReturnPass {
|
||||||
let ast::StmtKind::Expr(ref retexpr) = retexpr.node,
|
let ast::StmtKind::Expr(ref retexpr) = retexpr.node,
|
||||||
let Some(stmt) = it.next_back(),
|
let Some(stmt) = it.next_back(),
|
||||||
let ast::StmtKind::Local(ref local) = stmt.node,
|
let ast::StmtKind::Local(ref local) = stmt.node,
|
||||||
|
!local.attrs.iter().any(attr_is_cfg),
|
||||||
let Some(ref initexpr) = local.init,
|
let Some(ref initexpr) = local.init,
|
||||||
let ast::PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
|
let ast::PatKind::Ident(_, Spanned { node: id, .. }, _) = local.pat.node,
|
||||||
let ast::ExprKind::Path(_, ref path) = retexpr.node,
|
let ast::ExprKind::Path(_, ref path) = retexpr.node,
|
||||||
|
@ -151,3 +144,12 @@ impl EarlyLintPass for ReturnPass {
|
||||||
self.check_let_return(cx, 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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +0,0 @@
|
||||||
#[deny(warnings)]
|
|
||||||
fn cfg_return() -> i32 {
|
|
||||||
#[cfg(msvc)] return 1;
|
|
||||||
#[cfg(not(msvc))] return 2;
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
cfg_return();
|
|
||||||
}
|
|
19
tests/run-pass/returns.rs
Normal file
19
tests/run-pass/returns.rs
Normal file
|
@ -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();
|
||||||
|
}
|
Loading…
Reference in a new issue