Skip to content

Commit 0c8d143

Browse files
committed
move into_iter_on_ref and single_char_pattern to their own modules
1 parent 805aa47 commit 0c8d143

File tree

3 files changed

+74
-58
lines changed

3 files changed

+74
-58
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
use crate::utils::{has_iter_method, match_trait_method, paths, span_lint_and_sugg};
2+
use rustc_errors::Applicability;
3+
use rustc_hir as hir;
4+
use rustc_lint::LateContext;
5+
use rustc_middle::ty::{self, Ty};
6+
use rustc_span::source_map::Span;
7+
8+
use super::INTO_ITER_ON_REF;
9+
10+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_>, method_span: Span) {
11+
if !match_trait_method(cx, expr, &paths::INTO_ITERATOR) {
12+
return;
13+
}
14+
if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) {
15+
span_lint_and_sugg(
16+
cx,
17+
INTO_ITER_ON_REF,
18+
method_span,
19+
&format!(
20+
"this `.into_iter()` call is equivalent to `.{}()` and will not consume the `{}`",
21+
method_name, kind,
22+
),
23+
"call directly",
24+
method_name.to_string(),
25+
Applicability::MachineApplicable,
26+
);
27+
}
28+
}
29+
30+
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(&'static str, &'static str)> {
31+
has_iter_method(cx, self_ref_ty).map(|ty_name| {
32+
let mutbl = match self_ref_ty.kind() {
33+
ty::Ref(_, _, mutbl) => mutbl,
34+
_ => unreachable!(),
35+
};
36+
let method_name = match mutbl {
37+
hir::Mutability::Not => "iter",
38+
hir::Mutability::Mut => "iter_mut",
39+
};
40+
(ty_name, method_name)
41+
})
42+
}

clippy_lints/src/methods/mod.rs

Lines changed: 9 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod get_unwrap;
99
mod implicit_clone;
1010
mod inefficient_to_string;
1111
mod inspect_for_each;
12+
mod into_iter_on_ref;
1213
mod iter_cloned_collect;
1314
mod iter_count;
1415
mod iter_next_slice;
@@ -21,6 +22,7 @@ mod ok_expect;
2122
mod option_as_ref_deref;
2223
mod option_map_unwrap_or;
2324
mod single_char_insert_string;
25+
mod single_char_pattern;
2426
mod single_char_push_string;
2527
mod skip_while_next;
2628
mod string_extend_chars;
@@ -53,12 +55,11 @@ use rustc_typeck::hir_ty_to_ty;
5355
use crate::utils::eager_or_lazy::is_lazyness_candidate;
5456
use crate::utils::usage::mutated_variables;
5557
use crate::utils::{
56-
contains_return, contains_ty, get_parent_expr, get_trait_def_id, has_iter_method, implements_trait, in_macro,
57-
is_copy, is_expn_of, is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath,
58-
match_trait_method, match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths,
59-
remove_blocks, return_ty, single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite,
60-
span_lint, span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, sugg, walk_ptrs_ty_depth,
61-
SpanlessEq,
58+
contains_return, contains_ty, get_parent_expr, get_trait_def_id, implements_trait, in_macro, is_copy, is_expn_of,
59+
is_type_diagnostic_item, iter_input_pats, last_path_segment, match_def_path, match_qpath, match_trait_method,
60+
match_type, meets_msrv, method_calls, method_chain_args, path_to_local_id, paths, remove_blocks, return_ty,
61+
single_segment_path, snippet, snippet_with_applicability, snippet_with_macro_callsite, span_lint,
62+
span_lint_and_help, span_lint_and_sugg, span_lint_and_then, strip_pat_refs, sugg, walk_ptrs_ty_depth, SpanlessEq,
6263
};
6364

6465
declare_clippy_lint! {
@@ -1789,12 +1790,12 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17891790
ty::Ref(_, ty, _) if *ty.kind() == ty::Str => {
17901791
for &(method, pos) in &PATTERN_METHODS {
17911792
if method_call.ident.name.as_str() == method && args.len() > pos {
1792-
lint_single_char_pattern(cx, expr, &args[pos]);
1793+
single_char_pattern::check(cx, expr, &args[pos]);
17931794
}
17941795
}
17951796
},
17961797
ty::Ref(..) if method_call.ident.name == sym::into_iter => {
1797-
lint_into_iter(cx, expr, self_ty, *method_span);
1798+
into_iter_on_ref::check(cx, expr, self_ty, *method_span);
17981799
},
17991800
_ => (),
18001801
}
@@ -3202,22 +3203,6 @@ fn get_hint_if_single_char_arg(
32023203
}
32033204
}
32043205

3205-
/// lint for length-1 `str`s for methods in `PATTERN_METHODS`
3206-
fn lint_single_char_pattern(cx: &LateContext<'_>, _expr: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
3207-
let mut applicability = Applicability::MachineApplicable;
3208-
if let Some(hint) = get_hint_if_single_char_arg(cx, arg, &mut applicability) {
3209-
span_lint_and_sugg(
3210-
cx,
3211-
SINGLE_CHAR_PATTERN,
3212-
arg.span,
3213-
"single-character string constant used as pattern",
3214-
"try using a `char` instead",
3215-
hint,
3216-
applicability,
3217-
);
3218-
}
3219-
}
3220-
32213206
/// Checks for the `USELESS_ASREF` lint.
32223207
fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_ref_args: &[hir::Expr<'_>]) {
32233208
// when we get here, we've already checked that the call name is "as_ref" or "as_mut"
@@ -3254,40 +3239,6 @@ fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_re
32543239
}
32553240
}
32563241

3257-
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(Symbol, &'static str)> {
3258-
has_iter_method(cx, self_ref_ty).map(|ty_name| {
3259-
let mutbl = match self_ref_ty.kind() {
3260-
ty::Ref(_, _, mutbl) => mutbl,
3261-
_ => unreachable!(),
3262-
};
3263-
let method_name = match mutbl {
3264-
hir::Mutability::Not => "iter",
3265-
hir::Mutability::Mut => "iter_mut",
3266-
};
3267-
(ty_name, method_name)
3268-
})
3269-
}
3270-
3271-
fn lint_into_iter(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_>, method_span: Span) {
3272-
if !match_trait_method(cx, expr, &paths::INTO_ITERATOR) {
3273-
return;
3274-
}
3275-
if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) {
3276-
span_lint_and_sugg(
3277-
cx,
3278-
INTO_ITER_ON_REF,
3279-
method_span,
3280-
&format!(
3281-
"this `.into_iter()` call is equivalent to `.{}()` and will not consume the `{}`",
3282-
method_name, kind,
3283-
),
3284-
"call directly",
3285-
method_name.to_string(),
3286-
Applicability::MachineApplicable,
3287-
);
3288-
}
3289-
}
3290-
32913242
const FN_HEADER: hir::FnHeader = hir::FnHeader {
32923243
unsafety: hir::Unsafety::Normal,
32933244
constness: hir::Constness::NotConst,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use crate::methods::get_hint_if_single_char_arg;
2+
use crate::utils::span_lint_and_sugg;
3+
use rustc_errors::Applicability;
4+
use rustc_hir as hir;
5+
use rustc_lint::LateContext;
6+
7+
use super::SINGLE_CHAR_PATTERN;
8+
9+
/// 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+
);
22+
}
23+
}

0 commit comments

Comments
 (0)