mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 06:28:42 +00:00
Check type for SINGLE_CHAR_PATTERN
It’d be nicer to actually check for `Pattern` bounds but in the meantime this needs to be fixed.
This commit is contained in:
parent
0e16d9a425
commit
de9a80cd10
2 changed files with 17 additions and 7 deletions
|
@ -365,18 +365,25 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
match self_ty.sty {
|
||||||
|
ty::TyRef(_, ty) if ty.ty.sty == ty::TyStr => {
|
||||||
for &(method, pos) in &PATTERN_METHODS {
|
for &(method, pos) in &PATTERN_METHODS {
|
||||||
if name.node.as_str() == method && args.len() > pos {
|
if name.node.as_str() == method && args.len() > pos {
|
||||||
lint_single_char_pattern(cx, expr, &args[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 => {
|
||||||
if !lint_chars_next(cx, expr, lhs, rhs, op.node == BiEq) {
|
if !lint_chars_next(cx, expr, lhs, rhs, op.node == BiEq) {
|
||||||
lint_chars_next(cx, expr, rhs, lhs, op.node == BiEq);
|
lint_chars_next(cx, expr, rhs, lhs, op.node == BiEq);
|
||||||
|
@ -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…
Add table
Reference in a new issue