Skip to content

Commit 841f219

Browse files
committed
Auto merge of #10416 - Jarcho:explicit_iter_loop_ext, r=Manishearth
Extend `explicit_iter_loop` and `explicit_into_iter_loop` fixes #1518 Some included cleanups * Split `for_loop` test into different files for each lint (partially). * Move handling of some `into_iter` cases from `explicit_into_iter`. --- changelog: Enhancement: [`explicit_iter_loop`]: Now also handles types that implement `IntoIterator`. [#10416](#10416) changelog: Sugg: [`explicit_into_iter_loop`]: The suggestion now works on mutable references. [#10416](#10416) <!-- changelog_checked -->
2 parents 21e6235 + 949712c commit 841f219

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1168
-864
lines changed

clippy_lints/src/attrs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute, msrv: &Msr
808808
}
809809

810810
fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
811-
for item in items.iter() {
811+
for item in items {
812812
if let NestedMetaItem::MetaItem(meta) = item {
813813
if !meta.has_name(sym::any) && !meta.has_name(sym::all) {
814814
continue;
@@ -842,7 +842,7 @@ fn check_nested_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
842842
}
843843

844844
fn check_nested_misused_cfg(cx: &EarlyContext<'_>, items: &[NestedMetaItem]) {
845-
for item in items.iter() {
845+
for item in items {
846846
if let NestedMetaItem::MetaItem(meta) = item {
847847
if meta.has_name(sym!(features)) && let Some(val) = meta.value_str() {
848848
span_lint_and_sugg(

clippy_lints/src/default_numeric_fallback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
161161
let fields_def = &variant.fields;
162162

163163
// Push field type then visit each field expr.
164-
for field in fields.iter() {
164+
for field in *fields {
165165
let bound =
166166
fields_def
167167
.iter()

clippy_lints/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
680680
});
681681
store.register_late_pass(|_| Box::<shadow::Shadow>::default());
682682
store.register_late_pass(|_| Box::new(unit_types::UnitTypes));
683-
store.register_late_pass(|_| Box::new(loops::Loops));
683+
store.register_late_pass(move |_| Box::new(loops::Loops::new(msrv())));
684684
store.register_late_pass(|_| Box::<main_recursion::MainRecursion>::default());
685685
store.register_late_pass(|_| Box::new(lifetimes::Lifetimes));
686686
store.register_late_pass(|_| Box::new(entry::HashMapPass));

clippy_lints/src/lifetimes.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -562,7 +562,7 @@ fn has_where_lifetimes<'tcx>(cx: &LateContext<'tcx>, generics: &'tcx Generics<'_
562562
// if the bounds define new lifetimes, they are fine to occur
563563
let allowed_lts = allowed_lts_from(pred.bound_generic_params);
564564
// now walk the bounds
565-
for bound in pred.bounds.iter() {
565+
for bound in pred.bounds {
566566
walk_param_bound(&mut visitor, bound);
567567
}
568568
// and check that all lifetimes are allowed

clippy_lints/src/loops/explicit_into_iter_loop.rs

+65-4
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,76 @@ use clippy_utils::source::snippet_with_applicability;
55
use rustc_errors::Applicability;
66
use rustc_hir::Expr;
77
use rustc_lint::LateContext;
8+
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
89
use rustc_span::symbol::sym;
910

11+
#[derive(Clone, Copy)]
12+
enum AdjustKind {
13+
None,
14+
Borrow,
15+
BorrowMut,
16+
Reborrow,
17+
ReborrowMut,
18+
}
19+
impl AdjustKind {
20+
fn borrow(mutbl: AutoBorrowMutability) -> Self {
21+
match mutbl {
22+
AutoBorrowMutability::Not => Self::Borrow,
23+
AutoBorrowMutability::Mut { .. } => Self::BorrowMut,
24+
}
25+
}
26+
27+
fn reborrow(mutbl: AutoBorrowMutability) -> Self {
28+
match mutbl {
29+
AutoBorrowMutability::Not => Self::Reborrow,
30+
AutoBorrowMutability::Mut { .. } => Self::ReborrowMut,
31+
}
32+
}
33+
34+
fn display(self) -> &'static str {
35+
match self {
36+
Self::None => "",
37+
Self::Borrow => "&",
38+
Self::BorrowMut => "&mut ",
39+
Self::Reborrow => "&*",
40+
Self::ReborrowMut => "&mut *",
41+
}
42+
}
43+
}
44+
1045
pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<'_>) {
11-
let self_ty = cx.typeck_results().expr_ty(self_arg);
12-
let self_ty_adjusted = cx.typeck_results().expr_ty_adjusted(self_arg);
13-
if !(self_ty == self_ty_adjusted && is_trait_method(cx, call_expr, sym::IntoIterator)) {
46+
if !is_trait_method(cx, call_expr, sym::IntoIterator) {
1447
return;
1548
}
1649

50+
let typeck = cx.typeck_results();
51+
let self_ty = typeck.expr_ty(self_arg);
52+
let adjust = match typeck.expr_adjustments(self_arg) {
53+
[] => AdjustKind::None,
54+
&[
55+
Adjustment {
56+
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
57+
..
58+
},
59+
] => AdjustKind::borrow(mutbl),
60+
&[
61+
Adjustment {
62+
kind: Adjust::Deref(_), ..
63+
},
64+
Adjustment {
65+
kind: Adjust::Borrow(AutoBorrow::Ref(_, mutbl)),
66+
target,
67+
},
68+
] => {
69+
if self_ty == target && matches!(mutbl, AutoBorrowMutability::Not) {
70+
AdjustKind::None
71+
} else {
72+
AdjustKind::reborrow(mutbl)
73+
}
74+
},
75+
_ => return,
76+
};
77+
1778
let mut applicability = Applicability::MachineApplicable;
1879
let object = snippet_with_applicability(cx, self_arg.span, "_", &mut applicability);
1980
span_lint_and_sugg(
@@ -23,7 +84,7 @@ pub(super) fn check(cx: &LateContext<'_>, self_arg: &Expr<'_>, call_expr: &Expr<
2384
"it is more concise to loop over containers instead of using explicit \
2485
iteration methods",
2586
"to write this more concisely, try",
26-
object.to_string(),
87+
format!("{}{object}", adjust.display()),
2788
applicability,
2889
);
2990
}

0 commit comments

Comments
 (0)