Skip to content

Commit 775bacd

Browse files
committed
Simplify sort_by calls
1 parent ac4379f commit 775bacd

File tree

7 files changed

+10
-16
lines changed

7 files changed

+10
-16
lines changed

compiler/rustc_hir_typeck/src/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1031,7 +1031,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
10311031
.collect();
10321032

10331033
// Sort them by the name so we have a stable result.
1034-
names.sort_by(|a, b| a.as_str().partial_cmp(b.as_str()).unwrap());
1034+
names.sort_by(|a, b| a.as_str().cmp(b.as_str()));
10351035
names
10361036
}
10371037

compiler/rustc_hir_typeck/src/method/suggest.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ use rustc_trait_selection::traits::{
4242
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
4343
use super::{CandidateSource, MethodError, NoMatchData};
4444
use rustc_hir::intravisit::Visitor;
45-
use std::cmp::Ordering;
45+
use std::cmp::{self, Ordering};
4646
use std::iter;
4747

4848
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
@@ -2517,7 +2517,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25172517

25182518
if !candidates.is_empty() {
25192519
// Sort from most relevant to least relevant.
2520-
candidates.sort_by(|a, b| a.cmp(b).reverse());
2520+
candidates.sort_by_key(|&info| cmp::Reverse(info));
25212521
candidates.dedup();
25222522

25232523
let param_type = match rcvr_ty.kind() {

compiler/rustc_monomorphize/src/partitioning/merging.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn merge_codegen_units<'tcx>(
2424
// smallest into each other) we're sure to start off with a deterministic
2525
// order (sorted by name). This'll mean that if two cgus have the same size
2626
// the stable sort below will keep everything nice and deterministic.
27-
codegen_units.sort_by(|a, b| a.name().as_str().partial_cmp(b.name().as_str()).unwrap());
27+
codegen_units.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
2828

2929
// This map keeps track of what got merged into what.
3030
let mut cgu_contents: FxHashMap<Symbol, Vec<Symbol>> =

compiler/rustc_monomorphize/src/partitioning/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ pub fn partition<'tcx>(
252252
internalization_candidates: _,
253253
} = post_inlining;
254254

255-
result.sort_by(|a, b| a.name().as_str().partial_cmp(b.name().as_str()).unwrap());
255+
result.sort_by(|a, b| a.name().as_str().cmp(b.name().as_str()));
256256

257257
result
258258
}

compiler/rustc_parse/src/lexer/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub fn report_suspicious_mismatch_block(
7171
.collect();
7272

7373
// sort by `lo`, so the large block spans in the front
74-
matched_spans.sort_by(|a, b| a.0.lo().cmp(&b.0.lo()));
74+
matched_spans.sort_by_key(|(span, _)| span.lo());
7575

7676
// We use larger block whose identation is well to cover those inner mismatched blocks
7777
// O(N^2) here, but we are on error reporting path, so it is fine

compiler/rustc_resolve/src/late/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
17361736

17371737
let name = path[path.len() - 1].ident.name;
17381738
// Make sure error reporting is deterministic.
1739-
names.sort_by(|a, b| a.candidate.as_str().partial_cmp(b.candidate.as_str()).unwrap());
1739+
names.sort_by(|a, b| a.candidate.as_str().cmp(b.candidate.as_str()));
17401740

17411741
match find_best_match_for_name(
17421742
&names.iter().map(|suggestion| suggestion.candidate).collect::<Vec<Symbol>>(),

compiler/rustc_session/src/code_stats.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_data_structures::fx::FxHashSet;
22
use rustc_data_structures::sync::Lock;
33
use rustc_span::Symbol;
44
use rustc_target::abi::{Align, Size};
5-
use std::cmp::{self, Ordering};
5+
use std::cmp;
66

77
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
88
pub struct VariantInfo {
@@ -87,7 +87,7 @@ impl CodeStats {
8787
// Except for Generators, whose variants are already sorted according to
8888
// their yield points in `variant_info_for_generator`.
8989
if kind != DataTypeKind::Generator {
90-
variants.sort_by(|info1, info2| info2.size.cmp(&info1.size));
90+
variants.sort_by_key(|info| cmp::Reverse(info.size));
9191
}
9292
let info = TypeSizeInfo {
9393
kind,
@@ -107,13 +107,7 @@ impl CodeStats {
107107

108108
// Primary sort: large-to-small.
109109
// Secondary sort: description (dictionary order)
110-
sorted.sort_by(|info1, info2| {
111-
// (reversing cmp order to get large-to-small ordering)
112-
match info2.overall_size.cmp(&info1.overall_size) {
113-
Ordering::Equal => info1.type_description.cmp(&info2.type_description),
114-
other => other,
115-
}
116-
});
110+
sorted.sort_by_key(|info| (cmp::Reverse(info.overall_size), &info.type_description));
117111

118112
for info in sorted {
119113
let TypeSizeInfo { type_description, overall_size, align, kind, variants, .. } = info;

0 commit comments

Comments
 (0)