Skip to content

Commit 7a7fcc0

Browse files
committed
extract condition for into_iter_on_ref to its own module
1 parent d380769 commit 7a7fcc0

File tree

2 files changed

+31
-23
lines changed

2 files changed

+31
-23
lines changed

clippy_lints/src/methods/into_iter_on_ref.rs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,43 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::ty::has_iter_method;
33
use clippy_utils::{match_trait_method, paths};
4+
use if_chain::if_chain;
45
use rustc_errors::Applicability;
56
use rustc_hir as hir;
67
use rustc_lint::LateContext;
78
use rustc_middle::ty::{self, Ty};
89
use rustc_span::source_map::Span;
9-
use rustc_span::symbol::Symbol;
10+
use rustc_span::symbol::{sym, Symbol};
1011

1112
use super::INTO_ITER_ON_REF;
1213

13-
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, self_ref_ty: Ty<'_>, method_span: Span) {
14-
if !match_trait_method(cx, expr, &paths::INTO_ITERATOR) {
15-
return;
16-
}
17-
if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ref_ty) {
18-
span_lint_and_sugg(
19-
cx,
20-
INTO_ITER_ON_REF,
21-
method_span,
22-
&format!(
23-
"this `.into_iter()` call is equivalent to `.{}()` and will not consume the `{}`",
24-
method_name, kind,
25-
),
26-
"call directly",
27-
method_name.to_string(),
28-
Applicability::MachineApplicable,
29-
);
14+
pub(super) fn check(
15+
cx: &LateContext<'_>,
16+
expr: &hir::Expr<'_>,
17+
method_span: Span,
18+
method_name: Symbol,
19+
args: &[hir::Expr<'_>],
20+
) {
21+
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
22+
if_chain! {
23+
if let ty::Ref(..) = self_ty.kind();
24+
if method_name == sym::into_iter;
25+
if match_trait_method(cx, expr, &paths::INTO_ITERATOR);
26+
if let Some((kind, method_name)) = ty_has_iter_method(cx, self_ty);
27+
then {
28+
span_lint_and_sugg(
29+
cx,
30+
INTO_ITER_ON_REF,
31+
method_span,
32+
&format!(
33+
"this `.into_iter()` call is equivalent to `.{}()` and will not consume the `{}`",
34+
method_name, kind,
35+
),
36+
"call directly",
37+
method_name.to_string(),
38+
Applicability::MachineApplicable,
39+
);
40+
}
3041
}
3142
}
3243

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1786,19 +1786,16 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17861786
clone_on_ref_ptr::check(cx, expr, method_call.ident.name, args);
17871787
inefficient_to_string::check(cx, expr, method_call.ident.name, args);
17881788
single_char_add_str::check(cx, expr, args);
1789+
into_iter_on_ref::check(cx, expr, *method_span, method_call.ident.name, args);
17891790

1790-
let self_ty = cx.typeck_results().expr_ty_adjusted(&args[0]);
1791-
match self_ty.kind() {
1791+
match cx.typeck_results().expr_ty_adjusted(&args[0]).kind() {
17921792
ty::Ref(_, ty, _) if *ty.kind() == ty::Str => {
17931793
for &(method, pos) in &PATTERN_METHODS {
17941794
if method_call.ident.name.as_str() == method && args.len() > pos {
17951795
single_char_pattern::check(cx, expr, &args[pos]);
17961796
}
17971797
}
17981798
},
1799-
ty::Ref(..) if method_call.ident.name == sym::into_iter => {
1800-
into_iter_on_ref::check(cx, expr, self_ty, *method_span);
1801-
},
18021799
_ => (),
18031800
}
18041801
},

0 commit comments

Comments
 (0)