Skip to content

Commit 0f83c68

Browse files
author
Michael Wright
committed
Replace Constant::partial_cmp
1 parent 7a32c28 commit 0f83c68

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

clippy_lints/src/consts.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -122,21 +122,32 @@ impl Hash for Constant {
122122
}
123123
}
124124

125-
impl PartialOrd for Constant {
126-
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
127-
match (self, other) {
125+
impl Constant {
126+
pub fn partial_cmp(tcx: TyCtxt, cmp_type: &ty::TypeVariants, left: &Self, right: &Self) -> Option<Ordering> {
127+
match (left, right) {
128128
(&Constant::Str(ref ls), &Constant::Str(ref rs)) => Some(ls.cmp(rs)),
129129
(&Constant::Char(ref l), &Constant::Char(ref r)) => Some(l.cmp(r)),
130-
(&Constant::Int(l), &Constant::Int(r)) => Some(l.cmp(&r)),
130+
(&Constant::Int(l), &Constant::Int(r)) => {
131+
if let ty::TyInt(int_ty) = *cmp_type {
132+
Some(sext(tcx, l, int_ty).cmp(&sext(tcx, r, int_ty)))
133+
} else {
134+
Some(l.cmp(&r))
135+
}
136+
},
131137
(&Constant::F64(l), &Constant::F64(r)) => l.partial_cmp(&r),
132138
(&Constant::F32(l), &Constant::F32(r)) => l.partial_cmp(&r),
133139
(&Constant::Bool(ref l), &Constant::Bool(ref r)) => Some(l.cmp(r)),
134-
(&Constant::Tuple(ref l), &Constant::Tuple(ref r)) | (&Constant::Vec(ref l), &Constant::Vec(ref r)) => {
135-
l.partial_cmp(r)
136-
},
137-
(&Constant::Repeat(ref lv, ref ls), &Constant::Repeat(ref rv, ref rs)) => match lv.partial_cmp(rv) {
138-
Some(Equal) => Some(ls.cmp(rs)),
139-
x => x,
140+
(&Constant::Tuple(ref l), &Constant::Tuple(ref r)) | (&Constant::Vec(ref l), &Constant::Vec(ref r)) => l
141+
.iter()
142+
.zip(r.iter())
143+
.map(|(li, ri)| Constant::partial_cmp(tcx, cmp_type, li, ri))
144+
.find(|r| r.map_or(true, |o| o != Ordering::Equal))
145+
.unwrap_or_else(|| Some(l.len().cmp(&r.len()))),
146+
(&Constant::Repeat(ref lv, ref ls), &Constant::Repeat(ref rv, ref rs)) => {
147+
match Constant::partial_cmp(tcx, cmp_type, lv, rv) {
148+
Some(Equal) => Some(ls.cmp(rs)),
149+
x => x,
150+
}
140151
},
141152
_ => None, // TODO: Are there any useful inter-type orderings?
142153
}

clippy_lints/src/minmax.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use crate::consts::{constant_simple, Constant};
2-
use crate::utils::{match_def_path, opt_def_id, paths, sext, span_lint};
2+
use crate::utils::{match_def_path, opt_def_id, paths, span_lint};
33
use rustc::hir::*;
44
use rustc::lint::*;
5-
use rustc::ty::{self, TyCtxt};
6-
use std::cmp::{Ordering, PartialOrd};
5+
use std::cmp::Ordering;
76

87
/// **What it does:** Checks for expressions where `std::cmp::min` and `max` are
98
/// used to clamp values, but switched so that the result is constant.
@@ -43,7 +42,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MinMaxPass {
4342
}
4443
match (
4544
outer_max,
46-
const_partial_cmp(cx.tcx, &outer_c, &inner_c, &cx.tables.expr_ty(ie).sty),
45+
Constant::partial_cmp(cx.tcx, &cx.tables.expr_ty(ie).sty, &outer_c, &inner_c),
4746
) {
4847
(_, None) | (MinMax::Max, Some(Ordering::Less)) | (MinMax::Min, Some(Ordering::Greater)) => (),
4948
_ => {
@@ -60,20 +59,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MinMaxPass {
6059
}
6160
}
6261

63-
// Constant::partial_cmp incorrectly orders signed integers
64-
fn const_partial_cmp(tcx: TyCtxt, a: &Constant, b: &Constant, expr_ty: &ty::TypeVariants) -> Option<Ordering> {
65-
match *expr_ty {
66-
ty::TyInt(int_ty) => {
67-
if let (&Constant::Int(a), &Constant::Int(b)) = (a, b) {
68-
Some(sext(tcx, a, int_ty).cmp(&sext(tcx, b, int_ty)))
69-
} else {
70-
None
71-
}
72-
},
73-
_ => a.partial_cmp(&b),
74-
}
75-
}
76-
7762
#[derive(PartialEq, Eq, Debug)]
7863
enum MinMax {
7964
Min,

0 commit comments

Comments
 (0)