Skip to content

Commit

Permalink
Use iter::zip in src/tools/clippy/
Browse files Browse the repository at this point in the history
  • Loading branch information
cuviper committed Mar 26, 2021
1 parent 9f6b5de commit 0dddfbf
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 35 deletions.
5 changes: 3 additions & 2 deletions clippy_lints/src/default_numeric_fallback.rs
Expand Up @@ -13,6 +13,7 @@ use rustc_middle::{
ty::{self, FloatTy, IntTy, PolyFnSig, Ty},
};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use std::iter;

declare_clippy_lint! {
/// **What it does:** Checks for usage of unconstrained numeric literals which may cause default numeric fallback in type
Expand Down Expand Up @@ -107,7 +108,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
match &expr.kind {
ExprKind::Call(func, args) => {
if let Some(fn_sig) = fn_sig_opt(self.cx, func.hir_id) {
for (expr, bound) in args.iter().zip(fn_sig.skip_binder().inputs().iter()) {
for (expr, bound) in iter::zip(*args, fn_sig.skip_binder().inputs()) {
// Push found arg type, then visit arg.
self.ty_bounds.push(TyBound::Ty(bound));
self.visit_expr(expr);
Expand All @@ -120,7 +121,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
ExprKind::MethodCall(_, _, args, _) => {
if let Some(def_id) = self.cx.typeck_results().type_dependent_def_id(expr.hir_id) {
let fn_sig = self.cx.tcx.fn_sig(def_id).skip_binder();
for (expr, bound) in args.iter().zip(fn_sig.inputs().iter()) {
for (expr, bound) in iter::zip(*args, fn_sig.inputs()) {
self.ty_bounds.push(TyBound::Ty(bound));
self.visit_expr(expr);
self.ty_bounds.pop();
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/lib.rs
Expand Up @@ -4,6 +4,7 @@
#![feature(box_syntax)]
#![feature(drain_filter)]
#![feature(in_band_lifetimes)]
#![feature(iter_zip)]
#![feature(once_cell)]
#![cfg_attr(bootstrap, feature(or_patterns))]
#![feature(rustc_private)]
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/literal_representation.rs
Expand Up @@ -13,6 +13,7 @@ use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use std::iter;

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

let group_sizes: Vec<usize> = num_lit.integer.split('_').map(str::len).collect();
if UUID_GROUP_LENS.len() == group_sizes.len() {
UUID_GROUP_LENS.iter().zip(&group_sizes).all(|(&a, &b)| a == b)
iter::zip(&UUID_GROUP_LENS, &group_sizes).all(|(&a, &b)| a == b)
} else {
false
}
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/loops/needless_range_loop.rs
Expand Up @@ -17,7 +17,7 @@ use rustc_middle::hir::map::Map;
use rustc_middle::middle::region;
use rustc_middle::ty::{self, Ty};
use rustc_span::symbol::{sym, Symbol};
use std::iter::Iterator;
use std::iter::{self, Iterator};
use std::mem;

/// Checks for looping over a range and then indexing a sequence with it.
Expand Down Expand Up @@ -369,7 +369,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {
},
ExprKind::MethodCall(_, _, args, _) => {
let def_id = self.cx.typeck_results().type_dependent_def_id(expr.hir_id).unwrap();
for (ty, expr) in self.cx.tcx.fn_sig(def_id).inputs().skip_binder().iter().zip(args) {
for (ty, expr) in iter::zip(self.cx.tcx.fn_sig(def_id).inputs().skip_binder(), args) {
self.prefer_mutable = false;
if let ty::Ref(_, _, mutbl) = *ty.kind() {
if mutbl == Mutability::Mut {
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/matches.rs
Expand Up @@ -29,6 +29,7 @@ use rustc_span::source_map::{Span, Spanned};
use rustc_span::sym;
use std::cmp::Ordering;
use std::collections::hash_map::Entry;
use std::iter;
use std::ops::Bound;

declare_clippy_lint! {
Expand Down Expand Up @@ -1668,7 +1669,7 @@ where

values.sort();

for (a, b) in values.iter().zip(values.iter().skip(1)) {
for (a, b) in iter::zip(&values, &values[1..]) {
match (a, b) {
(&Kind::Start(_, ra), &Kind::End(_, rb)) => {
if ra.node != rb.node {
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/mut_key.rs
Expand Up @@ -6,6 +6,7 @@ use rustc_middle::ty::TypeFoldable;
use rustc_middle::ty::{Adt, Array, RawPtr, Ref, Slice, Tuple, Ty, TypeAndMut};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
use std::iter;

declare_clippy_lint! {
/// **What it does:** Checks for sets/maps with mutable key types.
Expand Down Expand Up @@ -87,7 +88,7 @@ impl<'tcx> LateLintPass<'tcx> for MutableKeyType {
fn check_sig<'tcx>(cx: &LateContext<'tcx>, item_hir_id: hir::HirId, decl: &hir::FnDecl<'_>) {
let fn_def_id = cx.tcx.hir().local_def_id(item_hir_id);
let fn_sig = cx.tcx.fn_sig(fn_def_id);
for (hir_ty, ty) in decl.inputs.iter().zip(fn_sig.inputs().skip_binder().iter()) {
for (hir_ty, ty) in iter::zip(decl.inputs, fn_sig.inputs().skip_binder()) {
check_ty(cx, hir_ty.span, ty);
}
check_ty(cx, decl.output.span(), cx.tcx.erase_late_bound_regions(fn_sig.output()));
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/mut_reference.rs
Expand Up @@ -4,6 +4,7 @@ use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::{self, Ty};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use std::iter;

declare_clippy_lint! {
/// **What it does:** Detects passing a mutable reference to a function that only
Expand Down Expand Up @@ -64,7 +65,7 @@ fn check_arguments<'tcx>(
match type_definition.kind() {
ty::FnDef(..) | ty::FnPtr(_) => {
let parameters = type_definition.fn_sig(cx.tcx).skip_binder().inputs();
for (argument, parameter) in arguments.iter().zip(parameters.iter()) {
for (argument, parameter) in iter::zip(arguments, parameters) {
match parameter.kind() {
ty::Ref(_, _, Mutability::Not)
| ty::RawPtr(ty::TypeAndMut {
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/pass_by_ref_or_value.rs
@@ -1,4 +1,5 @@
use std::cmp;
use std::iter;

use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_self_ty;
Expand Down Expand Up @@ -122,7 +123,7 @@ impl<'tcx> PassByRefOrValue {

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

for (index, (input, &ty)) in decl.inputs.iter().zip(fn_sig.inputs()).enumerate() {
for (index, (input, &ty)) in iter::zip(decl.inputs, fn_sig.inputs()).enumerate() {
// All spans generated from a proc-macro invocation are the same...
match span {
Some(s) if s == input.span => return,
Expand Down
3 changes: 2 additions & 1 deletion clippy_lints/src/pattern_type_mismatch.rs
Expand Up @@ -10,6 +10,7 @@ use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{AdtDef, FieldDef, Ty, TyKind, VariantDef};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::source_map::Span;
use std::iter;

declare_clippy_lint! {
/// **What it does:** Checks for patterns that aren't exact representations of the types
Expand Down Expand Up @@ -134,7 +135,7 @@ impl<'tcx> LateLintPass<'tcx> for PatternTypeMismatch {
hir_id: HirId,
) {
if let Some(fn_sig) = cx.typeck_results().liberated_fn_sigs().get(hir_id) {
for (param, ty) in body.params.iter().zip(fn_sig.inputs().iter()) {
for (param, ty) in iter::zip(body.params, fn_sig.inputs()) {
apply_lint(cx, &param.pat, ty, DerefPossible::Impossible);
}
}
Expand Down
31 changes: 13 additions & 18 deletions clippy_lints/src/unnecessary_sort_by.rs
Expand Up @@ -9,6 +9,7 @@ use rustc_middle::ty::{self, subst::GenericArgKind};
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::sym;
use rustc_span::symbol::Ident;
use std::iter;

declare_clippy_lint! {
/// **What it does:**
Expand Down Expand Up @@ -79,17 +80,15 @@ fn mirrored_exprs(
mirrored_exprs(cx, left_expr, a_ident, right_expr, b_ident)
},
// Two arrays with mirrored contents
(ExprKind::Array(left_exprs), ExprKind::Array(right_exprs)) => left_exprs
.iter()
.zip(right_exprs.iter())
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident)),
(ExprKind::Array(left_exprs), ExprKind::Array(right_exprs)) => {
iter::zip(*left_exprs, *right_exprs)
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident))
}
// The two exprs are function calls.
// Check to see that the function itself and its arguments are mirrored
(ExprKind::Call(left_expr, left_args), ExprKind::Call(right_expr, right_args)) => {
mirrored_exprs(cx, left_expr, a_ident, right_expr, b_ident)
&& left_args
.iter()
.zip(right_args.iter())
&& iter::zip(*left_args, *right_args)
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident))
},
// The two exprs are method calls.
Expand All @@ -100,16 +99,14 @@ fn mirrored_exprs(
ExprKind::MethodCall(right_segment, _, right_args, _),
) => {
left_segment.ident == right_segment.ident
&& left_args
.iter()
.zip(right_args.iter())
&& iter::zip(*left_args, *right_args)
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident))
},
}
// Two tuples with mirrored contents
(ExprKind::Tup(left_exprs), ExprKind::Tup(right_exprs)) => left_exprs
.iter()
.zip(right_exprs.iter())
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident)),
(ExprKind::Tup(left_exprs), ExprKind::Tup(right_exprs)) => {
iter::zip(*left_exprs, *right_exprs)
.all(|(left, right)| mirrored_exprs(cx, left, a_ident, right, b_ident))
}
// Two binary ops, which are the same operation and which have mirrored arguments
(ExprKind::Binary(left_op, left_left, left_right), ExprKind::Binary(right_op, right_left, right_right)) => {
left_op.node == right_op.node
Expand Down Expand Up @@ -146,9 +143,7 @@ fn mirrored_exprs(
},
)),
) => {
(left_segments
.iter()
.zip(right_segments.iter())
(iter::zip(*left_segments, *right_segments)
.all(|(left, right)| left.ident == right.ident)
&& left_segments
.iter()
Expand Down
13 changes: 7 additions & 6 deletions clippy_utils/src/consts.rs
Expand Up @@ -15,6 +15,7 @@ use rustc_span::symbol::Symbol;
use std::cmp::Ordering::{self, Equal};
use std::convert::TryInto;
use std::hash::{Hash, Hasher};
use std::iter;

/// A `LitKind`-like enum to fold constant `Expr`s into.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -139,12 +140,12 @@ impl Constant {
(&Self::F64(l), &Self::F64(r)) => l.partial_cmp(&r),
(&Self::F32(l), &Self::F32(r)) => l.partial_cmp(&r),
(&Self::Bool(ref l), &Self::Bool(ref r)) => Some(l.cmp(r)),
(&Self::Tuple(ref l), &Self::Tuple(ref r)) | (&Self::Vec(ref l), &Self::Vec(ref r)) => l
.iter()
.zip(r.iter())
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
.unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
(&Self::Tuple(ref l), &Self::Tuple(ref r)) | (&Self::Vec(ref l), &Self::Vec(ref r)) => {
iter::zip(l, r)
.map(|(li, ri)| Self::partial_cmp(tcx, cmp_type, li, ri))
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
.unwrap_or_else(|| Some(l.len().cmp(&r.len())))
}
(&Self::Repeat(ref lv, ref ls), &Self::Repeat(ref rv, ref rs)) => {
match Self::partial_cmp(tcx, cmp_type, lv, rv) {
Some(Equal) => Some(ls.cmp(rs)),
Expand Down
1 change: 1 addition & 0 deletions clippy_utils/src/lib.rs
@@ -1,5 +1,6 @@
#![feature(box_patterns)]
#![feature(in_band_lifetimes)]
#![feature(iter_zip)]
#![cfg_attr(bootstrap, feature(or_patterns))]
#![feature(rustc_private)]
#![recursion_limit = "512"]
Expand Down
3 changes: 2 additions & 1 deletion clippy_utils/src/numeric_literal.rs
@@ -1,4 +1,5 @@
use rustc_ast::ast::{Lit, LitFloatType, LitIntType, LitKind};
use std::iter;

#[derive(Debug, PartialEq, Copy, Clone)]
pub enum Radix {
Expand Down Expand Up @@ -192,7 +193,7 @@ impl<'a> NumericLiteral<'a> {
}
}

for (c, i) in digits.zip((0..group_size).cycle()) {
for (c, i) in iter::zip(digits, (0..group_size).cycle()) {
if i == 0 {
output.push('_');
}
Expand Down

0 comments on commit 0dddfbf

Please sign in to comment.