From 775bacd1b8d97ade32c8d74ce35be1f34759aead Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Tue, 7 Mar 2023 17:43:42 +0000 Subject: [PATCH] Simplify `sort_by` calls --- compiler/rustc_hir_typeck/src/method/probe.rs | 2 +- compiler/rustc_hir_typeck/src/method/suggest.rs | 4 ++-- .../rustc_monomorphize/src/partitioning/merging.rs | 2 +- compiler/rustc_monomorphize/src/partitioning/mod.rs | 2 +- compiler/rustc_parse/src/lexer/diagnostics.rs | 2 +- compiler/rustc_resolve/src/late/diagnostics.rs | 2 +- compiler/rustc_session/src/code_stats.rs | 12 +++--------- 7 files changed, 10 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_hir_typeck/src/method/probe.rs b/compiler/rustc_hir_typeck/src/method/probe.rs index 3bef5cfcd78..57805f7c800 100644 --- a/compiler/rustc_hir_typeck/src/method/probe.rs +++ b/compiler/rustc_hir_typeck/src/method/probe.rs @@ -1031,7 +1031,7 @@ fn candidate_method_names( .collect(); // Sort them by the name so we have a stable result. - names.sort_by(|a, b| a.as_str().partial_cmp(b.as_str()).unwrap()); + names.sort_by(|a, b| a.as_str().cmp(b.as_str())); names } diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 4f4779d1543..2ec67ecae3d 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -42,7 +42,7 @@ use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope}; use super::{CandidateSource, MethodError, NoMatchData}; use rustc_hir::intravisit::Visitor; -use std::cmp::Ordering; +use std::cmp::{self, Ordering}; use std::iter; impl<'a, 'tcx> FnCtxt<'a, 'tcx> { @@ -2517,7 +2517,7 @@ fn suggest_traits_to_import( if !candidates.is_empty() { // Sort from most relevant to least relevant. - candidates.sort_by(|a, b| a.cmp(b).reverse()); + candidates.sort_by_key(|&info| cmp::Reverse(info)); candidates.dedup(); let param_type = match rcvr_ty.kind() { diff --git a/compiler/rustc_monomorphize/src/partitioning/merging.rs b/compiler/rustc_monomorphize/src/partitioning/merging.rs index 02bb8dea0c0..5c524a18454 100644 --- a/compiler/rustc_monomorphize/src/partitioning/merging.rs +++ b/compiler/rustc_monomorphize/src/partitioning/merging.rs @@ -24,7 +24,7 @@ pub fn merge_codegen_units<'tcx>( // smallest into each other) we're sure to start off with a deterministic // order (sorted by name). This'll mean that if two cgus have the same size // the stable sort below will keep everything nice and deterministic. - codegen_units.sort_by(|a, b| a.name().as_str().partial_cmp(b.name().as_str()).unwrap()); + codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str())); // This map keeps track of what got merged into what. let mut cgu_contents: FxHashMap> = diff --git a/compiler/rustc_monomorphize/src/partitioning/mod.rs b/compiler/rustc_monomorphize/src/partitioning/mod.rs index 524c51d88d7..7ac1c9e057e 100644 --- a/compiler/rustc_monomorphize/src/partitioning/mod.rs +++ b/compiler/rustc_monomorphize/src/partitioning/mod.rs @@ -252,7 +252,7 @@ pub fn partition<'tcx>( internalization_candidates: _, } = post_inlining; - result.sort_by(|a, b| a.name().as_str().partial_cmp(b.name().as_str()).unwrap()); + result.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str())); result } diff --git a/compiler/rustc_parse/src/lexer/diagnostics.rs b/compiler/rustc_parse/src/lexer/diagnostics.rs index 27f4428d306..c4b9fdc81c5 100644 --- a/compiler/rustc_parse/src/lexer/diagnostics.rs +++ b/compiler/rustc_parse/src/lexer/diagnostics.rs @@ -71,7 +71,7 @@ pub fn report_suspicious_mismatch_block( .collect(); // sort by `lo`, so the large block spans in the front - matched_spans.sort_by(|a, b| a.0.lo().cmp(&b.0.lo())); + matched_spans.sort_by_key(|(span, _)| span.lo()); // We use larger block whose identation is well to cover those inner mismatched blocks // O(N^2) here, but we are on error reporting path, so it is fine diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs index 206c43f6902..6133e75a78f 100644 --- a/compiler/rustc_resolve/src/late/diagnostics.rs +++ b/compiler/rustc_resolve/src/late/diagnostics.rs @@ -1736,7 +1736,7 @@ fn lookup_typo_candidate( let name = path[path.len() - 1].ident.name; // Make sure error reporting is deterministic. - names.sort_by(|a, b| a.candidate.as_str().partial_cmp(b.candidate.as_str()).unwrap()); + names.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str())); match find_best_match_for_name( &names.iter().map(|suggestion| suggestion.candidate).collect::>(), diff --git a/compiler/rustc_session/src/code_stats.rs b/compiler/rustc_session/src/code_stats.rs index 55178250472..0dfee92f404 100644 --- a/compiler/rustc_session/src/code_stats.rs +++ b/compiler/rustc_session/src/code_stats.rs @@ -2,7 +2,7 @@ use rustc_data_structures::sync::Lock; use rustc_span::Symbol; use rustc_target::abi::{Align, Size}; -use std::cmp::{self, Ordering}; +use std::cmp; #[derive(Clone, PartialEq, Eq, Hash, Debug)] pub struct VariantInfo { @@ -87,7 +87,7 @@ pub fn record_type_size( // Except for Generators, whose variants are already sorted according to // their yield points in `variant_info_for_generator`. if kind != DataTypeKind::Generator { - variants.sort_by(|info1, info2| info2.size.cmp(&info1.size)); + variants.sort_by_key(|info| cmp::Reverse(info.size)); } let info = TypeSizeInfo { kind, @@ -107,13 +107,7 @@ pub fn print_type_sizes(&self) { // Primary sort: large-to-small. // Secondary sort: description (dictionary order) - sorted.sort_by(|info1, info2| { - // (reversing cmp order to get large-to-small ordering) - match info2.overall_size.cmp(&info1.overall_size) { - Ordering::Equal => info1.type_description.cmp(&info2.type_description), - other => other, - } - }); + sorted.sort_by_key(|info| (cmp::Reverse(info.overall_size), &info.type_description)); for info in sorted { let TypeSizeInfo { type_description, overall_size, align, kind, variants, .. } = info;