Skip to content

Commit 6ae8912

Browse files
committed
Auto merge of #89709 - clemenswasser:apply_clippy_suggestions_2, r=petrochenkov
Apply clippy suggestions for rustc and core
2 parents 86d6d2b + 14b6cf6 commit 6ae8912

File tree

22 files changed

+56
-72
lines changed

22 files changed

+56
-72
lines changed

compiler/rustc_apfloat/src/ieee.rs

+2
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,7 @@ impl<S: Semantics> fmt::Display for IeeeFloat<S> {
389389
let _: Loss = sig::shift_right(&mut sig, &mut exp, trailing_zeros as usize);
390390

391391
// Change the exponent from 2^e to 10^e.
392+
#[allow(clippy::comparison_chain)]
392393
if exp == 0 {
393394
// Nothing to do.
394395
} else if exp > 0 {
@@ -2526,6 +2527,7 @@ mod sig {
25262527
if *a_sign ^ b_sign {
25272528
let (reverse, loss);
25282529

2530+
#[allow(clippy::comparison_chain)]
25292531
if bits == 0 {
25302532
reverse = cmp(a_sig, b_sig) == Ordering::Less;
25312533
loss = Loss::ExactlyZero;

compiler/rustc_data_structures/src/base_n.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const BASE_64: &[u8; MAX_BASE as usize] =
1414

1515
#[inline]
1616
pub fn push_str(mut n: u128, base: usize, output: &mut String) {
17-
debug_assert!(base >= 2 && base <= MAX_BASE);
17+
debug_assert!((2..=MAX_BASE).contains(&base));
1818
let mut s = [0u8; 128];
1919
let mut index = 0;
2020

compiler/rustc_data_structures/src/graph/implementation/mod.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -206,17 +206,11 @@ impl<N: Debug, E: Debug> Graph<N, E> {
206206
AdjacentEdges { graph: self, direction, next: first_edge }
207207
}
208208

209-
pub fn successor_nodes<'a>(
210-
&'a self,
211-
source: NodeIndex,
212-
) -> impl Iterator<Item = NodeIndex> + 'a {
209+
pub fn successor_nodes(&self, source: NodeIndex) -> impl Iterator<Item = NodeIndex> + '_ {
213210
self.outgoing_edges(source).targets()
214211
}
215212

216-
pub fn predecessor_nodes<'a>(
217-
&'a self,
218-
target: NodeIndex,
219-
) -> impl Iterator<Item = NodeIndex> + 'a {
213+
pub fn predecessor_nodes(&self, target: NodeIndex) -> impl Iterator<Item = NodeIndex> + '_ {
220214
self.incoming_edges(target).sources()
221215
}
222216

compiler/rustc_data_structures/src/graph/iterate/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn post_order_walk<G: DirectedGraph + WithSuccessors + WithNumNodes>(
4848
let node = frame.node;
4949
visited[node] = true;
5050

51-
while let Some(successor) = frame.iter.next() {
51+
for successor in frame.iter.by_ref() {
5252
if !visited[successor] {
5353
stack.push(PostOrderFrame { node: successor, iter: graph.successors(successor) });
5454
continue 'recurse;
@@ -112,7 +112,7 @@ where
112112
/// This is equivalent to just invoke `next` repeatedly until
113113
/// you get a `None` result.
114114
pub fn complete_search(&mut self) {
115-
while let Some(_) = self.next() {}
115+
for _ in self {}
116116
}
117117

118118
/// Returns true if node has been visited thus far.

compiler/rustc_data_structures/src/obligation_forest/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ impl<O: ForestObligation> ObligationForest<O> {
390390
.map(|(index, _node)| Error { error: error.clone(), backtrace: self.error_at(index) })
391391
.collect();
392392

393-
self.compress(|_| assert!(false));
393+
self.compress(|_| unreachable!());
394394
errors
395395
}
396396

@@ -612,7 +612,7 @@ impl<O: ForestObligation> ObligationForest<O> {
612612
fn compress(&mut self, mut outcome_cb: impl FnMut(&O)) {
613613
let orig_nodes_len = self.nodes.len();
614614
let mut node_rewrites: Vec<_> = std::mem::take(&mut self.reused_node_vec);
615-
debug_assert!(node_rewrites.is_empty());
615+
assert!(node_rewrites.is_empty());
616616
node_rewrites.extend(0..orig_nodes_len);
617617
let mut dead_nodes = 0;
618618

@@ -623,13 +623,13 @@ impl<O: ForestObligation> ObligationForest<O> {
623623
// self.nodes[0..index - dead_nodes] are the first remaining nodes
624624
// self.nodes[index - dead_nodes..index] are all dead
625625
// self.nodes[index..] are unchanged
626-
for index in 0..orig_nodes_len {
626+
for (index, node_rewrite) in node_rewrites.iter_mut().enumerate() {
627627
let node = &self.nodes[index];
628628
match node.state.get() {
629629
NodeState::Pending | NodeState::Waiting => {
630630
if dead_nodes > 0 {
631631
self.nodes.swap(index, index - dead_nodes);
632-
node_rewrites[index] -= dead_nodes;
632+
*node_rewrite -= dead_nodes;
633633
}
634634
}
635635
NodeState::Done => {
@@ -646,7 +646,7 @@ impl<O: ForestObligation> ObligationForest<O> {
646646
}
647647
// Extract the success stories.
648648
outcome_cb(&node.obligation);
649-
node_rewrites[index] = orig_nodes_len;
649+
*node_rewrite = orig_nodes_len;
650650
dead_nodes += 1;
651651
}
652652
NodeState::Error => {
@@ -655,7 +655,7 @@ impl<O: ForestObligation> ObligationForest<O> {
655655
// check against.
656656
self.active_cache.remove(&node.obligation.as_cache_key());
657657
self.insert_into_error_cache(index);
658-
node_rewrites[index] = orig_nodes_len;
658+
*node_rewrite = orig_nodes_len;
659659
dead_nodes += 1;
660660
}
661661
NodeState::Success => unreachable!(),

compiler/rustc_data_structures/src/sorted_map.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -205,22 +205,22 @@ impl<K: Ord, V> SortedMap<K, V> {
205205
R: RangeBounds<K>,
206206
{
207207
let start = match range.start_bound() {
208-
Bound::Included(ref k) => match self.lookup_index_for(k) {
208+
Bound::Included(k) => match self.lookup_index_for(k) {
209209
Ok(index) | Err(index) => index,
210210
},
211-
Bound::Excluded(ref k) => match self.lookup_index_for(k) {
211+
Bound::Excluded(k) => match self.lookup_index_for(k) {
212212
Ok(index) => index + 1,
213213
Err(index) => index,
214214
},
215215
Bound::Unbounded => 0,
216216
};
217217

218218
let end = match range.end_bound() {
219-
Bound::Included(ref k) => match self.lookup_index_for(k) {
219+
Bound::Included(k) => match self.lookup_index_for(k) {
220220
Ok(index) => index + 1,
221221
Err(index) => index,
222222
},
223-
Bound::Excluded(ref k) => match self.lookup_index_for(k) {
223+
Bound::Excluded(k) => match self.lookup_index_for(k) {
224224
Ok(index) | Err(index) => index,
225225
},
226226
Bound::Unbounded => self.data.len(),

compiler/rustc_data_structures/src/sorted_map/index_map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
7575
///
7676
/// If there are multiple items that are equivalent to `key`, they will be yielded in
7777
/// insertion order.
78-
pub fn get_by_key(&'a self, key: K) -> impl 'a + Iterator<Item = &'a V> {
78+
pub fn get_by_key(&self, key: K) -> impl Iterator<Item = &V> {
7979
self.get_by_key_enumerated(key).map(|(_, v)| v)
8080
}
8181

@@ -84,7 +84,7 @@ impl<I: Idx, K: Ord, V> SortedIndexMultiMap<I, K, V> {
8484
///
8585
/// If there are multiple items that are equivalent to `key`, they will be yielded in
8686
/// insertion order.
87-
pub fn get_by_key_enumerated(&'a self, key: K) -> impl '_ + Iterator<Item = (I, &V)> {
87+
pub fn get_by_key_enumerated(&self, key: K) -> impl Iterator<Item = (I, &V)> {
8888
let lower_bound = self.idx_sorted_by_item_key.partition_point(|&i| self.items[i].0 < key);
8989
self.idx_sorted_by_item_key[lower_bound..].iter().map_while(move |&i| {
9090
let (k, v) = &self.items[i];

compiler/rustc_data_structures/src/sso/map.rs

+4-12
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,7 @@ impl<K: Eq + Hash, V> SsoHashMap<K, V> {
257257
pub fn remove(&mut self, key: &K) -> Option<V> {
258258
match self {
259259
SsoHashMap::Array(array) => {
260-
if let Some(index) = array.iter().position(|(k, _v)| k == key) {
261-
Some(array.swap_remove(index).1)
262-
} else {
263-
None
264-
}
260+
array.iter().position(|(k, _v)| k == key).map(|index| array.swap_remove(index).1)
265261
}
266262
SsoHashMap::Map(map) => map.remove(key),
267263
}
@@ -272,11 +268,7 @@ impl<K: Eq + Hash, V> SsoHashMap<K, V> {
272268
pub fn remove_entry(&mut self, key: &K) -> Option<(K, V)> {
273269
match self {
274270
SsoHashMap::Array(array) => {
275-
if let Some(index) = array.iter().position(|(k, _v)| k == key) {
276-
Some(array.swap_remove(index))
277-
} else {
278-
None
279-
}
271+
array.iter().position(|(k, _v)| k == key).map(|index| array.swap_remove(index))
280272
}
281273
SsoHashMap::Map(map) => map.remove_entry(key),
282274
}
@@ -423,14 +415,14 @@ impl<K, V> IntoIterator for SsoHashMap<K, V> {
423415

424416
/// adapts Item of array reference iterator to Item of hashmap reference iterator.
425417
#[inline(always)]
426-
fn adapt_array_ref_it<K, V>(pair: &'a (K, V)) -> (&'a K, &'a V) {
418+
fn adapt_array_ref_it<K, V>(pair: &(K, V)) -> (&K, &V) {
427419
let (a, b) = pair;
428420
(a, b)
429421
}
430422

431423
/// adapts Item of array mut reference iterator to Item of hashmap mut reference iterator.
432424
#[inline(always)]
433-
fn adapt_array_mut_it<K, V>(pair: &'a mut (K, V)) -> (&'a K, &'a mut V) {
425+
fn adapt_array_mut_it<K, V>(pair: &mut (K, V)) -> (&K, &mut V) {
434426
let (a, b) = pair;
435427
(a, b)
436428
}

compiler/rustc_data_structures/src/sso/set.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<T> SsoHashSet<T> {
7575
/// An iterator visiting all elements in arbitrary order.
7676
/// The iterator element type is `&'a T`.
7777
#[inline]
78-
pub fn iter(&'a self) -> impl Iterator<Item = &'a T> {
78+
pub fn iter(&self) -> impl Iterator<Item = &T> {
7979
self.into_iter()
8080
}
8181

compiler/rustc_data_structures/src/stable_hasher.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,14 @@ impl<CTX> HashStable<CTX> for ::std::num::NonZeroUsize {
229229

230230
impl<CTX> HashStable<CTX> for f32 {
231231
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
232-
let val: u32 = unsafe { ::std::mem::transmute(*self) };
232+
let val: u32 = self.to_bits();
233233
val.hash_stable(ctx, hasher);
234234
}
235235
}
236236

237237
impl<CTX> HashStable<CTX> for f64 {
238238
fn hash_stable(&self, ctx: &mut CTX, hasher: &mut StableHasher) {
239-
let val: u64 = unsafe { ::std::mem::transmute(*self) };
239+
let val: u64 = self.to_bits();
240240
val.hash_stable(ctx, hasher);
241241
}
242242
}

compiler/rustc_data_structures/src/stack.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const RED_ZONE: usize = 100 * 1024; // 100k
55

66
// Only the first stack that is pushed, grows exponentially (2^n * STACK_PER_RECURSION) from then
77
// on. This flag has performance relevant characteristics. Don't set it too high.
8+
#[allow(clippy::identity_op)]
89
const STACK_PER_RECURSION: usize = 1 * 1024 * 1024; // 1MB
910

1011
/// Grows the stack on demand to prevent stack overflow. Call this in strategic locations

compiler/rustc_data_structures/src/steal.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<T> Steal<T> {
3434
#[track_caller]
3535
pub fn borrow(&self) -> MappedReadGuard<'_, T> {
3636
let borrow = self.value.borrow();
37-
if let None = &*borrow {
37+
if borrow.is_none() {
3838
panic!("attempted to read from stolen value: {}", std::any::type_name::<T>());
3939
}
4040
ReadGuard::map(borrow, |opt| opt.as_ref().unwrap())

compiler/rustc_data_structures/src/tiny_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl<T: PartialEq> TinyList<T> {
4848
#[inline]
4949
pub fn contains(&self, data: &T) -> bool {
5050
let mut elem = self.head.as_ref();
51-
while let Some(ref e) = elem {
51+
while let Some(e) = elem {
5252
if &e.data == data {
5353
return true;
5454
}

compiler/rustc_data_structures/src/vec_linked_list.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_index::vec::{Idx, IndexVec};
22

33
pub fn iter<Ls>(
44
first: Option<Ls::LinkIndex>,
5-
links: &'a Ls,
6-
) -> impl Iterator<Item = Ls::LinkIndex> + 'a
5+
links: &Ls,
6+
) -> impl Iterator<Item = Ls::LinkIndex> + '_
77
where
88
Ls: Links,
99
{

compiler/rustc_graphviz/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ impl<'a> LabelText<'a> {
512512
pub fn to_dot_string(&self) -> String {
513513
match *self {
514514
LabelStr(ref s) => format!("\"{}\"", s.escape_default()),
515-
EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(&s)),
515+
EscStr(ref s) => format!("\"{}\"", LabelText::escape_str(s)),
516516
HtmlStr(ref s) => format!("<{}>", s),
517517
}
518518
}

compiler/rustc_index/src/bit_set.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -990,9 +990,8 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
990990
pub fn insert_all_into_row(&mut self, row: R) {
991991
assert!(row.index() < self.num_rows);
992992
let (start, end) = self.range(row);
993-
let words = &mut self.words[..];
994-
for index in start..end {
995-
words[index] = !0;
993+
for word in self.words[start..end].iter_mut() {
994+
*word = !0;
996995
}
997996
self.clear_excess_bits(row);
998997
}
@@ -1144,7 +1143,7 @@ impl<R: Idx, C: Idx> SparseBitMatrix<R, C> {
11441143

11451144
/// Iterates through all the columns set to true in a given row of
11461145
/// the matrix.
1147-
pub fn iter<'a>(&'a self, row: R) -> impl Iterator<Item = C> + 'a {
1146+
pub fn iter(&self, row: R) -> impl Iterator<Item = C> + '_ {
11481147
self.row(row).into_iter().flat_map(|r| r.iter())
11491148
}
11501149

compiler/rustc_index/src/vec.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -634,18 +634,15 @@ impl<I: Idx, T> IndexVec<I, T> {
634634
}
635635

636636
#[inline]
637-
pub fn drain<'a, R: RangeBounds<usize>>(
638-
&'a mut self,
639-
range: R,
640-
) -> impl Iterator<Item = T> + 'a {
637+
pub fn drain<R: RangeBounds<usize>>(&mut self, range: R) -> impl Iterator<Item = T> + '_ {
641638
self.raw.drain(range)
642639
}
643640

644641
#[inline]
645-
pub fn drain_enumerated<'a, R: RangeBounds<usize>>(
646-
&'a mut self,
642+
pub fn drain_enumerated<R: RangeBounds<usize>>(
643+
&mut self,
647644
range: R,
648-
) -> impl Iterator<Item = (I, T)> + 'a {
645+
) -> impl Iterator<Item = (I, T)> + '_ {
649646
self.raw.drain(range).enumerate().map(|(n, t)| (I::new(n), t))
650647
}
651648

compiler/rustc_lexer/src/unescape.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,10 @@ pub enum EscapeError {
6868
impl EscapeError {
6969
/// Returns true for actual errors, as opposed to warnings.
7070
pub fn is_fatal(&self) -> bool {
71-
match self {
72-
EscapeError::UnskippedWhitespaceWarning => false,
73-
EscapeError::MultipleSkippedLinesWarning => false,
74-
_ => true,
75-
}
71+
!matches!(
72+
self,
73+
EscapeError::UnskippedWhitespaceWarning | EscapeError::MultipleSkippedLinesWarning
74+
)
7675
}
7776
}
7877

@@ -330,7 +329,7 @@ where
330329
callback(start..end, Err(EscapeError::MultipleSkippedLinesWarning));
331330
}
332331
let tail = &tail[first_non_space..];
333-
if let Some(c) = tail.chars().nth(0) {
332+
if let Some(c) = tail.chars().next() {
334333
// For error reporting, we would like the span to contain the character that was not
335334
// skipped. The +1 is necessary to account for the leading \ that started the escape.
336335
let end = start + first_non_space + c.len_utf8() + 1;

compiler/rustc_macros/src/hash_stable.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,9 @@ fn parse_attributes(field: &syn::Field) -> Attributes {
2424
}
2525
if meta.path().is_ident("project") {
2626
if let Meta::List(list) = meta {
27-
if let Some(nested) = list.nested.iter().next() {
28-
if let NestedMeta::Meta(meta) = nested {
29-
attrs.project = meta.path().get_ident().cloned();
30-
any_attr = true;
31-
}
27+
if let Some(NestedMeta::Meta(meta)) = list.nested.iter().next() {
28+
attrs.project = meta.path().get_ident().cloned();
29+
any_attr = true;
3230
}
3331
}
3432
}

compiler/rustc_macros/src/session_diagnostic.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -349,14 +349,14 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
349349
) -> Result<proc_macro2::TokenStream, SessionDiagnosticDeriveError> {
350350
let field_binding = &info.binding.binding;
351351

352-
let option_ty = option_inner_ty(&info.ty);
352+
let option_ty = option_inner_ty(info.ty);
353353

354354
let generated_code = self.generate_non_option_field_code(
355355
attr,
356356
FieldInfo {
357357
vis: info.vis,
358358
binding: info.binding,
359-
ty: option_ty.unwrap_or(&info.ty),
359+
ty: option_ty.unwrap_or(info.ty),
360360
span: info.span,
361361
},
362362
)?;
@@ -388,7 +388,7 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
388388
let formatted_str = self.build_format(&s.value(), attr.span());
389389
match name {
390390
"message" => {
391-
if type_matches_path(&info.ty, &["rustc_span", "Span"]) {
391+
if type_matches_path(info.ty, &["rustc_span", "Span"]) {
392392
quote! {
393393
#diag.set_span(*#field_binding);
394394
#diag.set_primary_message(#formatted_str);
@@ -401,7 +401,7 @@ impl<'a> SessionDiagnosticDeriveBuilder<'a> {
401401
}
402402
}
403403
"label" => {
404-
if type_matches_path(&info.ty, &["rustc_span", "Span"]) {
404+
if type_matches_path(info.ty, &["rustc_span", "Span"]) {
405405
quote! {
406406
#diag.span_label(*#field_binding, #formatted_str);
407407
}

0 commit comments

Comments
 (0)