Skip to content

Commit 8517ca5

Browse files
committed
Merge branch 'master' of github.com:Manishearth/rust-clippy
2 parents b5dac50 + 13a0d5c commit 8517ca5

File tree

16 files changed

+907
-357
lines changed

16 files changed

+907
-357
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ A collection of lints to catch common mistakes and improve your Rust code.
66
[Jump to usage instructions](#usage)
77

88
##Lints
9-
There are 119 lints included in this crate:
9+
There are 120 lints included in this crate:
1010

1111
name | default | meaning
1212
---------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -65,6 +65,7 @@ name
6565
[match_bool](https://github.com/Manishearth/rust-clippy/wiki#match_bool) | warn | a match on boolean expression; recommends `if..else` block instead
6666
[match_overlapping_arm](https://github.com/Manishearth/rust-clippy/wiki#match_overlapping_arm) | warn | a match has overlapping arms
6767
[match_ref_pats](https://github.com/Manishearth/rust-clippy/wiki#match_ref_pats) | warn | a match or `if let` has all arms prefixed with `&`; the match expression can be dereferenced instead
68+
[match_same_arms](https://github.com/Manishearth/rust-clippy/wiki#match_same_arms) | warn | `match` with identical arm bodies
6869
[min_max](https://github.com/Manishearth/rust-clippy/wiki#min_max) | warn | `min(_, max(_, _))` (or vice versa) with bounds clamping the result to a constant
6970
[modulo_one](https://github.com/Manishearth/rust-clippy/wiki#modulo_one) | warn | taking a number modulo 1, which always returns 0
7071
[mut_mut](https://github.com/Manishearth/rust-clippy/wiki#mut_mut) | allow | usage of double-mut refs, e.g. `&mut &mut ...` (either copy'n'paste error, or shows a fundamental misunderstanding of references)

src/consts.rs

Lines changed: 60 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,18 @@
22

33
use rustc::lint::LateContext;
44
use rustc::middle::const_eval::lookup_const_by_id;
5-
use rustc::middle::def::PathResolution;
6-
use rustc::middle::def::Def;
5+
use rustc::middle::def::{Def, PathResolution};
76
use rustc_front::hir::*;
8-
use syntax::ptr::P;
9-
use std::cmp::PartialOrd;
107
use std::cmp::Ordering::{self, Greater, Less, Equal};
11-
use std::rc::Rc;
8+
use std::cmp::PartialOrd;
9+
use std::hash::{Hash, Hasher};
10+
use std::mem;
1211
use std::ops::Deref;
12+
use std::rc::Rc;
13+
use syntax::ast::{FloatTy, LitIntType, LitKind, StrStyle, UintTy};
14+
use syntax::ptr::P;
1315

14-
use syntax::ast::LitKind;
15-
use syntax::ast::LitIntType;
16-
use syntax::ast::{UintTy, FloatTy, StrStyle};
17-
18-
19-
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
16+
#[derive(Debug, Copy, Clone)]
2017
pub enum FloatWidth {
2118
Fw32,
2219
Fw64,
@@ -32,14 +29,14 @@ impl From<FloatTy> for FloatWidth {
3229
}
3330
}
3431

35-
#[derive(Copy, Eq, Debug, Clone, PartialEq)]
32+
#[derive(Copy, Eq, Debug, Clone, PartialEq, Hash)]
3633
pub enum Sign {
3734
Plus,
3835
Minus,
3936
}
4037

4138
/// a Lit_-like enum to fold constant `Expr`s into
42-
#[derive(Eq, Debug, Clone)]
39+
#[derive(Debug, Clone)]
4340
pub enum Constant {
4441
/// a String "abc"
4542
Str(String, StrStyle),
@@ -101,18 +98,12 @@ impl PartialEq for Constant {
10198
(&Constant::Int(lv, _, lneg), &Constant::Int(rv, _, rneg)) => {
10299
lv == rv && lneg == rneg
103100
}
104-
(&Constant::Float(ref ls, lw), &Constant::Float(ref rs, rw)) => {
105-
use self::FloatWidth::*;
106-
if match (lw, rw) {
107-
(FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
101+
(&Constant::Float(ref ls, _), &Constant::Float(ref rs, _)) => {
102+
// we want `Fw32 == FwAny` and `FwAny == Fw64`, by transitivity we must have
103+
// `Fw32 == Fw64` so don’t compare them
104+
match (ls.parse::<f64>(), rs.parse::<f64>()) {
105+
(Ok(l), Ok(r)) => l.eq(&r),
108106
_ => false,
109-
} {
110-
match (ls.parse::<f64>(), rs.parse::<f64>()) {
111-
(Ok(l), Ok(r)) => l.eq(&r),
112-
_ => false,
113-
}
114-
} else {
115-
false
116107
}
117108
}
118109
(&Constant::Bool(l), &Constant::Bool(r)) => l == r,
@@ -124,6 +115,46 @@ impl PartialEq for Constant {
124115
}
125116
}
126117

118+
impl Hash for Constant {
119+
fn hash<H>(&self, state: &mut H) where H: Hasher {
120+
match *self {
121+
Constant::Str(ref s, ref k) => {
122+
s.hash(state);
123+
k.hash(state);
124+
}
125+
Constant::Binary(ref b) => {
126+
b.hash(state);
127+
}
128+
Constant::Byte(u) => {
129+
u.hash(state);
130+
}
131+
Constant::Char(c) => {
132+
c.hash(state);
133+
}
134+
Constant::Int(u, _, t) => {
135+
u.hash(state);
136+
t.hash(state);
137+
}
138+
Constant::Float(ref f, _) => {
139+
// don’t use the width here because of PartialEq implementation
140+
if let Ok(f) = f.parse::<f64>() {
141+
unsafe { mem::transmute::<f64, u64>(f) }.hash(state);
142+
}
143+
}
144+
Constant::Bool(b) => {
145+
b.hash(state);
146+
}
147+
Constant::Vec(ref v) | Constant::Tuple(ref v)=> {
148+
v.hash(state);
149+
}
150+
Constant::Repeat(ref c, l) => {
151+
c.hash(state);
152+
l.hash(state);
153+
}
154+
}
155+
}
156+
}
157+
127158
impl PartialOrd for Constant {
128159
fn partial_cmp(&self, other: &Constant) -> Option<Ordering> {
129160
match (self, other) {
@@ -141,18 +172,10 @@ impl PartialOrd for Constant {
141172
(&Constant::Int(ref lv, _, Sign::Minus), &Constant::Int(ref rv, _, Sign::Minus)) => Some(rv.cmp(lv)),
142173
(&Constant::Int(_, _, Sign::Minus), &Constant::Int(_, _, Sign::Plus)) => Some(Less),
143174
(&Constant::Int(_, _, Sign::Plus), &Constant::Int(_, _, Sign::Minus)) => Some(Greater),
144-
(&Constant::Float(ref ls, lw), &Constant::Float(ref rs, rw)) => {
145-
use self::FloatWidth::*;
146-
if match (lw, rw) {
147-
(FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
148-
_ => false,
149-
} {
150-
match (ls.parse::<f64>(), rs.parse::<f64>()) {
151-
(Ok(ref l), Ok(ref r)) => l.partial_cmp(r),
152-
_ => None,
153-
}
154-
} else {
155-
None
175+
(&Constant::Float(ref ls, _), &Constant::Float(ref rs, _)) => {
176+
match (ls.parse::<f64>(), rs.parse::<f64>()) {
177+
(Ok(ref l), Ok(ref r)) => l.partial_cmp(r),
178+
_ => None,
156179
}
157180
}
158181
(&Constant::Bool(ref l), &Constant::Bool(ref r)) => Some(l.cmp(r)),
@@ -187,8 +210,7 @@ fn constant_not(o: Constant) -> Option<Constant> {
187210
use self::Constant::*;
188211
match o {
189212
Bool(b) => Some(Bool(!b)),
190-
Int(::std::u64::MAX, LitIntType::Signed(_), Sign::Plus) => None,
191-
Int(value, LitIntType::Signed(ity), Sign::Plus) => Some(Int(value + 1, LitIntType::Signed(ity), Sign::Minus)),
213+
Int(value, LitIntType::Signed(ity), Sign::Plus) if value != ::std::u64::MAX => Some(Int(value + 1, LitIntType::Signed(ity), Sign::Minus)),
192214
Int(0, LitIntType::Signed(ity), Sign::Minus) => Some(Int(1, LitIntType::Signed(ity), Sign::Minus)),
193215
Int(value, LitIntType::Signed(ity), Sign::Minus) => Some(Int(value - 1, LitIntType::Signed(ity), Sign::Plus)),
194216
Int(value, LitIntType::Unsigned(ity), Sign::Plus) => {

0 commit comments

Comments
 (0)