From de9a80cd102021c05ad0352369abe5bdf38ab57d Mon Sep 17 00:00:00 2001 From: mcarton Date: Wed, 20 Apr 2016 21:09:38 +0200 Subject: [PATCH] Check type for `SINGLE_CHAR_PATTERN` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit It’d be nicer to actually check for `Pattern` bounds but in the meantime this needs to be fixed. --- src/methods.rs | 20 +++++++++++++------- tests/compile-fail/methods.rs | 4 ++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/methods.rs b/src/methods.rs index 4a31a564a..73f6a7aa4 100644 --- a/src/methods.rs +++ b/src/methods.rs @@ -365,16 +365,23 @@ impl LateLintPass for MethodsPass { lint_cstring_as_ptr(cx, expr, &arglists[0][0], &arglists[1][0]); } - lint_or_fun_call(cx, expr, &name.node.as_str(), &args); + + let self_ty = cx.tcx.expr_ty_adjusted(&args[0]); if args.len() == 1 && name.node.as_str() == "clone" { lint_clone_on_copy(cx, expr); - lint_clone_double_ref(cx, expr, &args[0]); + lint_clone_double_ref(cx, expr, &args[0], self_ty); } - for &(method, pos) in &PATTERN_METHODS { - if name.node.as_str() == method && args.len() > pos { - lint_single_char_pattern(cx, expr, &args[pos]); + + match self_ty.sty { + ty::TyRef(_, ty) if ty.ty.sty == ty::TyStr => { + for &(method, pos) in &PATTERN_METHODS { + if name.node.as_str() == method && args.len() > pos { + lint_single_char_pattern(cx, expr, &args[pos]); + } + } } + _ => (), } } ExprBinary(op, ref lhs, ref rhs) if op.node == BiEq || op.node == BiNe => { @@ -552,8 +559,7 @@ fn lint_clone_on_copy(cx: &LateContext, expr: &Expr) { } /// Checks for the `CLONE_DOUBLE_REF` lint. -fn lint_clone_double_ref(cx: &LateContext, expr: &Expr, arg: &Expr) { - let ty = cx.tcx.expr_ty(arg); +fn lint_clone_double_ref(cx: &LateContext, expr: &Expr, arg: &Expr, ty: ty::Ty) { if let ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) = ty.sty { if let ty::TyRef(..) = inner.sty { let mut db = span_lint(cx, diff --git a/tests/compile-fail/methods.rs b/tests/compile-fail/methods.rs index 1869fd12a..7503cb507 100644 --- a/tests/compile-fail/methods.rs +++ b/tests/compile-fail/methods.rs @@ -6,6 +6,7 @@ use std::collections::BTreeMap; use std::collections::HashMap; +use std::collections::HashSet; use std::ops::Mul; struct T; @@ -469,6 +470,9 @@ fn single_char_pattern() { //~^ ERROR single-character string constant used as pattern //~| HELP try using a char instead: //~| SUGGESTION x.trim_right_matches('x'); + + let h = HashSet::::new(); + h.contains("X"); // should not warn } #[allow(result_unwrap_used)]