Skip to content

Commit 4d1f2bc

Browse files
committed
extract conditions for single_char_pattern into its own module
1 parent 7a7fcc0 commit 4d1f2bc

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,17 +1787,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17871787
inefficient_to_string::check(cx, expr, method_call.ident.name, args);
17881788
single_char_add_str::check(cx, expr, args);
17891789
into_iter_on_ref::check(cx, expr, *method_span, method_call.ident.name, args);
1790-
1791-
match cx.typeck_results().expr_ty_adjusted(&args[0]).kind() {
1792-
ty::Ref(_, ty, _) if *ty.kind() == ty::Str => {
1793-
for &(method, pos) in &PATTERN_METHODS {
1794-
if method_call.ident.name.as_str() == method && args.len() > pos {
1795-
single_char_pattern::check(cx, expr, &args[pos]);
1796-
}
1797-
}
1798-
},
1799-
_ => (),
1800-
}
1790+
single_char_pattern::check(cx, expr, method_call.ident.name, args);
18011791
},
18021792
hir::ExprKind::Binary(op, ref lhs, ref rhs)
18031793
if op.node == hir::BinOpKind::Eq || op.node == hir::BinOpKind::Ne =>
Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,35 @@
11
use crate::methods::get_hint_if_single_char_arg;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
3+
use if_chain::if_chain;
34
use rustc_errors::Applicability;
45
use rustc_hir as hir;
56
use rustc_lint::LateContext;
7+
use rustc_middle::ty;
8+
use rustc_span::symbol::Symbol;
69

710
use super::SINGLE_CHAR_PATTERN;
811

912
/// lint for length-1 `str`s for methods in `PATTERN_METHODS`
10-
pub(super) fn check(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
11-
let mut applicability = Applicability::MachineApplicable;
12-
if let Some(hint) = get_hint_if_single_char_arg(cx, arg, &mut applicability) {
13-
span_lint_and_sugg(
14-
cx,
15-
SINGLE_CHAR_PATTERN,
16-
arg.span,
17-
"single-character string constant used as pattern",
18-
"try using a `char` instead",
19-
hint,
20-
applicability,
21-
);
13+
pub(super) fn check(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, method_name: Symbol, args: &[hir::Expr<'_>]) {
14+
for &(method, pos) in &crate::methods::PATTERN_METHODS {
15+
if_chain! {
16+
if let ty::Ref(_, ty, _) = cx.typeck_results().expr_ty_adjusted(&args[0]).kind();
17+
if *ty.kind() == ty::Str;
18+
if method_name.as_str() == method && args.len() > pos;
19+
let arg = &args[pos];
20+
let mut applicability = Applicability::MachineApplicable;
21+
if let Some(hint) = get_hint_if_single_char_arg(cx, arg, &mut applicability);
22+
then {
23+
span_lint_and_sugg(
24+
cx,
25+
SINGLE_CHAR_PATTERN,
26+
arg.span,
27+
"single-character string constant used as pattern",
28+
"try using a `char` instead",
29+
hint,
30+
applicability,
31+
);
32+
}
33+
}
2234
}
2335
}

0 commit comments

Comments
 (0)