2015-10-31 04:58:37 +00:00
|
|
|
use rustc::lint::*;
|
2016-04-07 15:46:48 +00:00
|
|
|
use rustc::hir::*;
|
2016-05-19 21:14:34 +00:00
|
|
|
use syntax::ast;
|
2016-11-17 18:44:18 +00:00
|
|
|
use utils::{is_adjusted, match_path, match_trait_method, match_type, remove_blocks, paths, snippet,
|
2016-04-14 16:13:15 +00:00
|
|
|
span_help_and_lint, walk_ptrs_ty, walk_ptrs_ty_depth};
|
2015-10-31 04:58:37 +00:00
|
|
|
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **What it does:** Checks for mapping `clone()` over an iterator.
|
2015-12-14 12:31:28 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Why is this bad?** It makes the code less readable than using the
|
|
|
|
/// `.cloned()` adapter.
|
2015-12-14 12:31:28 +00:00
|
|
|
///
|
2016-08-06 07:55:04 +00:00
|
|
|
/// **Known problems:** None.
|
2015-12-14 12:31:28 +00:00
|
|
|
///
|
2016-07-15 22:25:44 +00:00
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// x.map(|e| e.clone());
|
|
|
|
/// ```
|
2016-02-05 23:13:29 +00:00
|
|
|
declare_lint! {
|
2016-08-06 08:18:36 +00:00
|
|
|
pub MAP_CLONE,
|
|
|
|
Warn,
|
|
|
|
"using `.map(|x| x.clone())` to clone an iterator or option's contents"
|
2016-02-05 23:13:29 +00:00
|
|
|
}
|
2015-10-31 04:58:37 +00:00
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
2016-06-10 14:17:20 +00:00
|
|
|
pub struct Pass;
|
2015-10-31 04:58:37 +00:00
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
impl LateLintPass for Pass {
|
2016-12-06 10:32:21 +00:00
|
|
|
fn check_expr<'a, 'tcx: 'a>(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
2015-11-05 16:11:41 +00:00
|
|
|
// call to .map()
|
|
|
|
if let ExprMethodCall(name, _, ref args) = expr.node {
|
2016-11-23 20:19:03 +00:00
|
|
|
if &*name.node.as_str() == "map" && args.len() == 2 {
|
2015-11-05 16:11:41 +00:00
|
|
|
match args[1].node {
|
2016-12-06 10:32:21 +00:00
|
|
|
ExprClosure(_, ref decl, closure_eid, _) => {
|
|
|
|
let closure_expr = remove_blocks(cx.tcx.map.expr(closure_eid));
|
2016-06-06 00:09:19 +00:00
|
|
|
if_let_chain! {[
|
2015-11-05 16:11:41 +00:00
|
|
|
// nothing special in the argument, besides reference bindings
|
|
|
|
// (e.g. .map(|&x| x) )
|
|
|
|
let Some(arg_ident) = get_arg_name(&*decl.inputs[0].pat),
|
|
|
|
// the method is being called on a known type (option or iterator)
|
|
|
|
let Some(type_name) = get_type_name(cx, expr, &args[0])
|
2016-06-06 00:09:19 +00:00
|
|
|
], {
|
|
|
|
// 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
|
2016-11-16 20:57:56 +00:00
|
|
|
walk_ptrs_ty_depth(cx.tcx.tables().pat_ty(&*decl.inputs[0].pat)).1 == 1
|
2016-06-06 00:09:19 +00: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, "..")));
|
|
|
|
}
|
|
|
|
// explicit clone() calls ( .map(|x| x.clone()) )
|
|
|
|
else if let ExprMethodCall(clone_call, _, ref clone_args) = closure_expr.node {
|
2016-11-23 20:19:03 +00:00
|
|
|
if &*clone_call.node.as_str() == "clone" &&
|
2016-06-06 00:09:19 +00:00
|
|
|
clone_args.len() == 1 &&
|
|
|
|
match_trait_method(cx, closure_expr, &paths::CLONE_TRAIT) &&
|
|
|
|
expr_eq_name(&clone_args[0], arg_ident)
|
2015-11-05 16:11:41 +00: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-06 00:09:19 +00:00
|
|
|
}}
|
2015-11-17 04:39:42 +00:00
|
|
|
}
|
2016-12-01 21:31:56 +00:00
|
|
|
ExprPath(ref path) => {
|
2016-04-14 16:13:15 +00:00
|
|
|
if match_path(path, &paths::CLONE) {
|
2015-11-05 16:11:41 +00:00
|
|
|
let type_name = get_type_name(cx, expr, &args[0]).unwrap_or("_");
|
2016-01-04 04:26:12 +00: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, "..")));
|
2015-11-05 16:11:41 +00:00
|
|
|
}
|
2015-10-31 04:58:37 +00:00
|
|
|
}
|
2015-11-05 16:11:41 +00:00
|
|
|
_ => (),
|
2015-10-31 04:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 21:14:34 +00:00
|
|
|
fn expr_eq_name(expr: &Expr, id: ast::Name) -> bool {
|
2015-10-31 04:58:37 +00:00
|
|
|
match expr.node {
|
2016-12-01 21:31:56 +00:00
|
|
|
ExprPath(QPath::Resolved(None, ref path)) => {
|
2016-01-04 04:26:12 +00:00
|
|
|
let arg_segment = [PathSegment {
|
2016-05-19 21:14:34 +00:00
|
|
|
name: id,
|
2016-01-04 04:26:12 +00:00
|
|
|
parameters: PathParameters::none(),
|
|
|
|
}];
|
2015-12-21 23:22:35 +00:00
|
|
|
!path.global && path.segments[..] == arg_segment
|
2015-11-17 04:39:42 +00:00
|
|
|
}
|
2015-10-31 04:58:37 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_type_name(cx: &LateContext, expr: &Expr, arg: &Expr) -> Option<&'static str> {
|
2016-04-26 11:31:52 +00:00
|
|
|
if match_trait_method(cx, expr, &paths::ITERATOR) {
|
2015-10-31 04:58:37 +00:00
|
|
|
Some("iterator")
|
2016-11-16 20:57:56 +00:00
|
|
|
} else if match_type(cx, walk_ptrs_ty(cx.tcx.tables().expr_ty(arg)), &paths::OPTION) {
|
2015-10-31 04:58:37 +00:00
|
|
|
Some("Option")
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 21:14:34 +00:00
|
|
|
fn get_arg_name(pat: &Pat) -> Option<ast::Name> {
|
2015-10-31 04:58:37 +00:00
|
|
|
match pat.node {
|
2016-12-01 21:31:56 +00:00
|
|
|
PatKind::Binding(_, _, name, None) => Some(name.node),
|
2016-02-18 20:16:39 +00:00
|
|
|
PatKind::Ref(ref subpat, _) => get_arg_name(subpat),
|
2015-10-31 04:58:37 +00:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-19 21:14:34 +00:00
|
|
|
fn only_derefs(cx: &LateContext, expr: &Expr, id: ast::Name) -> bool {
|
2015-11-04 03:11:40 +00:00
|
|
|
match expr.node {
|
2016-01-04 04:26:12 +00:00
|
|
|
ExprUnary(UnDeref, ref subexpr) if !is_adjusted(cx, subexpr) => only_derefs(cx, subexpr, id),
|
2016-05-19 21:14:34 +00:00
|
|
|
_ => expr_eq_name(expr, id),
|
2015-10-31 04:58:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-10 14:17:20 +00:00
|
|
|
impl LintPass for Pass {
|
2015-10-31 04:58:37 +00:00
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(MAP_CLONE)
|
|
|
|
}
|
|
|
|
}
|