From 44570143989cdd529aff488b8fa5534c4802fbce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matthias=20Kr=C3=BCger?= Date: Fri, 15 Oct 2021 11:24:20 +0200 Subject: [PATCH] Revert "Auto merge of #89709 - clemenswasser:apply_clippy_suggestions_2, r=petrochenkov" The PR had some unforseen perf regressions that are not as easy to find. Revert the PR for now. This reverts commit 6ae8912a3e7d2c4c775024f58a7ba4b1aedc4073, reversing changes made to 86d6d2b7389fe1b339402c1798edae8b695fc9ef. --- compiler/rustc_apfloat/src/ieee.rs | 2 -- compiler/rustc_data_structures/src/base_n.rs | 2 +- .../src/graph/implementation/mod.rs | 10 ++++++++-- .../src/graph/iterate/mod.rs | 4 ++-- .../src/obligation_forest/mod.rs | 12 ++++++------ compiler/rustc_data_structures/src/sorted_map.rs | 8 ++++---- .../src/sorted_map/index_map.rs | 4 ++-- compiler/rustc_data_structures/src/sso/map.rs | 16 ++++++++++++---- compiler/rustc_data_structures/src/sso/set.rs | 2 +- .../rustc_data_structures/src/stable_hasher.rs | 4 ++-- compiler/rustc_data_structures/src/stack.rs | 1 - compiler/rustc_data_structures/src/steal.rs | 2 +- compiler/rustc_data_structures/src/tiny_list.rs | 2 +- .../rustc_data_structures/src/vec_linked_list.rs | 4 ++-- compiler/rustc_graphviz/src/lib.rs | 2 +- compiler/rustc_index/src/bit_set.rs | 7 ++++--- compiler/rustc_index/src/vec.rs | 11 +++++++---- compiler/rustc_lexer/src/unescape.rs | 11 ++++++----- compiler/rustc_macros/src/hash_stable.rs | 8 +++++--- compiler/rustc_macros/src/session_diagnostic.rs | 8 ++++---- compiler/rustc_serialize/src/serialize.rs | 4 ++-- library/core/src/num/fmt.rs | 4 +--- 22 files changed, 72 insertions(+), 56 deletions(-) diff --git a/compiler/rustc_apfloat/src/ieee.rs b/compiler/rustc_apfloat/src/ieee.rs index 739c6fd0a435f..96277950cfe1a 100644 --- a/compiler/rustc_apfloat/src/ieee.rs +++ b/compiler/rustc_apfloat/src/ieee.rs @@ -389,7 +389,6 @@ impl fmt::Display for IeeeFloat { let _: Loss = sig::shift_right(&mut sig, &mut exp, trailing_zeros as usize); // Change the exponent from 2^e to 10^e. - #[allow(clippy::comparison_chain)] if exp == 0 { // Nothing to do. } else if exp > 0 { @@ -2527,7 +2526,6 @@ mod sig { if *a_sign ^ b_sign { let (reverse, loss); - #[allow(clippy::comparison_chain)] if bits == 0 { reverse = cmp(a_sig, b_sig) == Ordering::Less; loss = Loss::ExactlyZero; diff --git a/compiler/rustc_data_structures/src/base_n.rs b/compiler/rustc_data_structures/src/base_n.rs index 81e2501ecbead..3c7bea2712409 100644 --- a/compiler/rustc_data_structures/src/base_n.rs +++ b/compiler/rustc_data_structures/src/base_n.rs @@ -14,7 +14,7 @@ const BASE_64: &[u8; MAX_BASE as usize] = #[inline] pub fn push_str(mut n: u128, base: usize, output: &mut String) { - debug_assert!((2..=MAX_BASE).contains(&base)); + debug_assert!(base >= 2 && base <= MAX_BASE); let mut s = [0u8; 128]; let mut index = 0; diff --git a/compiler/rustc_data_structures/src/graph/implementation/mod.rs b/compiler/rustc_data_structures/src/graph/implementation/mod.rs index 9ff401c3c7aad..1aa7ac024d94e 100644 --- a/compiler/rustc_data_structures/src/graph/implementation/mod.rs +++ b/compiler/rustc_data_structures/src/graph/implementation/mod.rs @@ -206,11 +206,17 @@ impl Graph { AdjacentEdges { graph: self, direction, next: first_edge } } - pub fn successor_nodes(&self, source: NodeIndex) -> impl Iterator + '_ { + pub fn successor_nodes<'a>( + &'a self, + source: NodeIndex, + ) -> impl Iterator + 'a { self.outgoing_edges(source).targets() } - pub fn predecessor_nodes(&self, target: NodeIndex) -> impl Iterator + '_ { + pub fn predecessor_nodes<'a>( + &'a self, + target: NodeIndex, + ) -> impl Iterator + 'a { self.incoming_edges(target).sources() } diff --git a/compiler/rustc_data_structures/src/graph/iterate/mod.rs b/compiler/rustc_data_structures/src/graph/iterate/mod.rs index 1c6979dc489a6..a9db3497b2390 100644 --- a/compiler/rustc_data_structures/src/graph/iterate/mod.rs +++ b/compiler/rustc_data_structures/src/graph/iterate/mod.rs @@ -48,7 +48,7 @@ fn post_order_walk( let node = frame.node; visited[node] = true; - for successor in frame.iter.by_ref() { + while let Some(successor) = frame.iter.next() { if !visited[successor] { stack.push(PostOrderFrame { node: successor, iter: graph.successors(successor) }); continue 'recurse; @@ -112,7 +112,7 @@ where /// This is equivalent to just invoke `next` repeatedly until /// you get a `None` result. pub fn complete_search(&mut self) { - for _ in self {} + while let Some(_) = self.next() {} } /// Returns true if node has been visited thus far. diff --git a/compiler/rustc_data_structures/src/obligation_forest/mod.rs b/compiler/rustc_data_structures/src/obligation_forest/mod.rs index caf515b0d192a..25b7a84b3a069 100644 --- a/compiler/rustc_data_structures/src/obligation_forest/mod.rs +++ b/compiler/rustc_data_structures/src/obligation_forest/mod.rs @@ -390,7 +390,7 @@ impl ObligationForest { .map(|(index, _node)| Error { error: error.clone(), backtrace: self.error_at(index) }) .collect(); - self.compress(|_| unreachable!()); + self.compress(|_| assert!(false)); errors } @@ -612,7 +612,7 @@ impl ObligationForest { fn compress(&mut self, mut outcome_cb: impl FnMut(&O)) { let orig_nodes_len = self.nodes.len(); let mut node_rewrites: Vec<_> = std::mem::take(&mut self.reused_node_vec); - assert!(node_rewrites.is_empty()); + debug_assert!(node_rewrites.is_empty()); node_rewrites.extend(0..orig_nodes_len); let mut dead_nodes = 0; @@ -623,13 +623,13 @@ impl ObligationForest { // self.nodes[0..index - dead_nodes] are the first remaining nodes // self.nodes[index - dead_nodes..index] are all dead // self.nodes[index..] are unchanged - for (index, node_rewrite) in node_rewrites.iter_mut().enumerate() { + for index in 0..orig_nodes_len { let node = &self.nodes[index]; match node.state.get() { NodeState::Pending | NodeState::Waiting => { if dead_nodes > 0 { self.nodes.swap(index, index - dead_nodes); - *node_rewrite -= dead_nodes; + node_rewrites[index] -= dead_nodes; } } NodeState::Done => { @@ -646,7 +646,7 @@ impl ObligationForest { } // Extract the success stories. outcome_cb(&node.obligation); - *node_rewrite = orig_nodes_len; + node_rewrites[index] = orig_nodes_len; dead_nodes += 1; } NodeState::Error => { @@ -655,7 +655,7 @@ impl ObligationForest { // check against. self.active_cache.remove(&node.obligation.as_cache_key()); self.insert_into_error_cache(index); - *node_rewrite = orig_nodes_len; + node_rewrites[index] = orig_nodes_len; dead_nodes += 1; } NodeState::Success => unreachable!(), diff --git a/compiler/rustc_data_structures/src/sorted_map.rs b/compiler/rustc_data_structures/src/sorted_map.rs index e80db0845a7be..20e2a3b9696e8 100644 --- a/compiler/rustc_data_structures/src/sorted_map.rs +++ b/compiler/rustc_data_structures/src/sorted_map.rs @@ -205,10 +205,10 @@ impl SortedMap { R: RangeBounds, { let start = match range.start_bound() { - Bound::Included(k) => match self.lookup_index_for(k) { + Bound::Included(ref k) => match self.lookup_index_for(k) { Ok(index) | Err(index) => index, }, - Bound::Excluded(k) => match self.lookup_index_for(k) { + Bound::Excluded(ref k) => match self.lookup_index_for(k) { Ok(index) => index + 1, Err(index) => index, }, @@ -216,11 +216,11 @@ impl SortedMap { }; let end = match range.end_bound() { - Bound::Included(k) => match self.lookup_index_for(k) { + Bound::Included(ref k) => match self.lookup_index_for(k) { Ok(index) => index + 1, Err(index) => index, }, - Bound::Excluded(k) => match self.lookup_index_for(k) { + Bound::Excluded(ref k) => match self.lookup_index_for(k) { Ok(index) | Err(index) => index, }, Bound::Unbounded => self.data.len(), diff --git a/compiler/rustc_data_structures/src/sorted_map/index_map.rs b/compiler/rustc_data_structures/src/sorted_map/index_map.rs index 1395bb16e875c..e92db9ea12805 100644 --- a/compiler/rustc_data_structures/src/sorted_map/index_map.rs +++ b/compiler/rustc_data_structures/src/sorted_map/index_map.rs @@ -75,7 +75,7 @@ impl SortedIndexMultiMap { /// /// If there are multiple items that are equivalent to `key`, they will be yielded in /// insertion order. - pub fn get_by_key(&self, key: K) -> impl Iterator { + pub fn get_by_key(&'a self, key: K) -> impl 'a + Iterator { self.get_by_key_enumerated(key).map(|(_, v)| v) } @@ -84,7 +84,7 @@ impl SortedIndexMultiMap { /// /// If there are multiple items that are equivalent to `key`, they will be yielded in /// insertion order. - pub fn get_by_key_enumerated(&self, key: K) -> impl Iterator { + pub fn get_by_key_enumerated(&'a self, key: K) -> impl '_ + Iterator { let lower_bound = self.idx_sorted_by_item_key.partition_point(|&i| self.items[i].0 < key); self.idx_sorted_by_item_key[lower_bound..].iter().map_while(move |&i| { let (k, v) = &self.items[i]; diff --git a/compiler/rustc_data_structures/src/sso/map.rs b/compiler/rustc_data_structures/src/sso/map.rs index d4274e99f1cff..2de05cd4e5679 100644 --- a/compiler/rustc_data_structures/src/sso/map.rs +++ b/compiler/rustc_data_structures/src/sso/map.rs @@ -257,7 +257,11 @@ impl SsoHashMap { pub fn remove(&mut self, key: &K) -> Option { match self { SsoHashMap::Array(array) => { - array.iter().position(|(k, _v)| k == key).map(|index| array.swap_remove(index).1) + if let Some(index) = array.iter().position(|(k, _v)| k == key) { + Some(array.swap_remove(index).1) + } else { + None + } } SsoHashMap::Map(map) => map.remove(key), } @@ -268,7 +272,11 @@ impl SsoHashMap { pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)> { match self { SsoHashMap::Array(array) => { - array.iter().position(|(k, _v)| k == key).map(|index| array.swap_remove(index)) + if let Some(index) = array.iter().position(|(k, _v)| k == key) { + Some(array.swap_remove(index)) + } else { + None + } } SsoHashMap::Map(map) => map.remove_entry(key), } @@ -415,14 +423,14 @@ impl IntoIterator for SsoHashMap { /// adapts Item of array reference iterator to Item of hashmap reference iterator. #[inline(always)] -fn adapt_array_ref_it(pair: &(K, V)) -> (&K, &V) { +fn adapt_array_ref_it(pair: &'a (K, V)) -> (&'a K, &'a V) { let (a, b) = pair; (a, b) } /// adapts Item of array mut reference iterator to Item of hashmap mut reference iterator. #[inline(always)] -fn adapt_array_mut_it(pair: &mut (K, V)) -> (&K, &mut V) { +fn adapt_array_mut_it(pair: &'a mut (K, V)) -> (&'a K, &'a mut V) { let (a, b) = pair; (a, b) } diff --git a/compiler/rustc_data_structures/src/sso/set.rs b/compiler/rustc_data_structures/src/sso/set.rs index f71522d37148a..29baf4e1ddb66 100644 --- a/compiler/rustc_data_structures/src/sso/set.rs +++ b/compiler/rustc_data_structures/src/sso/set.rs @@ -75,7 +75,7 @@ impl SsoHashSet { /// An iterator visiting all elements in arbitrary order. /// The iterator element type is `&'a T`. #[inline] - pub fn iter(&self) -> impl Iterator { + pub fn iter(&'a self) -> impl Iterator { self.into_iter() } diff --git a/compiler/rustc_data_structures/src/stable_hasher.rs b/compiler/rustc_data_structures/src/stable_hasher.rs index 2e992e762273c..354f9dd93cc4d 100644 --- a/compiler/rustc_data_structures/src/stable_hasher.rs +++ b/compiler/rustc_data_structures/src/stable_hasher.rs @@ -229,14 +229,14 @@ impl HashStable for ::std::num::NonZeroUsize { impl HashStable for f32 { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - let val: u32 = self.to_bits(); + let val: u32 = unsafe { ::std::mem::transmute(*self) }; val.hash_stable(ctx, hasher); } } impl HashStable for f64 { fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) { - let val: u64 = self.to_bits(); + let val: u64 = unsafe { ::std::mem::transmute(*self) }; val.hash_stable(ctx, hasher); } } diff --git a/compiler/rustc_data_structures/src/stack.rs b/compiler/rustc_data_structures/src/stack.rs index ba22c7f9b9799..a4964b7aa0cc8 100644 --- a/compiler/rustc_data_structures/src/stack.rs +++ b/compiler/rustc_data_structures/src/stack.rs @@ -5,7 +5,6 @@ const RED_ZONE: usize = 100 * 1024; // 100k // Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then // on. This flag has performance relevant characteristics. Don't set it too high. -#[allow(clippy::identity_op)] const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB /// Grows the stack on demand to prevent stack overflow. Call this in strategic locations diff --git a/compiler/rustc_data_structures/src/steal.rs b/compiler/rustc_data_structures/src/steal.rs index a3ece6550473c..a1ffbae8b15f6 100644 --- a/compiler/rustc_data_structures/src/steal.rs +++ b/compiler/rustc_data_structures/src/steal.rs @@ -34,7 +34,7 @@ impl Steal { #[track_caller] pub fn borrow(&self) -> MappedReadGuard<'_, T> { let borrow = self.value.borrow(); - if borrow.is_none() { + if let None = &*borrow { panic!("attempted to read from stolen value: {}", std::any::type_name::()); } ReadGuard::map(borrow, |opt| opt.as_ref().unwrap()) diff --git a/compiler/rustc_data_structures/src/tiny_list.rs b/compiler/rustc_data_structures/src/tiny_list.rs index 9e605ea2d982c..9b07f86846eb3 100644 --- a/compiler/rustc_data_structures/src/tiny_list.rs +++ b/compiler/rustc_data_structures/src/tiny_list.rs @@ -48,7 +48,7 @@ impl TinyList { #[inline] pub fn contains(&self, data: &T) -> bool { let mut elem = self.head.as_ref(); - while let Some(e) = elem { + while let Some(ref e) = elem { if &e.data == data { return true; } diff --git a/compiler/rustc_data_structures/src/vec_linked_list.rs b/compiler/rustc_data_structures/src/vec_linked_list.rs index ce60d40b24b44..1cf030d852e9f 100644 --- a/compiler/rustc_data_structures/src/vec_linked_list.rs +++ b/compiler/rustc_data_structures/src/vec_linked_list.rs @@ -2,8 +2,8 @@ use rustc_index::vec::{Idx, IndexVec}; pub fn iter( first: Option, - links: &Ls, -) -> impl Iterator + '_ + links: &'a Ls, +) -> impl Iterator + 'a where Ls: Links, { diff --git a/compiler/rustc_graphviz/src/lib.rs b/compiler/rustc_graphviz/src/lib.rs index e69289b71f97f..27390fd2e4d91 100644 --- a/compiler/rustc_graphviz/src/lib.rs +++ b/compiler/rustc_graphviz/src/lib.rs @@ -512,7 +512,7 @@ impl<'a> LabelText<'a> { pub fn to_dot_string(&self) -> String { match *self { LabelStr(ref s) => format!("\"{}\"", s.escape_default()), - EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(s)), + EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s)), HtmlStr(ref s) => format!("<{}>", s), } } diff --git a/compiler/rustc_index/src/bit_set.rs b/compiler/rustc_index/src/bit_set.rs index 573124c8ec9a8..67b3cec0a3e59 100644 --- a/compiler/rustc_index/src/bit_set.rs +++ b/compiler/rustc_index/src/bit_set.rs @@ -990,8 +990,9 @@ impl BitMatrix { pub fn insert_all_into_row(&mut self, row: R) { assert!(row.index() < self.num_rows); let (start, end) = self.range(row); - for word in self.words[start..end].iter_mut() { - *word = !0; + let words = &mut self.words[..]; + for index in start..end { + words[index] = !0; } self.clear_excess_bits(row); } @@ -1143,7 +1144,7 @@ impl SparseBitMatrix { /// Iterates through all the columns set to true in a given row of /// the matrix. - pub fn iter(&self, row: R) -> impl Iterator + '_ { + pub fn iter<'a>(&'a self, row: R) -> impl Iterator + 'a { self.row(row).into_iter().flat_map(|r| r.iter()) } diff --git a/compiler/rustc_index/src/vec.rs b/compiler/rustc_index/src/vec.rs index 69578e85f2781..66399d2999848 100644 --- a/compiler/rustc_index/src/vec.rs +++ b/compiler/rustc_index/src/vec.rs @@ -634,15 +634,18 @@ impl IndexVec { } #[inline] - pub fn drain>(&mut self, range: R) -> impl Iterator + '_ { + pub fn drain<'a, R: RangeBounds>( + &'a mut self, + range: R, + ) -> impl Iterator + 'a { self.raw.drain(range) } #[inline] - pub fn drain_enumerated>( - &mut self, + pub fn drain_enumerated<'a, R: RangeBounds>( + &'a mut self, range: R, - ) -> impl Iterator + '_ { + ) -> impl Iterator + 'a { self.raw.drain(range).enumerate().map(|(n, t)| (I::new(n), t)) } diff --git a/compiler/rustc_lexer/src/unescape.rs b/compiler/rustc_lexer/src/unescape.rs index 804dc657f2d47..b970c9e4911fa 100644 --- a/compiler/rustc_lexer/src/unescape.rs +++ b/compiler/rustc_lexer/src/unescape.rs @@ -68,10 +68,11 @@ pub enum EscapeError { impl EscapeError { /// Returns true for actual errors, as opposed to warnings. pub fn is_fatal(&self) -> bool { - !matches!( - self, - EscapeError::UnskippedWhitespaceWarning | EscapeError::MultipleSkippedLinesWarning - ) + match self { + EscapeError::UnskippedWhitespaceWarning => false, + EscapeError::MultipleSkippedLinesWarning => false, + _ => true, + } } } @@ -329,7 +330,7 @@ where callback(start..end, Err(EscapeError::MultipleSkippedLinesWarning)); } let tail = &tail[first_non_space..]; - if let Some(c) = tail.chars().next() { + if let Some(c) = tail.chars().nth(0) { // For error reporting, we would like the span to contain the character that was not // skipped. The +1 is necessary to account for the leading \ that started the escape. let end = start + first_non_space + c.len_utf8() + 1; diff --git a/compiler/rustc_macros/src/hash_stable.rs b/compiler/rustc_macros/src/hash_stable.rs index 63bdcea87f817..dba885a27fe22 100644 --- a/compiler/rustc_macros/src/hash_stable.rs +++ b/compiler/rustc_macros/src/hash_stable.rs @@ -24,9 +24,11 @@ fn parse_attributes(field: &syn::Field) -> Attributes { } if meta.path().is_ident("project") { if let Meta::List(list) = meta { - if let Some(NestedMeta::Meta(meta)) = list.nested.iter().next() { - attrs.project = meta.path().get_ident().cloned(); - any_attr = true; + if let Some(nested) = list.nested.iter().next() { + if let NestedMeta::Meta(meta) = nested { + attrs.project = meta.path().get_ident().cloned(); + any_attr = true; + } } } } diff --git a/compiler/rustc_macros/src/session_diagnostic.rs b/compiler/rustc_macros/src/session_diagnostic.rs index c8959dc86ad2d..80dcf99da6224 100644 --- a/compiler/rustc_macros/src/session_diagnostic.rs +++ b/compiler/rustc_macros/src/session_diagnostic.rs @@ -349,14 +349,14 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> { ) -> Result { let field_binding = &info.binding.binding; - let option_ty = option_inner_ty(info.ty); + let option_ty = option_inner_ty(&info.ty); let generated_code = self.generate_non_option_field_code( attr, FieldInfo { vis: info.vis, binding: info.binding, - ty: option_ty.unwrap_or(info.ty), + ty: option_ty.unwrap_or(&info.ty), span: info.span, }, )?; @@ -388,7 +388,7 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> { let formatted_str = self.build_format(&s.value(), attr.span()); match name { "message" => { - if type_matches_path(info.ty, &["rustc_span", "Span"]) { + if type_matches_path(&info.ty, &["rustc_span", "Span"]) { quote! { #diag.set_span(*#field_binding); #diag.set_primary_message(#formatted_str); @@ -401,7 +401,7 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> { } } "label" => { - if type_matches_path(info.ty, &["rustc_span", "Span"]) { + if type_matches_path(&info.ty, &["rustc_span", "Span"]) { quote! { #diag.span_label(*#field_binding, #formatted_str); } diff --git a/compiler/rustc_serialize/src/serialize.rs b/compiler/rustc_serialize/src/serialize.rs index 6671c7c0fa60c..e32e4493726db 100644 --- a/compiler/rustc_serialize/src/serialize.rs +++ b/compiler/rustc_serialize/src/serialize.rs @@ -500,8 +500,8 @@ impl Decodable for [u8; N] { d.read_seq(|d, len| { assert!(len == N); let mut v = [0u8; N]; - for x in &mut v { - *x = d.read_seq_elt(|d| Decodable::decode(d))?; + for i in 0..len { + v[i] = d.read_seq_elt(|d| Decodable::decode(d))?; } Ok(v) }) diff --git a/library/core/src/num/fmt.rs b/library/core/src/num/fmt.rs index 8cff266642fef..578288bda2595 100644 --- a/library/core/src/num/fmt.rs +++ b/library/core/src/num/fmt.rs @@ -31,10 +31,8 @@ impl<'a> Part<'a> { } else { 3 } - } else if v < 10_000 { - 4 } else { - 5 + if v < 10_000 { 4 } else { 5 } } } Part::Copy(buf) => buf.len(),