mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 05:03:21 +00:00
Handle closure with single expression blocks
This commit is contained in:
parent
19c5f5394b
commit
945c027768
3 changed files with 27 additions and 1 deletions
|
@ -1,7 +1,7 @@
|
|||
use rustc::lint::*;
|
||||
use rustc::hir::*;
|
||||
use syntax::ast;
|
||||
use utils::{is_adjusted, match_path, match_trait_method, match_type, paths, snippet,
|
||||
use utils::{is_adjusted, match_path, match_trait_method, match_type, remove_blocks, paths, snippet,
|
||||
span_help_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
|
||||
|
||||
/// **What it does:** Checks for mapping `clone()` over an iterator.
|
||||
|
@ -31,6 +31,7 @@ impl LateLintPass for Pass {
|
|||
if name.node.as_str() == "map" && args.len() == 2 {
|
||||
match args[1].node {
|
||||
ExprClosure(_, ref decl, ref closure_expr, _) => {
|
||||
let closure_expr = remove_blocks(closure_expr);
|
||||
if_let_chain! {[
|
||||
// nothing special in the argument, besides reference bindings
|
||||
// (e.g. .map(|&x| x) )
|
||||
|
|
|
@ -773,3 +773,22 @@ pub fn is_refutable(cx: &LateContext, pat: &Pat) -> bool {
|
|||
pub fn is_automatically_derived(attrs: &[ast::Attribute]) -> bool {
|
||||
attr::contains_name(attrs, "automatically_derived")
|
||||
}
|
||||
|
||||
/// Remove blocks around an expression.
|
||||
///
|
||||
/// Ie. `x`, `{ x }` and `{{{{ x }}}}` all give `x`. `{ x; y }` and `{}` return themselves.
|
||||
pub fn remove_blocks(expr: &Expr) -> &Expr {
|
||||
if let ExprBlock(ref block) = expr.node {
|
||||
if block.stmts.is_empty() {
|
||||
if let Some(ref expr) = block.expr {
|
||||
remove_blocks(expr)
|
||||
} else {
|
||||
expr
|
||||
}
|
||||
} else {
|
||||
expr
|
||||
}
|
||||
} else {
|
||||
expr
|
||||
}
|
||||
}
|
||||
|
|
|
@ -15,6 +15,12 @@ fn map_clone_iter() {
|
|||
//~^ HELP try
|
||||
x.iter().map(|y| *y); //~ ERROR you seem to be using .map()
|
||||
//~^ HELP try
|
||||
x.iter().map(|y| { y.clone() }); //~ ERROR you seem to be using .map()
|
||||
//~^ HELP try
|
||||
x.iter().map(|&y| { y }); //~ ERROR you seem to be using .map()
|
||||
//~^ HELP try
|
||||
x.iter().map(|y| { *y }); //~ ERROR you seem to be using .map()
|
||||
//~^ HELP try
|
||||
x.iter().map(Clone::clone); //~ ERROR you seem to be using .map()
|
||||
//~^ HELP try
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue