Skip to content

Commit f653bf4

Browse files
committed
Improve readability in a few sorts
1 parent 1398572 commit f653bf4

File tree

6 files changed

+9
-17
lines changed

6 files changed

+9
-17
lines changed

src/librustc_driver/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1229,10 +1229,7 @@ Available lint options:
12291229
fn sort_lint_groups(lints: Vec<(&'static str, Vec<lint::LintId>, bool)>)
12301230
-> Vec<(&'static str, Vec<lint::LintId>)> {
12311231
let mut lints: Vec<_> = lints.into_iter().map(|(x, y, _)| (x, y)).collect();
1232-
lints.sort_by(|&(x, _): &(&'static str, Vec<lint::LintId>),
1233-
&(y, _): &(&'static str, Vec<lint::LintId>)| {
1234-
x.cmp(y)
1235-
});
1232+
lints.sort_by_key(|ref l| l.0);
12361233
lints
12371234
}
12381235

src/librustc_driver/profile/trace.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -202,14 +202,13 @@ fn compute_counts_rec(counts: &mut HashMap<String,QueryMetric>, traces: &Vec<Rec
202202

203203
pub fn write_counts(count_file: &mut File, counts: &mut HashMap<String,QueryMetric>) {
204204
use rustc::util::common::duration_to_secs_str;
205-
use std::cmp::Ordering;
205+
use std::cmp::Reverse;
206206

207207
let mut data = vec![];
208208
for (ref cons, ref qm) in counts.iter() {
209209
data.push((cons.clone(), qm.count.clone(), qm.dur_total.clone(), qm.dur_self.clone()));
210210
};
211-
data.sort_by(|&(_,_,_,self1),&(_,_,_,self2)|
212-
if self1 > self2 { Ordering::Less } else { Ordering::Greater } );
211+
data.sort_by_key(|&k| Reverse(k.3));
213212
for (cons, count, dur_total, dur_self) in data {
214213
write!(count_file, "{}, {}, {}, {}\n",
215214
cons, count,

src/librustc_errors/emitter.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use std::borrow::Cow;
2222
use std::io::prelude::*;
2323
use std::io;
2424
use std::collections::HashMap;
25-
use std::cmp::min;
25+
use std::cmp::{min, Reverse};
2626
use termcolor::{StandardStream, ColorChoice, ColorSpec, BufferWriter};
2727
use termcolor::{WriteColor, Color, Buffer};
2828
use unicode_width;
@@ -265,9 +265,7 @@ impl EmitterWriter {
265265
}
266266

267267
// Find overlapping multiline annotations, put them at different depths
268-
multiline_annotations.sort_by(|a, b| {
269-
(a.1.line_start, a.1.line_end).cmp(&(b.1.line_start, b.1.line_end))
270-
});
268+
multiline_annotations.sort_by_key(|&(_, ref ml)| (ml.line_start, ml.line_end));
271269
for item in multiline_annotations.clone() {
272270
let ann = item.1;
273271
for item in multiline_annotations.iter_mut() {
@@ -403,7 +401,7 @@ impl EmitterWriter {
403401
// otherwise the lines would end up needing to go over a message.
404402

405403
let mut annotations = line.annotations.clone();
406-
annotations.sort_by(|a,b| b.start_col.cmp(&a.start_col));
404+
annotations.sort_by_key(|a| Reverse(a.start_col));
407405

408406
// First, figure out where each label will be positioned.
409407
//

src/librustc_mir/monomorphize/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ pub fn assert_symbols_are_distinct<'a, 'tcx, I>(tcx: TyCtxt<'a, 'tcx, 'tcx>, mon
2929
(mono_item, mono_item.symbol_name(tcx))
3030
}).collect();
3131

32-
(&mut symbols[..]).sort_by(|&(_, ref sym1), &(_, ref sym2)|{
33-
sym1.cmp(sym2)
34-
});
32+
(&mut symbols[..]).sort_by_key(|&sym| sym.1);
3533

3634
for pair in (&symbols[..]).windows(2) {
3735
let sym1 = &pair[0].1;

src/librustc_mir/util/patch.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'tcx> MirPatch<'tcx> {
156156
}
157157

158158
let mut new_statements = self.new_statements;
159-
new_statements.sort_by(|u,v| u.0.cmp(&v.0));
159+
new_statements.sort_by_key(|s| s.0);
160160

161161
let mut delta = 0;
162162
let mut last_bb = START_BLOCK;

src/librustc_typeck/collect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,7 @@ pub fn compute_bounds<'gcx: 'tcx, 'tcx>(astconv: &dyn AstConv<'gcx, 'tcx>,
17461746
astconv.ast_region_to_region(r, None)
17471747
}).collect();
17481748

1749-
trait_bounds.sort_by(|a,b| a.def_id().cmp(&b.def_id()));
1749+
trait_bounds.sort_by_key(|t| t.def_id());
17501750

17511751
let implicitly_sized = if let SizedByDefault::Yes = sized_by_default {
17521752
!is_unsized(astconv, ast_bounds, span)

0 commit comments

Comments
 (0)