Skip to content

Commit b094bb1

Browse files
authored
Rollup merge of rust-lang#82917 - cuviper:iter-zip, r=m-ou-se
Add function core::iter::zip This makes it a little easier to `zip` iterators: ```rust for (x, y) in zip(xs, ys) {} // vs. for (x, y) in xs.into_iter().zip(ys) {} ``` You can `zip(&mut xs, &ys)` for the conventional `iter_mut()` and `iter()`, respectively. This can also support arbitrary nesting, where it's easier to see the item layout than with arbitrary `zip` chains: ```rust for ((x, y), z) in zip(zip(xs, ys), zs) {} for (x, (y, z)) in zip(xs, zip(ys, zs)) {} // vs. for ((x, y), z) in xs.into_iter().zip(ys).zip(xz) {} for (x, (y, z)) in xs.into_iter().zip((ys.into_iter().zip(xz)) {} ``` It may also format more nicely, especially when the first iterator is a longer chain of methods -- for example: ```rust iter::zip( trait_ref.substs.types().skip(1), impl_trait_ref.substs.types().skip(1), ) // vs. trait_ref .substs .types() .skip(1) .zip(impl_trait_ref.substs.types().skip(1)) ``` This replaces the tuple-pair `IntoIterator` in rust-lang#78204. There is prior art for the utility of this in [`itertools::zip`]. [`itertools::zip`]: https://docs.rs/itertools/0.10.0/itertools/fn.zip.html
2 parents ce4e668 + 0dddfbf commit b094bb1

13 files changed

+41
-35
lines changed

clippy_lints/src/default_numeric_fallback.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_middle::{
1313
ty::{self, FloatTy, IntTy, PolyFnSig, Ty},
1414
};
1515
use rustc_session::{declare_lint_pass, declare_tool_lint};
16+
use std::iter;
1617

1718
declare_clippy_lint! {
1819
/// **What it does:** Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type
@@ -107,7 +108,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
107108
match &expr.kind {
108109
ExprKind::Call(func, args) => {
109110
if let Some(fn_sig) = fn_sig_opt(self.cx, func.hir_id) {
110-
for (expr, bound) in args.iter().zip(fn_sig.skip_binder().inputs().iter()) {
111+
for (expr, bound) in iter::zip(*args, fn_sig.skip_binder().inputs()) {
111112
// Push found arg type, then visit arg.
112113
self.ty_bounds.push(TyBound::Ty(bound));
113114
self.visit_expr(expr);
@@ -120,7 +121,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
120121
ExprKind::MethodCall(_, _, args, _) => {
121122
if let Some(def_id) = self.cx.typeck_results().type_dependent_def_id(expr.hir_id) {
122123
let fn_sig = self.cx.tcx.fn_sig(def_id).skip_binder();
123-
for (expr, bound) in args.iter().zip(fn_sig.inputs().iter()) {
124+
for (expr, bound) in iter::zip(*args, fn_sig.inputs()) {
124125
self.ty_bounds.push(TyBound::Ty(bound));
125126
self.visit_expr(expr);
126127
self.ty_bounds.pop();

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(box_syntax)]
55
#![feature(drain_filter)]
66
#![feature(in_band_lifetimes)]
7+
#![feature(iter_zip)]
78
#![feature(once_cell)]
89
#![cfg_attr(bootstrap, feature(or_patterns))]
910
#![feature(rustc_private)]

clippy_lints/src/literal_representation.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use rustc_errors::Applicability;
1313
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
1414
use rustc_middle::lint::in_external_macro;
1515
use rustc_session::{declare_tool_lint, impl_lint_pass};
16+
use std::iter;
1617

1718
declare_clippy_lint! {
1819
/// **What it does:** Warns if a long integral or floating-point constant does
@@ -349,7 +350,7 @@ impl LiteralDigitGrouping {
349350

350351
let group_sizes: Vec<usize> = num_lit.integer.split('_').map(str::len).collect();
351352
if UUID_GROUP_LENS.len() == group_sizes.len() {
352-
UUID_GROUP_LENS.iter().zip(&group_sizes).all(|(&a, &b)| a == b)
353+
iter::zip(&UUID_GROUP_LENS, &group_sizes).all(|(&a, &b)| a == b)
353354
} else {
354355
false
355356
}

clippy_lints/src/loops/needless_range_loop.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::hir::map::Map;
1717
use rustc_middle::middle::region;
1818
use rustc_middle::ty::{self, Ty};
1919
use rustc_span::symbol::{sym, Symbol};
20-
use std::iter::Iterator;
20+
use std::iter::{self, Iterator};
2121
use std::mem;
2222

2323
/// Checks for looping over a range and then indexing a sequence with it.
@@ -369,7 +369,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
369369
},
370370
ExprKind::MethodCall(_, _, args, _) => {
371371
let def_id = self.cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
372-
for (ty, expr) in self.cx.tcx.fn_sig(def_id).inputs().skip_binder().iter().zip(args) {
372+
for (ty, expr) in iter::zip(self.cx.tcx.fn_sig(def_id).inputs().skip_binder(), args) {
373373
self.prefer_mutable = false;
374374
if let ty::Ref(_, _, mutbl) = *ty.kind() {
375375
if mutbl == Mutability::Mut {

clippy_lints/src/matches.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use rustc_span::source_map::{Span, Spanned};
2929
use rustc_span::sym;
3030
use std::cmp::Ordering;
3131
use std::collections::hash_map::Entry;
32+
use std::iter;
3233
use std::ops::Bound;
3334

3435
declare_clippy_lint! {
@@ -1668,7 +1669,7 @@ where
16681669

16691670
values.sort();
16701671

1671-
for (a, b) in values.iter().zip(values.iter().skip(1)) {
1672+
for (a, b) in iter::zip(&values, &values[1..]) {
16721673
match (a, b) {
16731674
(&Kind::Start(_, ra), &Kind::End(_, rb)) => {
16741675
if ra.node != rb.node {

clippy_lints/src/mut_key.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_middle::ty::TypeFoldable;
66
use rustc_middle::ty::{Adt, Array, RawPtr, Ref, Slice, Tuple, Ty, TypeAndMut};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_span::source_map::Span;
9+
use std::iter;
910

1011
declare_clippy_lint! {
1112
/// **What it does:** Checks for sets/maps with mutable key types.
@@ -87,7 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableKeyType {
8788
fn check_sig<'tcx>(cx: &LateContext<'tcx>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) {
8889
let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
8990
let fn_sig = cx.tcx.fn_sig(fn_def_id);
90-
for (hir_ty, ty) in decl.inputs.iter().zip(fn_sig.inputs().skip_binder().iter()) {
91+
for (hir_ty, ty) in iter::zip(decl.inputs, fn_sig.inputs().skip_binder()) {
9192
check_ty(cx, hir_ty.span, ty);
9293
}
9394
check_ty(cx, decl.output.span(), cx.tcx.erase_late_bound_regions(fn_sig.output()));

clippy_lints/src/mut_reference.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_lint::{LateContext, LateLintPass};
44
use rustc_middle::ty::subst::Subst;
55
use rustc_middle::ty::{self, Ty};
66
use rustc_session::{declare_lint_pass, declare_tool_lint};
7+
use std::iter;
78

89
declare_clippy_lint! {
910
/// **What it does:** Detects passing a mutable reference to a function that only
@@ -64,7 +65,7 @@ fn check_arguments<'tcx>(
6465
match type_definition.kind() {
6566
ty::FnDef(..) | ty::FnPtr(_) => {
6667
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
67-
for (argument, parameter) in arguments.iter().zip(parameters.iter()) {
68+
for (argument, parameter) in iter::zip(arguments, parameters) {
6869
match parameter.kind() {
6970
ty::Ref(_, _, Mutability::Not)
7071
| ty::RawPtr(ty::TypeAndMut {

clippy_lints/src/pass_by_ref_or_value.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::cmp;
2+
use std::iter;
23

34
use clippy_utils::diagnostics::span_lint_and_sugg;
45
use clippy_utils::is_self_ty;
@@ -122,7 +123,7 @@ impl<'tcx> PassByRefOrValue {
122123

123124
let fn_body = cx.enclosing_body.map(|id| cx.tcx.hir().body(id));
124125

125-
for (index, (input, &ty)) in decl.inputs.iter().zip(fn_sig.inputs()).enumerate() {
126+
for (index, (input, &ty)) in iter::zip(decl.inputs, fn_sig.inputs()).enumerate() {
126127
// All spans generated from a proc-macro invocation are the same...
127128
match span {
128129
Some(s) if s == input.span => return,

clippy_lints/src/pattern_type_mismatch.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use rustc_middle::ty::subst::SubstsRef;
1010
use rustc_middle::ty::{AdtDef, FieldDef, Ty, TyKind, VariantDef};
1111
use rustc_session::{declare_lint_pass, declare_tool_lint};
1212
use rustc_span::source_map::Span;
13+
use std::iter;
1314

1415
declare_clippy_lint! {
1516
/// **What it does:** Checks for patterns that aren't exact representations of the types
@@ -134,7 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
134135
hir_id: HirId,
135136
) {
136137
if let Some(fn_sig) = cx.typeck_results().liberated_fn_sigs().get(hir_id) {
137-
for (param, ty) in body.params.iter().zip(fn_sig.inputs().iter()) {
138+
for (param, ty) in iter::zip(body.params, fn_sig.inputs()) {
138139
apply_lint(cx, &param.pat, ty, DerefPossible::Impossible);
139140
}
140141
}

clippy_lints/src/unnecessary_sort_by.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_middle::ty::{self, subst::GenericArgKind};
99
use rustc_session::{declare_lint_pass, declare_tool_lint};
1010
use rustc_span::sym;
1111
use rustc_span::symbol::Ident;
12+
use std::iter;
1213

1314
declare_clippy_lint! {
1415
/// **What it does:**
@@ -79,17 +80,15 @@ fn mirrored_exprs(
7980
mirrored_exprs(cx, left_expr, a_ident, right_expr, b_ident)
8081
},
8182
// Two arrays with mirrored contents
82-
(ExprKind::Array(left_exprs), ExprKind::Array(right_exprs)) => left_exprs
83-
.iter()
84-
.zip(right_exprs.iter())
85-
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident)),
83+
(ExprKind::Array(left_exprs), ExprKind::Array(right_exprs)) => {
84+
iter::zip(*left_exprs, *right_exprs)
85+
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident))
86+
}
8687
// The two exprs are function calls.
8788
// Check to see that the function itself and its arguments are mirrored
8889
(ExprKind::Call(left_expr, left_args), ExprKind::Call(right_expr, right_args)) => {
8990
mirrored_exprs(cx, left_expr, a_ident, right_expr, b_ident)
90-
&& left_args
91-
.iter()
92-
.zip(right_args.iter())
91+
&& iter::zip(*left_args, *right_args)
9392
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident))
9493
},
9594
// The two exprs are method calls.
@@ -100,16 +99,14 @@ fn mirrored_exprs(
10099
ExprKind::MethodCall(right_segment, _, right_args, _),
101100
) => {
102101
left_segment.ident == right_segment.ident
103-
&& left_args
104-
.iter()
105-
.zip(right_args.iter())
102+
&& iter::zip(*left_args, *right_args)
106103
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident))
107-
},
104+
}
108105
// Two tuples with mirrored contents
109-
(ExprKind::Tup(left_exprs), ExprKind::Tup(right_exprs)) => left_exprs
110-
.iter()
111-
.zip(right_exprs.iter())
112-
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident)),
106+
(ExprKind::Tup(left_exprs), ExprKind::Tup(right_exprs)) => {
107+
iter::zip(*left_exprs, *right_exprs)
108+
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident))
109+
}
113110
// Two binary ops, which are the same operation and which have mirrored arguments
114111
(ExprKind::Binary(left_op, left_left, left_right), ExprKind::Binary(right_op, right_left, right_right)) => {
115112
left_op.node == right_op.node
@@ -146,9 +143,7 @@ fn mirrored_exprs(
146143
},
147144
)),
148145
) => {
149-
(left_segments
150-
.iter()
151-
.zip(right_segments.iter())
146+
(iter::zip(*left_segments, *right_segments)
152147
.all(|(left, right)| left.ident == right.ident)
153148
&& left_segments
154149
.iter()

clippy_utils/src/consts.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_span::symbol::Symbol;
1515
use std::cmp::Ordering::{self, Equal};
1616
use std::convert::TryInto;
1717
use std::hash::{Hash, Hasher};
18+
use std::iter;
1819

1920
/// A `LitKind`-like enum to fold constant `Expr`s into.
2021
#[derive(Debug, Clone)]
@@ -139,12 +140,12 @@ impl Constant {
139140
(&Self::F64(l), &Self::F64(r)) => l.partial_cmp(&r),
140141
(&Self::F32(l), &Self::F32(r)) => l.partial_cmp(&r),
141142
(&Self::Bool(ref l), &Self::Bool(ref r)) => Some(l.cmp(r)),
142-
(&Self::Tuple(ref l), &Self::Tuple(ref r)) | (&Self::Vec(ref l), &Self::Vec(ref r)) => l
143-
.iter()
144-
.zip(r.iter())
145-
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
146-
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
147-
.unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
143+
(&Self::Tuple(ref l), &Self::Tuple(ref r)) | (&Self::Vec(ref l), &Self::Vec(ref r)) => {
144+
iter::zip(l, r)
145+
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
146+
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
147+
.unwrap_or_else(|| Some(l.len().cmp(&r.len())))
148+
}
148149
(&Self::Repeat(ref lv, ref ls), &Self::Repeat(ref rv, ref rs)) => {
149150
match Self::partial_cmp(tcx, cmp_type, lv, rv) {
150151
Some(Equal) => Some(ls.cmp(rs)),

clippy_utils/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![feature(box_patterns)]
22
#![feature(in_band_lifetimes)]
3+
#![feature(iter_zip)]
34
#![cfg_attr(bootstrap, feature(or_patterns))]
45
#![feature(rustc_private)]
56
#![recursion_limit = "512"]

clippy_utils/src/numeric_literal.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_ast::ast::{Lit, LitFloatType, LitIntType, LitKind};
2+
use std::iter;
23

34
#[derive(Debug, PartialEq, Copy, Clone)]
45
pub enum Radix {
@@ -192,7 +193,7 @@ impl<'a> NumericLiteral<'a> {
192193
}
193194
}
194195

195-
for (c, i) in digits.zip((0..group_size).cycle()) {
196+
for (c, i) in iter::zip(digits, (0..group_size).cycle()) {
196197
if i == 0 {
197198
output.push('_');
198199
}

0 commit comments

Comments
 (0)