2015-10-30 23:58:37 -05:00
|
|
|
use rustc::lint::*;
|
2016-04-07 10:46:48 -05:00
|
|
|
use rustc::hir::*;
|
2017-05-10 06:42:19 -05:00
|
|
|
use rustc::ty;
|
2016-05-19 16:14:34 -05:00
|
|
|
use syntax::ast;
|
2017-09-05 04:33:04 -05:00
|
|
|
use utils::{is_adjusted, iter_input_pats, match_qpath, match_trait_method, match_type, paths, remove_blocks, snippet,
|
|
|
|
span_help_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
|
2015-10-30 23:58:37 -05:00
|
|
|
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **What it does:** Checks for mapping `clone()` over an iterator.
|
2015-12-14 06:31:28 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Why is this bad?** It makes the code less readable than using the
|
|
|
|
/// `.cloned()` adapter.
|
2015-12-14 06:31:28 -06:00
|
|
|
///
|
2016-08-06 02:55:04 -05:00
|
|
|
/// **Known problems:** None.
|
2015-12-14 06:31:28 -06:00
|
|
|
///
|
2016-07-15 17:25:44 -05:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// x.map(|e| e.clone());
|
|
|
|
/// ```
|
2016-02-05 17:13:29 -06:00
|
|
|
declare_lint! {
|
2016-08-06 03:18:36 -05:00
|
|
|
pub MAP_CLONE,
|
|
|
|
Warn,
|
|
|
|
"using `.map(|x| x.clone())` to clone an iterator or option's contents"
|
2016-02-05 17:13:29 -06:00
|
|
|
}
|
2015-10-30 23:58:37 -05:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
2016-06-10 09:17:20 -05:00
|
|
|
pub struct Pass;
|
2015-10-30 23:58:37 -05:00
|
|
|
|
2016-12-07 06:13:40 -06:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
2015-11-05 10:11:41 -06:00
|
|
|
// call to .map()
|
2017-07-10 03:17:40 -05:00
|
|
|
if let ExprMethodCall(ref method, _, ref args) = expr.node {
|
|
|
|
if method.name == "map" && args.len() == 2 {
|
2015-11-05 10:11:41 -06:00
|
|
|
match args[1].node {
|
2017-08-30 03:54:24 -05:00
|
|
|
ExprClosure(_, ref decl, closure_eid, _, _) => {
|
2017-02-02 10:53:28 -06:00
|
|
|
let body = cx.tcx.hir.body(closure_eid);
|
2017-01-04 15:46:41 -06:00
|
|
|
let closure_expr = remove_blocks(&body.value);
|
2017-05-10 06:42:19 -05:00
|
|
|
let ty = cx.tables.pat_ty(&body.arguments[0].pat);
|
2017-10-23 14:18:02 -05:00
|
|
|
if_chain! {
|
2015-11-05 10:11:41 -06:00
|
|
|
// nothing special in the argument, besides reference bindings
|
|
|
|
// (e.g. .map(|&x| x) )
|
2017-10-23 14:18:02 -05:00
|
|
|
if let Some(first_arg) = iter_input_pats(decl, body).next();
|
|
|
|
if let Some(arg_ident) = get_arg_name(&first_arg.pat);
|
2015-11-05 10:11:41 -06:00
|
|
|
// the method is being called on a known type (option or iterator)
|
2017-10-23 14:18:02 -05:00
|
|
|
if let Some(type_name) = get_type_name(cx, expr, &args[0]);
|
|
|
|
then {
|
|
|
|
// look for derefs, for .map(|x| *x)
|
|
|
|
if only_derefs(cx, &*closure_expr, arg_ident) &&
|
|
|
|
// .cloned() only removes one level of indirection, don't lint on more
|
|
|
|
walk_ptrs_ty_depth(cx.tables.pat_ty(&first_arg.pat)).1 == 1
|
|
|
|
{
|
|
|
|
// the argument is not an &mut T
|
|
|
|
if let ty::TyRef(_, tam) = ty.sty {
|
|
|
|
if tam.mutbl == MutImmutable {
|
|
|
|
span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
|
|
|
|
"you seem to be using .map() to clone the contents of an {}, consider \
|
|
|
|
using `.cloned()`", type_name),
|
|
|
|
&format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// explicit clone() calls ( .map(|x| x.clone()) )
|
|
|
|
else if let ExprMethodCall(ref clone_call, _, ref clone_args) = closure_expr.node {
|
|
|
|
if clone_call.name == "clone" &&
|
|
|
|
clone_args.len() == 1 &&
|
|
|
|
match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) &&
|
|
|
|
expr_eq_name(&clone_args[0], arg_ident)
|
|
|
|
{
|
2017-05-10 06:42:19 -05:00
|
|
|
span_help_and_lint(cx, MAP_CLONE, expr.span, &format!(
|
|
|
|
"you seem to be using .map() to clone the contents of an {}, consider \
|
|
|
|
using `.cloned()`", type_name),
|
|
|
|
&format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")));
|
|
|
|
}
|
|
|
|
}
|
2016-06-05 19:09:19 -05:00
|
|
|
}
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2017-09-05 04:33:04 -05:00
|
|
|
ExprPath(ref path) => if match_qpath(path, &paths::CLONE) {
|
|
|
|
let type_name = get_type_name(cx, expr, &args[0]).unwrap_or("_");
|
|
|
|
span_help_and_lint(
|
|
|
|
cx,
|
|
|
|
MAP_CLONE,
|
|
|
|
expr.span,
|
|
|
|
&format!(
|
|
|
|
"you seem to be using .map() to clone the contents of an \
|
|
|
|
{}, consider using `.cloned()`",
|
|
|
|
type_name
|
|
|
|
),
|
|
|
|
&format!("try\n{}.cloned()", snippet(cx, args[0].span, "..")),
|
|
|
|
);
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2015-11-05 10:11:41 -06:00
|
|
|
_ => (),
|
2015-10-30 23:58:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 16:14:34 -05:00
|
|
|
fn expr_eq_name(expr: &Expr, id: ast::Name) -> bool {
|
2015-10-30 23:58:37 -05:00
|
|
|
match expr.node {
|
2016-12-01 15:31:56 -06:00
|
|
|
ExprPath(QPath::Resolved(None, ref path)) => {
|
2017-08-09 02:30:56 -05:00
|
|
|
let arg_segment = [
|
|
|
|
PathSegment {
|
|
|
|
name: id,
|
2017-09-25 21:04:55 -05:00
|
|
|
parameters: None,
|
|
|
|
infer_types: true,
|
2017-08-09 02:30:56 -05:00
|
|
|
},
|
|
|
|
];
|
2017-01-03 22:40:42 -06:00
|
|
|
!path.is_global() && path.segments[..] == arg_segment
|
2016-12-20 11:21:30 -06:00
|
|
|
},
|
2015-10-30 23:58:37 -05:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
|
2016-04-26 06:31:52 -05:00
|
|
|
if match_trait_method(cx, expr, &paths::ITERATOR) {
|
2015-10-30 23:58:37 -05:00
|
|
|
Some("iterator")
|
2017-01-13 10:04:56 -06:00
|
|
|
} else if match_type(cx, walk_ptrs_ty(cx.tables.expr_ty(arg)), &paths::OPTION) {
|
2015-10-30 23:58:37 -05:00
|
|
|
Some("Option")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 16:14:34 -05:00
|
|
|
fn get_arg_name(pat: &Pat) -> Option<ast::Name> {
|
2015-10-30 23:58:37 -05:00
|
|
|
match pat.node {
|
2016-12-01 15:31:56 -06:00
|
|
|
PatKind::Binding(_, _, name, None) => Some(name.node),
|
2016-02-18 14:16:39 -06:00
|
|
|
PatKind::Ref(ref subpat, _) => get_arg_name(subpat),
|
2015-10-30 23:58:37 -05:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 16:14:34 -05:00
|
|
|
fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Name) -> bool {
|
2015-11-03 21:11:40 -06:00
|
|
|
match expr.node {
|
2016-01-03 22:26:12 -06:00
|
|
|
ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
|
2016-05-19 16:14:34 -05:00
|
|
|
_ => expr_eq_name(expr, id),
|
2015-10-30 23:58:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 09:17:20 -05:00
|
|
|
impl LintPass for Pass {
|
2015-10-30 23:58:37 -05:00
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MAP_CLONE)
|
|
|
|
}
|
|
|
|
}
|