mirror of
https://github.com/rust-lang/rust.git
synced 2025-06-04 19:29:07 +00:00
Merge pull request #867 from Manishearth/#830
Check type for `SINGLE_CHAR_PATTERN`
This commit is contained in:
commit
30f7651271
@ -365,16 +365,23 @@ impl LateLintPass for MethodsPass {
|
|||||||
lint_cstring_as_ptr(cx, expr, &arglists[0][0], &arglists[1][0]);
|
lint_cstring_as_ptr(cx, expr, &arglists[0][0], &arglists[1][0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lint_or_fun_call(cx, expr, &name.node.as_str(), &args);
|
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" {
|
if args.len() == 1 && name.node.as_str() == "clone" {
|
||||||
lint_clone_on_copy(cx, expr);
|
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 {
|
match self_ty.sty {
|
||||||
lint_single_char_pattern(cx, expr, &args[pos]);
|
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 => {
|
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.
|
/// Checks for the `CLONE_DOUBLE_REF` lint.
|
||||||
fn lint_clone_double_ref(cx: &LateContext, expr: &Expr, arg: &Expr) {
|
fn lint_clone_double_ref(cx: &LateContext, expr: &Expr, arg: &Expr, ty: ty::Ty) {
|
||||||
let ty = cx.tcx.expr_ty(arg);
|
|
||||||
if let ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) = ty.sty {
|
if let ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) = ty.sty {
|
||||||
if let ty::TyRef(..) = inner.sty {
|
if let ty::TyRef(..) = inner.sty {
|
||||||
let mut db = span_lint(cx,
|
let mut db = span_lint(cx,
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::collections::HashSet;
|
||||||
use std::ops::Mul;
|
use std::ops::Mul;
|
||||||
|
|
||||||
struct T;
|
struct T;
|
||||||
@ -469,6 +470,9 @@ fn single_char_pattern() {
|
|||||||
//~^ ERROR single-character string constant used as pattern
|
//~^ ERROR single-character string constant used as pattern
|
||||||
//~| HELP try using a char instead:
|
//~| HELP try using a char instead:
|
||||||
//~| SUGGESTION x.trim_right_matches('x');
|
//~| SUGGESTION x.trim_right_matches('x');
|
||||||
|
|
||||||
|
let h = HashSet::<String>::new();
|
||||||
|
h.contains("X"); // should not warn
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(result_unwrap_used)]
|
#[allow(result_unwrap_used)]
|
||||||
|
Loading…
Reference in New Issue
Block a user