mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-23 05:03:21 +00:00
fallout
This commit is contained in:
parent
328d2c7626
commit
3b1df8d381
10 changed files with 210 additions and 216 deletions
167
src/consts.rs
167
src/consts.rs
|
@ -12,7 +12,6 @@ use std::cmp::Ordering::{self, Greater, Less, Equal};
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use self::Constant::*;
|
|
||||||
use self::FloatWidth::*;
|
use self::FloatWidth::*;
|
||||||
|
|
||||||
use syntax::ast::Lit_::*;
|
use syntax::ast::Lit_::*;
|
||||||
|
@ -44,25 +43,25 @@ impl From<FloatTy> for FloatWidth {
|
||||||
#[derive(Eq, Debug, Clone)]
|
#[derive(Eq, Debug, Clone)]
|
||||||
pub enum Constant {
|
pub enum Constant {
|
||||||
/// a String "abc"
|
/// a String "abc"
|
||||||
ConstantStr(String, StrStyle),
|
Str(String, StrStyle),
|
||||||
/// a Binary String b"abc"
|
/// a Binary String b"abc"
|
||||||
ConstantBinary(Rc<Vec<u8>>),
|
Binary(Rc<Vec<u8>>),
|
||||||
/// a single byte b'a'
|
/// a single byte b'a'
|
||||||
ConstantByte(u8),
|
Byte(u8),
|
||||||
/// a single char 'a'
|
/// a single char 'a'
|
||||||
ConstantChar(char),
|
Char(char),
|
||||||
/// an integer
|
/// an integer
|
||||||
ConstantInt(u64, LitIntType),
|
Int(u64, LitIntType),
|
||||||
/// a float with given type
|
/// a float with given type
|
||||||
ConstantFloat(String, FloatWidth),
|
Float(String, FloatWidth),
|
||||||
/// true or false
|
/// true or false
|
||||||
ConstantBool(bool),
|
Bool(bool),
|
||||||
/// an array of constants
|
/// an array of constants
|
||||||
ConstantVec(Vec<Constant>),
|
Vec(Vec<Constant>),
|
||||||
/// also an array, but with only one constant, repeated N times
|
/// also an array, but with only one constant, repeated N times
|
||||||
ConstantRepeat(Box<Constant>, usize),
|
Repeat(Box<Constant>, usize),
|
||||||
/// a tuple of constants
|
/// a tuple of constants
|
||||||
ConstantTuple(Vec<Constant>),
|
Tuple(Vec<Constant>),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Constant {
|
impl Constant {
|
||||||
|
@ -72,7 +71,7 @@ impl Constant {
|
||||||
///
|
///
|
||||||
/// if the constant could not be converted to u64 losslessly
|
/// if the constant could not be converted to u64 losslessly
|
||||||
fn as_u64(&self) -> u64 {
|
fn as_u64(&self) -> u64 {
|
||||||
if let ConstantInt(val, _) = *self {
|
if let Constant::Int(val, _) = *self {
|
||||||
val // TODO we may want to check the sign if any
|
val // TODO we may want to check the sign if any
|
||||||
} else {
|
} else {
|
||||||
panic!("Could not convert a {:?} to u64", self);
|
panic!("Could not convert a {:?} to u64", self);
|
||||||
|
@ -83,9 +82,9 @@ impl Constant {
|
||||||
#[allow(cast_precision_loss)]
|
#[allow(cast_precision_loss)]
|
||||||
pub fn as_float(&self) -> Option<f64> {
|
pub fn as_float(&self) -> Option<f64> {
|
||||||
match *self {
|
match *self {
|
||||||
ConstantByte(b) => Some(b as f64),
|
Constant::Byte(b) => Some(b as f64),
|
||||||
ConstantFloat(ref s, _) => s.parse().ok(),
|
Constant::Float(ref s, _) => s.parse().ok(),
|
||||||
ConstantInt(i, ty) => {
|
Constant::Int(i, ty) => {
|
||||||
Some(if is_negative(ty) {
|
Some(if is_negative(ty) {
|
||||||
-(i as f64)
|
-(i as f64)
|
||||||
} else {
|
} else {
|
||||||
|
@ -100,14 +99,14 @@ impl Constant {
|
||||||
impl PartialEq for Constant {
|
impl PartialEq for Constant {
|
||||||
fn eq(&self, other: &Constant) -> bool {
|
fn eq(&self, other: &Constant) -> bool {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(&ConstantStr(ref ls, ref lsty), &ConstantStr(ref rs, ref rsty)) => ls == rs && lsty == rsty,
|
(&Constant::Str(ref ls, ref lsty), &Constant::Str(ref rs, ref rsty)) => ls == rs && lsty == rsty,
|
||||||
(&ConstantBinary(ref l), &ConstantBinary(ref r)) => l == r,
|
(&Constant::Binary(ref l), &Constant::Binary(ref r)) => l == r,
|
||||||
(&ConstantByte(l), &ConstantByte(r)) => l == r,
|
(&Constant::Byte(l), &Constant::Byte(r)) => l == r,
|
||||||
(&ConstantChar(l), &ConstantChar(r)) => l == r,
|
(&Constant::Char(l), &Constant::Char(r)) => l == r,
|
||||||
(&ConstantInt(lv, lty), &ConstantInt(rv, rty)) => {
|
(&Constant::Int(lv, lty), &Constant::Int(rv, rty)) => {
|
||||||
lv == rv && (is_negative(lty) & (lv != 0)) == (is_negative(rty) & (rv != 0))
|
lv == rv && (is_negative(lty) & (lv != 0)) == (is_negative(rty) & (rv != 0))
|
||||||
}
|
}
|
||||||
(&ConstantFloat(ref ls, lw), &ConstantFloat(ref rs, rw)) => {
|
(&Constant::Float(ref ls, lw), &Constant::Float(ref rs, rw)) => {
|
||||||
if match (lw, rw) {
|
if match (lw, rw) {
|
||||||
(FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
|
(FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -120,10 +119,10 @@ impl PartialEq for Constant {
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(&ConstantBool(l), &ConstantBool(r)) => l == r,
|
(&Constant::Bool(l), &Constant::Bool(r)) => l == r,
|
||||||
(&ConstantVec(ref l), &ConstantVec(ref r)) => l == r,
|
(&Constant::Vec(ref l), &Constant::Vec(ref r)) => l == r,
|
||||||
(&ConstantRepeat(ref lv, ref ls), &ConstantRepeat(ref rv, ref rs)) => ls == rs && lv == rv,
|
(&Constant::Repeat(ref lv, ref ls), &Constant::Repeat(ref rv, ref rs)) => ls == rs && lv == rv,
|
||||||
(&ConstantTuple(ref l), &ConstantTuple(ref r)) => l == r,
|
(&Constant::Tuple(ref l), &Constant::Tuple(ref r)) => l == r,
|
||||||
_ => false, //TODO: Are there inter-type equalities?
|
_ => false, //TODO: Are there inter-type equalities?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -132,16 +131,16 @@ impl PartialEq for Constant {
|
||||||
impl PartialOrd for Constant {
|
impl PartialOrd for Constant {
|
||||||
fn partial_cmp(&self, other: &Constant) -> Option<Ordering> {
|
fn partial_cmp(&self, other: &Constant) -> Option<Ordering> {
|
||||||
match (self, other) {
|
match (self, other) {
|
||||||
(&ConstantStr(ref ls, ref lsty), &ConstantStr(ref rs, ref rsty)) => {
|
(&Constant::Str(ref ls, ref lsty), &Constant::Str(ref rs, ref rsty)) => {
|
||||||
if lsty != rsty {
|
if lsty != rsty {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(ls.cmp(rs))
|
Some(ls.cmp(rs))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(&ConstantByte(ref l), &ConstantByte(ref r)) => Some(l.cmp(r)),
|
(&Constant::Byte(ref l), &Constant::Byte(ref r)) => Some(l.cmp(r)),
|
||||||
(&ConstantChar(ref l), &ConstantChar(ref r)) => Some(l.cmp(r)),
|
(&Constant::Char(ref l), &Constant::Char(ref r)) => Some(l.cmp(r)),
|
||||||
(&ConstantInt(ref lv, lty), &ConstantInt(ref rv, rty)) => {
|
(&Constant::Int(ref lv, lty), &Constant::Int(ref rv, rty)) => {
|
||||||
Some(match (is_negative(lty) && *lv != 0, is_negative(rty) && *rv != 0) {
|
Some(match (is_negative(lty) && *lv != 0, is_negative(rty) && *rv != 0) {
|
||||||
(true, true) => rv.cmp(lv),
|
(true, true) => rv.cmp(lv),
|
||||||
(false, false) => lv.cmp(rv),
|
(false, false) => lv.cmp(rv),
|
||||||
|
@ -149,7 +148,7 @@ impl PartialOrd for Constant {
|
||||||
(false, true) => Greater,
|
(false, true) => Greater,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
(&ConstantFloat(ref ls, lw), &ConstantFloat(ref rs, rw)) => {
|
(&Constant::Float(ref ls, lw), &Constant::Float(ref rs, rw)) => {
|
||||||
if match (lw, rw) {
|
if match (lw, rw) {
|
||||||
(FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
|
(FwAny, _) | (_, FwAny) | (Fw32, Fw32) | (Fw64, Fw64) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
|
@ -162,15 +161,15 @@ impl PartialOrd for Constant {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(&ConstantBool(ref l), &ConstantBool(ref r)) => Some(l.cmp(r)),
|
(&Constant::Bool(ref l), &Constant::Bool(ref r)) => Some(l.cmp(r)),
|
||||||
(&ConstantVec(ref l), &ConstantVec(ref r)) => l.partial_cmp(&r),
|
(&Constant::Vec(ref l), &Constant::Vec(ref r)) => l.partial_cmp(&r),
|
||||||
(&ConstantRepeat(ref lv, ref ls), &ConstantRepeat(ref rv, ref rs)) => {
|
(&Constant::Repeat(ref lv, ref ls), &Constant::Repeat(ref rv, ref rs)) => {
|
||||||
match lv.partial_cmp(rv) {
|
match lv.partial_cmp(rv) {
|
||||||
Some(Equal) => Some(ls.cmp(rs)),
|
Some(Equal) => Some(ls.cmp(rs)),
|
||||||
x => x,
|
x => x,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(&ConstantTuple(ref l), &ConstantTuple(ref r)) => l.partial_cmp(r),
|
(&Constant::Tuple(ref l), &Constant::Tuple(ref r)) => l.partial_cmp(r),
|
||||||
_ => None, //TODO: Are there any useful inter-type orderings?
|
_ => None, //TODO: Are there any useful inter-type orderings?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -193,21 +192,21 @@ fn format_byte(fmt: &mut fmt::Formatter, b: u8) -> fmt::Result {
|
||||||
impl fmt::Display for Constant {
|
impl fmt::Display for Constant {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
ConstantStr(ref s, _) => write!(fmt, "{:?}", s),
|
Constant::Str(ref s, _) => write!(fmt, "{:?}", s),
|
||||||
ConstantByte(ref b) => {
|
Constant::Byte(ref b) => {
|
||||||
write!(fmt, "b'")
|
write!(fmt, "b'")
|
||||||
.and_then(|_| format_byte(fmt, *b))
|
.and_then(|_| format_byte(fmt, *b))
|
||||||
.and_then(|_| write!(fmt, "'"))
|
.and_then(|_| write!(fmt, "'"))
|
||||||
}
|
}
|
||||||
ConstantBinary(ref bs) => {
|
Constant::Binary(ref bs) => {
|
||||||
try!(write!(fmt, "b\""));
|
try!(write!(fmt, "b\""));
|
||||||
for b in bs.iter() {
|
for b in bs.iter() {
|
||||||
try!(format_byte(fmt, *b));
|
try!(format_byte(fmt, *b));
|
||||||
}
|
}
|
||||||
write!(fmt, "\"")
|
write!(fmt, "\"")
|
||||||
}
|
}
|
||||||
ConstantChar(ref c) => write!(fmt, "'{}'", c),
|
Constant::Char(ref c) => write!(fmt, "'{}'", c),
|
||||||
ConstantInt(ref i, ref ity) => {
|
Constant::Int(ref i, ref ity) => {
|
||||||
let (sign, suffix) = match *ity {
|
let (sign, suffix) = match *ity {
|
||||||
LitIntType::SignedIntLit(ref sity, ref sign) => {
|
LitIntType::SignedIntLit(ref sity, ref sign) => {
|
||||||
(if let Sign::Minus = *sign {
|
(if let Sign::Minus = *sign {
|
||||||
|
@ -229,7 +228,7 @@ impl fmt::Display for Constant {
|
||||||
};
|
};
|
||||||
write!(fmt, "{}{}{}", sign, i, suffix)
|
write!(fmt, "{}{}{}", sign, i, suffix)
|
||||||
}
|
}
|
||||||
ConstantFloat(ref s, ref fw) => {
|
Constant::Float(ref s, ref fw) => {
|
||||||
let suffix = match *fw {
|
let suffix = match *fw {
|
||||||
FloatWidth::Fw32 => "f32",
|
FloatWidth::Fw32 => "f32",
|
||||||
FloatWidth::Fw64 => "f64",
|
FloatWidth::Fw64 => "f64",
|
||||||
|
@ -237,9 +236,9 @@ impl fmt::Display for Constant {
|
||||||
};
|
};
|
||||||
write!(fmt, "{}{}", s, suffix)
|
write!(fmt, "{}{}", s, suffix)
|
||||||
}
|
}
|
||||||
ConstantBool(ref b) => write!(fmt, "{}", b),
|
Constant::Bool(ref b) => write!(fmt, "{}", b),
|
||||||
ConstantRepeat(ref c, ref n) => write!(fmt, "[{}; {}]", c, n),
|
Constant::Repeat(ref c, ref n) => write!(fmt, "[{}; {}]", c, n),
|
||||||
ConstantVec(ref v) => {
|
Constant::Vec(ref v) => {
|
||||||
write!(fmt,
|
write!(fmt,
|
||||||
"[{}]",
|
"[{}]",
|
||||||
v.iter()
|
v.iter()
|
||||||
|
@ -247,7 +246,7 @@ impl fmt::Display for Constant {
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join(", "))
|
.join(", "))
|
||||||
}
|
}
|
||||||
ConstantTuple(ref t) => {
|
Constant::Tuple(ref t) => {
|
||||||
write!(fmt,
|
write!(fmt,
|
||||||
"({})",
|
"({})",
|
||||||
t.iter()
|
t.iter()
|
||||||
|
@ -262,21 +261,21 @@ impl fmt::Display for Constant {
|
||||||
|
|
||||||
fn lit_to_constant(lit: &Lit_) -> Constant {
|
fn lit_to_constant(lit: &Lit_) -> Constant {
|
||||||
match *lit {
|
match *lit {
|
||||||
LitStr(ref is, style) => ConstantStr(is.to_string(), style),
|
LitStr(ref is, style) => Constant::Str(is.to_string(), style),
|
||||||
LitByte(b) => ConstantByte(b),
|
LitByte(b) => Constant::Byte(b),
|
||||||
LitByteStr(ref s) => ConstantBinary(s.clone()),
|
LitByteStr(ref s) => Constant::Binary(s.clone()),
|
||||||
LitChar(c) => ConstantChar(c),
|
LitChar(c) => Constant::Char(c),
|
||||||
LitInt(value, ty) => ConstantInt(value, ty),
|
LitInt(value, ty) => Constant::Int(value, ty),
|
||||||
LitFloat(ref is, ty) => ConstantFloat(is.to_string(), ty.into()),
|
LitFloat(ref is, ty) => Constant::Float(is.to_string(), ty.into()),
|
||||||
LitFloatUnsuffixed(ref is) => ConstantFloat(is.to_string(), FwAny),
|
LitFloatUnsuffixed(ref is) => Constant::Float(is.to_string(), FwAny),
|
||||||
LitBool(b) => ConstantBool(b),
|
LitBool(b) => Constant::Bool(b),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn constant_not(o: Constant) -> Option<Constant> {
|
fn constant_not(o: Constant) -> Option<Constant> {
|
||||||
Some(match o {
|
Some(match o {
|
||||||
ConstantBool(b) => ConstantBool(!b),
|
Constant::Bool(b) => Constant::Bool(!b),
|
||||||
ConstantInt(value, ty) => {
|
Constant::Int(value, ty) => {
|
||||||
let (nvalue, nty) = match ty {
|
let (nvalue, nty) = match ty {
|
||||||
SignedIntLit(ity, Plus) => {
|
SignedIntLit(ity, Plus) => {
|
||||||
if value == ::std::u64::MAX {
|
if value == ::std::u64::MAX {
|
||||||
|
@ -307,7 +306,7 @@ fn constant_not(o: Constant) -> Option<Constant> {
|
||||||
return None;
|
return None;
|
||||||
} // refuse to guess
|
} // refuse to guess
|
||||||
};
|
};
|
||||||
ConstantInt(nvalue, nty)
|
Constant::Int(nvalue, nty)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return None;
|
return None;
|
||||||
|
@ -317,8 +316,8 @@ fn constant_not(o: Constant) -> Option<Constant> {
|
||||||
|
|
||||||
fn constant_negate(o: Constant) -> Option<Constant> {
|
fn constant_negate(o: Constant) -> Option<Constant> {
|
||||||
Some(match o {
|
Some(match o {
|
||||||
ConstantInt(value, ty) => {
|
Constant::Int(value, ty) => {
|
||||||
ConstantInt(value,
|
Constant::Int(value,
|
||||||
match ty {
|
match ty {
|
||||||
SignedIntLit(ity, sign) => SignedIntLit(ity, neg_sign(sign)),
|
SignedIntLit(ity, sign) => SignedIntLit(ity, neg_sign(sign)),
|
||||||
UnsuffixedIntLit(sign) => UnsuffixedIntLit(neg_sign(sign)),
|
UnsuffixedIntLit(sign) => UnsuffixedIntLit(neg_sign(sign)),
|
||||||
|
@ -327,7 +326,7 @@ fn constant_negate(o: Constant) -> Option<Constant> {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
ConstantFloat(is, ty) => ConstantFloat(neg_float_str(is), ty),
|
Constant::Float(is, ty) => Constant::Float(neg_float_str(is), ty),
|
||||||
_ => {
|
_ => {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
@ -402,9 +401,9 @@ fn unify_int_type(l: LitIntType, r: LitIntType, s: Sign) -> Option<LitIntType> {
|
||||||
|
|
||||||
fn add_neg_int(pos: u64, pty: LitIntType, neg: u64, nty: LitIntType) -> Option<Constant> {
|
fn add_neg_int(pos: u64, pty: LitIntType, neg: u64, nty: LitIntType) -> Option<Constant> {
|
||||||
if neg > pos {
|
if neg > pos {
|
||||||
unify_int_type(nty, pty, Minus).map(|ty| ConstantInt(neg - pos, ty))
|
unify_int_type(nty, pty, Minus).map(|ty| Constant::Int(neg - pos, ty))
|
||||||
} else {
|
} else {
|
||||||
unify_int_type(nty, pty, Plus).map(|ty| ConstantInt(pos - neg, ty))
|
unify_int_type(nty, pty, Plus).map(|ty| Constant::Int(pos - neg, ty))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -416,7 +415,7 @@ fn sub_int(l: u64, lty: LitIntType, r: u64, rty: LitIntType, neg: bool) -> Optio
|
||||||
} else {
|
} else {
|
||||||
Plus
|
Plus
|
||||||
})
|
})
|
||||||
.and_then(|ty| l.checked_sub(r).map(|v| ConstantInt(v, ty)))
|
.and_then(|ty| l.checked_sub(r).map(|v| Constant::Int(v, ty)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -449,10 +448,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
ExprBlock(ref block) => self.block(block),
|
ExprBlock(ref block) => self.block(block),
|
||||||
ExprIf(ref cond, ref then, ref otherwise) => self.ifthenelse(cond, then, otherwise),
|
ExprIf(ref cond, ref then, ref otherwise) => self.ifthenelse(cond, then, otherwise),
|
||||||
ExprLit(ref lit) => Some(lit_to_constant(&lit.node)),
|
ExprLit(ref lit) => Some(lit_to_constant(&lit.node)),
|
||||||
ExprVec(ref vec) => self.multi(vec).map(ConstantVec),
|
ExprVec(ref vec) => self.multi(vec).map(Constant::Vec),
|
||||||
ExprTup(ref tup) => self.multi(tup).map(ConstantTuple),
|
ExprTup(ref tup) => self.multi(tup).map(Constant::Tuple),
|
||||||
ExprRepeat(ref value, ref number) => {
|
ExprRepeat(ref value, ref number) => {
|
||||||
self.binop_apply(value, number, |v, n| Some(ConstantRepeat(Box::new(v), n.as_u64() as usize)))
|
self.binop_apply(value, number, |v, n| Some(Constant::Repeat(Box::new(v), n.as_u64() as usize)))
|
||||||
}
|
}
|
||||||
ExprUnary(op, ref operand) => {
|
ExprUnary(op, ref operand) => {
|
||||||
self.expr(operand).and_then(|o| {
|
self.expr(operand).and_then(|o| {
|
||||||
|
@ -508,7 +507,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ifthenelse(&mut self, cond: &Expr, then: &Block, otherwise: &Option<P<Expr>>) -> Option<Constant> {
|
fn ifthenelse(&mut self, cond: &Expr, then: &Block, otherwise: &Option<P<Expr>>) -> Option<Constant> {
|
||||||
if let Some(ConstantBool(b)) = self.expr(cond) {
|
if let Some(Constant::Bool(b)) = self.expr(cond) {
|
||||||
if b {
|
if b {
|
||||||
self.block(then)
|
self.block(then)
|
||||||
} else {
|
} else {
|
||||||
|
@ -524,8 +523,8 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
BiAdd => {
|
BiAdd => {
|
||||||
self.binop_apply(left, right, |l, r| {
|
self.binop_apply(left, right, |l, r| {
|
||||||
match (l, r) {
|
match (l, r) {
|
||||||
(ConstantByte(l8), ConstantByte(r8)) => l8.checked_add(r8).map(ConstantByte),
|
(Constant::Byte(l8), Constant::Byte(r8)) => l8.checked_add(r8).map(Constant::Byte),
|
||||||
(ConstantInt(l64, lty), ConstantInt(r64, rty)) => {
|
(Constant::Int(l64, lty), Constant::Int(r64, rty)) => {
|
||||||
let (ln, rn) = (is_negative(lty), is_negative(rty));
|
let (ln, rn) = (is_negative(lty), is_negative(rty));
|
||||||
if ln == rn {
|
if ln == rn {
|
||||||
unify_int_type(lty,
|
unify_int_type(lty,
|
||||||
|
@ -535,7 +534,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
} else {
|
} else {
|
||||||
Plus
|
Plus
|
||||||
})
|
})
|
||||||
.and_then(|ty| l64.checked_add(r64).map(|v| ConstantInt(v, ty)))
|
.and_then(|ty| l64.checked_add(r64).map(|v| Constant::Int(v, ty)))
|
||||||
} else if ln {
|
} else if ln {
|
||||||
add_neg_int(r64, rty, l64, lty)
|
add_neg_int(r64, rty, l64, lty)
|
||||||
} else {
|
} else {
|
||||||
|
@ -550,24 +549,24 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
BiSub => {
|
BiSub => {
|
||||||
self.binop_apply(left, right, |l, r| {
|
self.binop_apply(left, right, |l, r| {
|
||||||
match (l, r) {
|
match (l, r) {
|
||||||
(ConstantByte(l8), ConstantByte(r8)) => {
|
(Constant::Byte(l8), Constant::Byte(r8)) => {
|
||||||
if r8 > l8 {
|
if r8 > l8 {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(ConstantByte(l8 - r8))
|
Some(Constant::Byte(l8 - r8))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(ConstantInt(l64, lty), ConstantInt(r64, rty)) => {
|
(Constant::Int(l64, lty), Constant::Int(r64, rty)) => {
|
||||||
match (is_negative(lty), is_negative(rty)) {
|
match (is_negative(lty), is_negative(rty)) {
|
||||||
(false, false) => sub_int(l64, lty, r64, rty, r64 > l64),
|
(false, false) => sub_int(l64, lty, r64, rty, r64 > l64),
|
||||||
(true, true) => sub_int(l64, lty, r64, rty, l64 > r64),
|
(true, true) => sub_int(l64, lty, r64, rty, l64 > r64),
|
||||||
(true, false) => {
|
(true, false) => {
|
||||||
unify_int_type(lty, rty, Minus)
|
unify_int_type(lty, rty, Minus)
|
||||||
.and_then(|ty| l64.checked_add(r64).map(|v| ConstantInt(v, ty)))
|
.and_then(|ty| l64.checked_add(r64).map(|v| Constant::Int(v, ty)))
|
||||||
}
|
}
|
||||||
(false, true) => {
|
(false, true) => {
|
||||||
unify_int_type(lty, rty, Plus)
|
unify_int_type(lty, rty, Plus)
|
||||||
.and_then(|ty| l64.checked_add(r64).map(|v| ConstantInt(v, ty)))
|
.and_then(|ty| l64.checked_add(r64).map(|v| Constant::Int(v, ty)))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -585,8 +584,8 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
BiBitOr => self.bitop(left, right, |x, y| (x | y)),
|
BiBitOr => self.bitop(left, right, |x, y| (x | y)),
|
||||||
BiShl => self.bitop(left, right, |x, y| x << y),
|
BiShl => self.bitop(left, right, |x, y| x << y),
|
||||||
BiShr => self.bitop(left, right, |x, y| x >> y),
|
BiShr => self.bitop(left, right, |x, y| x >> y),
|
||||||
BiEq => self.binop_apply(left, right, |l, r| Some(ConstantBool(l == r))),
|
BiEq => self.binop_apply(left, right, |l, r| Some(Constant::Bool(l == r))),
|
||||||
BiNe => self.binop_apply(left, right, |l, r| Some(ConstantBool(l != r))),
|
BiNe => self.binop_apply(left, right, |l, r| Some(Constant::Bool(l != r))),
|
||||||
BiLt => self.cmp(left, right, Less, true),
|
BiLt => self.cmp(left, right, Less, true),
|
||||||
BiLe => self.cmp(left, right, Greater, false),
|
BiLe => self.cmp(left, right, Greater, false),
|
||||||
BiGe => self.cmp(left, right, Less, false),
|
BiGe => self.cmp(left, right, Less, false),
|
||||||
|
@ -600,7 +599,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
{
|
{
|
||||||
self.binop_apply(left, right, |l, r| {
|
self.binop_apply(left, right, |l, r| {
|
||||||
match (l, r) {
|
match (l, r) {
|
||||||
(ConstantInt(l64, lty), ConstantInt(r64, rty)) => {
|
(Constant::Int(l64, lty), Constant::Int(r64, rty)) => {
|
||||||
f(l64, r64).and_then(|value| {
|
f(l64, r64).and_then(|value| {
|
||||||
unify_int_type(lty,
|
unify_int_type(lty,
|
||||||
rty,
|
rty,
|
||||||
|
@ -609,7 +608,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
} else {
|
} else {
|
||||||
Minus
|
Minus
|
||||||
})
|
})
|
||||||
.map(|ty| ConstantInt(value, ty))
|
.map(|ty| Constant::Int(value, ty))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
|
@ -622,10 +621,10 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
{
|
{
|
||||||
self.binop_apply(left, right, |l, r| {
|
self.binop_apply(left, right, |l, r| {
|
||||||
match (l, r) {
|
match (l, r) {
|
||||||
(ConstantBool(l), ConstantBool(r)) => Some(ConstantBool(f(l as u64, r as u64) != 0)),
|
(Constant::Bool(l), Constant::Bool(r)) => Some(Constant::Bool(f(l as u64, r as u64) != 0)),
|
||||||
(ConstantByte(l8), ConstantByte(r8)) => Some(ConstantByte(f(l8 as u64, r8 as u64) as u8)),
|
(Constant::Byte(l8), Constant::Byte(r8)) => Some(Constant::Byte(f(l8 as u64, r8 as u64) as u8)),
|
||||||
(ConstantInt(l, lty), ConstantInt(r, rty)) => {
|
(Constant::Int(l, lty), Constant::Int(r, rty)) => {
|
||||||
unify_int_type(lty, rty, Plus).map(|ty| ConstantInt(f(l, r), ty))
|
unify_int_type(lty, rty, Plus).map(|ty| Constant::Int(f(l, r), ty))
|
||||||
}
|
}
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
|
@ -635,7 +634,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
fn cmp(&mut self, left: &Expr, right: &Expr, ordering: Ordering, b: bool) -> Option<Constant> {
|
fn cmp(&mut self, left: &Expr, right: &Expr, ordering: Ordering, b: bool) -> Option<Constant> {
|
||||||
self.binop_apply(left,
|
self.binop_apply(left,
|
||||||
right,
|
right,
|
||||||
|l, r| l.partial_cmp(&r).map(|o| ConstantBool(b == (o == ordering))))
|
|l, r| l.partial_cmp(&r).map(|o| Constant::Bool(b == (o == ordering))))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn binop_apply<F>(&mut self, left: &Expr, right: &Expr, op: F) -> Option<Constant>
|
fn binop_apply<F>(&mut self, left: &Expr, right: &Expr, op: F) -> Option<Constant>
|
||||||
|
@ -650,12 +649,12 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
|
||||||
|
|
||||||
fn short_circuit(&mut self, left: &Expr, right: &Expr, b: bool) -> Option<Constant> {
|
fn short_circuit(&mut self, left: &Expr, right: &Expr, b: bool) -> Option<Constant> {
|
||||||
self.expr(left).and_then(|left| {
|
self.expr(left).and_then(|left| {
|
||||||
if let ConstantBool(lbool) = left {
|
if let Constant::Bool(lbool) = left {
|
||||||
if lbool == b {
|
if lbool == b {
|
||||||
Some(left)
|
Some(left)
|
||||||
} else {
|
} else {
|
||||||
self.expr(right).and_then(|right| {
|
self.expr(right).and_then(|right| {
|
||||||
if let ConstantBool(_) = right {
|
if let Constant::Bool(_) = right {
|
||||||
Some(right)
|
Some(right)
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
|
@ -2,8 +2,7 @@ use rustc::lint::*;
|
||||||
use rustc_front::hir::*;
|
use rustc_front::hir::*;
|
||||||
use syntax::codemap::Span;
|
use syntax::codemap::Span;
|
||||||
|
|
||||||
use consts::{constant_simple, is_negative};
|
use consts::{constant_simple, is_negative, Constant};
|
||||||
use consts::Constant::ConstantInt;
|
|
||||||
use utils::{span_lint, snippet, in_macro};
|
use utils::{span_lint, snippet, in_macro};
|
||||||
|
|
||||||
/// **What it does:** This lint checks for identity operations, e.g. `x + 0`. It is `Warn` by default.
|
/// **What it does:** This lint checks for identity operations, e.g. `x + 0`. It is `Warn` by default.
|
||||||
|
@ -54,7 +53,7 @@ impl LateLintPass for IdentityOp {
|
||||||
|
|
||||||
|
|
||||||
fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
|
fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) {
|
||||||
if let Some(ConstantInt(v, ty)) = constant_simple(e) {
|
if let Some(Constant::Int(v, ty)) = constant_simple(e) {
|
||||||
if match m {
|
if match m {
|
||||||
0 => v == 0,
|
0 => v == 0,
|
||||||
-1 => is_negative(ty) && v == 1,
|
-1 => is_negative(ty) && v == 1,
|
||||||
|
|
|
@ -365,8 +365,8 @@ fn check_for_loop_reverse_range(cx: &LateContext, arg: &Expr, expr: &Expr) {
|
||||||
// if this for loop is iterating over a two-sided range...
|
// if this for loop is iterating over a two-sided range...
|
||||||
if let ExprRange(Some(ref start_expr), Some(ref stop_expr)) = arg.node {
|
if let ExprRange(Some(ref start_expr), Some(ref stop_expr)) = arg.node {
|
||||||
// ...and both sides are compile-time constant integers...
|
// ...and both sides are compile-time constant integers...
|
||||||
if let Some(start_idx @ Constant::ConstantInt(..)) = constant_simple(start_expr) {
|
if let Some(start_idx @ Constant::Int(..)) = constant_simple(start_expr) {
|
||||||
if let Some(stop_idx @ Constant::ConstantInt(..)) = constant_simple(stop_expr) {
|
if let Some(stop_idx @ Constant::Int(..)) = constant_simple(stop_expr) {
|
||||||
// ...and the start index is greater than the stop index,
|
// ...and the start index is greater than the stop index,
|
||||||
// this loop will never run. This is often confusing for developers
|
// this loop will never run. This is often confusing for developers
|
||||||
// who think that this will iterate from the larger value to the
|
// who think that this will iterate from the larger value to the
|
||||||
|
|
197
src/methods.rs
197
src/methods.rs
|
@ -19,9 +19,6 @@ use utils::{
|
||||||
use utils::MethodArgs;
|
use utils::MethodArgs;
|
||||||
use rustc::middle::cstore::CrateStore;
|
use rustc::middle::cstore::CrateStore;
|
||||||
|
|
||||||
use self::SelfKind::*;
|
|
||||||
use self::OutType::*;
|
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct MethodsPass;
|
pub struct MethodsPass;
|
||||||
|
|
||||||
|
@ -456,11 +453,11 @@ fn lint_extend(cx: &LateContext, expr: &Expr, args: &MethodArgs) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn derefs_to_slice(cx: &LateContext, expr: &Expr, ty: &ty::Ty)
|
fn derefs_to_slice(cx: &LateContext, expr: &Expr, ty: &ty::Ty)
|
||||||
-> Option<(Span, &'static str)> {
|
-> Option<(Span, &'static str)> {
|
||||||
fn may_slice(cx: &LateContext, ty: &ty::Ty) -> bool {
|
fn may_slice(cx: &LateContext, ty: &ty::Ty) -> bool {
|
||||||
match ty.sty {
|
match ty.sty {
|
||||||
ty::TySlice(_) => true,
|
ty::TySlice(_) => true,
|
||||||
ty::TyStruct(..) => match_type(cx, ty, &VEC_PATH),
|
ty::TyStruct(..) => match_type(cx, ty, &VEC_PATH),
|
||||||
ty::TyArray(_, size) => size < 32,
|
ty::TyArray(_, size) => size < 32,
|
||||||
ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) |
|
ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) |
|
||||||
|
@ -469,7 +466,7 @@ fn derefs_to_slice(cx: &LateContext, expr: &Expr, ty: &ty::Ty)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let ExprMethodCall(name, _, ref args) = expr.node {
|
if let ExprMethodCall(name, _, ref args) = expr.node {
|
||||||
if &name.node.as_str() == &"iter" &&
|
if &name.node.as_str() == &"iter" &&
|
||||||
may_slice(cx, &cx.tcx.expr_ty(&args[0])) {
|
may_slice(cx, &cx.tcx.expr_ty(&args[0])) {
|
||||||
Some((args[0].span, "&"))
|
Some((args[0].span, "&"))
|
||||||
} else {
|
} else {
|
||||||
|
@ -479,7 +476,7 @@ fn derefs_to_slice(cx: &LateContext, expr: &Expr, ty: &ty::Ty)
|
||||||
match ty.sty {
|
match ty.sty {
|
||||||
ty::TySlice(_) => Some((expr.span, "")),
|
ty::TySlice(_) => Some((expr.span, "")),
|
||||||
ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) |
|
ty::TyRef(_, ty::TypeAndMut { ty: ref inner, .. }) |
|
||||||
ty::TyBox(ref inner) => if may_slice(cx, inner) {
|
ty::TyBox(ref inner) => if may_slice(cx, inner) {
|
||||||
Some((expr.span, ""))
|
Some((expr.span, ""))
|
||||||
} else { None },
|
} else { None },
|
||||||
_ => None
|
_ => None
|
||||||
|
@ -731,180 +728,180 @@ fn has_debug_impl<'a, 'b>(ty: ty::Ty<'a>, cx: &LateContext<'b, 'a>) -> bool {
|
||||||
debug_impl_exists
|
debug_impl_exists
|
||||||
}
|
}
|
||||||
|
|
||||||
const CONVENTIONS: [(&'static str, &'static [SelfKind]); 5] = [("into_", &[ValueSelf]),
|
const CONVENTIONS: [(&'static str, &'static [SelfKind]); 5] = [("into_", &[SelfKind::Value]),
|
||||||
("to_", &[RefSelf]),
|
("to_", &[SelfKind::Ref]),
|
||||||
("as_", &[RefSelf, RefMutSelf]),
|
("as_", &[SelfKind::Ref, SelfKind::RefMut]),
|
||||||
("is_", &[RefSelf, NoSelf]),
|
("is_", &[SelfKind::Ref, SelfKind::No]),
|
||||||
("from_", &[NoSelf])];
|
("from_", &[SelfKind::No])];
|
||||||
|
|
||||||
const TRAIT_METHODS: [(&'static str, usize, SelfKind, OutType, &'static str); 30] = [("add",
|
const TRAIT_METHODS: [(&'static str, usize, SelfKind, OutType, &'static str); 30] = [("add",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Add"),
|
"std::ops::Add"),
|
||||||
("sub",
|
("sub",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Sub"),
|
"std::ops::Sub"),
|
||||||
("mul",
|
("mul",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Mul"),
|
"std::ops::Mul"),
|
||||||
("div",
|
("div",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Div"),
|
"std::ops::Div"),
|
||||||
("rem",
|
("rem",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Rem"),
|
"std::ops::Rem"),
|
||||||
("shl",
|
("shl",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Shl"),
|
"std::ops::Shl"),
|
||||||
("shr",
|
("shr",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Shr"),
|
"std::ops::Shr"),
|
||||||
("bitand",
|
("bitand",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::BitAnd"),
|
"std::ops::BitAnd"),
|
||||||
("bitor",
|
("bitor",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::BitOr"),
|
"std::ops::BitOr"),
|
||||||
("bitxor",
|
("bitxor",
|
||||||
2,
|
2,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::BitXor"),
|
"std::ops::BitXor"),
|
||||||
("neg",
|
("neg",
|
||||||
1,
|
1,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Neg"),
|
"std::ops::Neg"),
|
||||||
("not",
|
("not",
|
||||||
1,
|
1,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::ops::Not"),
|
"std::ops::Not"),
|
||||||
("drop",
|
("drop",
|
||||||
1,
|
1,
|
||||||
RefMutSelf,
|
SelfKind::RefMut,
|
||||||
UnitType,
|
OutType::Unit,
|
||||||
"std::ops::Drop"),
|
"std::ops::Drop"),
|
||||||
("index",
|
("index",
|
||||||
2,
|
2,
|
||||||
RefSelf,
|
SelfKind::Ref,
|
||||||
RefType,
|
OutType::Ref,
|
||||||
"std::ops::Index"),
|
"std::ops::Index"),
|
||||||
("index_mut",
|
("index_mut",
|
||||||
2,
|
2,
|
||||||
RefMutSelf,
|
SelfKind::RefMut,
|
||||||
RefType,
|
OutType::Ref,
|
||||||
"std::ops::IndexMut"),
|
"std::ops::IndexMut"),
|
||||||
("deref",
|
("deref",
|
||||||
1,
|
1,
|
||||||
RefSelf,
|
SelfKind::Ref,
|
||||||
RefType,
|
OutType::Ref,
|
||||||
"std::ops::Deref"),
|
"std::ops::Deref"),
|
||||||
("deref_mut",
|
("deref_mut",
|
||||||
1,
|
1,
|
||||||
RefMutSelf,
|
SelfKind::RefMut,
|
||||||
RefType,
|
OutType::Ref,
|
||||||
"std::ops::DerefMut"),
|
"std::ops::DerefMut"),
|
||||||
("clone",
|
("clone",
|
||||||
1,
|
1,
|
||||||
RefSelf,
|
SelfKind::Ref,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::clone::Clone"),
|
"std::clone::Clone"),
|
||||||
("borrow",
|
("borrow",
|
||||||
1,
|
1,
|
||||||
RefSelf,
|
SelfKind::Ref,
|
||||||
RefType,
|
OutType::Ref,
|
||||||
"std::borrow::Borrow"),
|
"std::borrow::Borrow"),
|
||||||
("borrow_mut",
|
("borrow_mut",
|
||||||
1,
|
1,
|
||||||
RefMutSelf,
|
SelfKind::RefMut,
|
||||||
RefType,
|
OutType::Ref,
|
||||||
"std::borrow::BorrowMut"),
|
"std::borrow::BorrowMut"),
|
||||||
("as_ref",
|
("as_ref",
|
||||||
1,
|
1,
|
||||||
RefSelf,
|
SelfKind::Ref,
|
||||||
RefType,
|
OutType::Ref,
|
||||||
"std::convert::AsRef"),
|
"std::convert::AsRef"),
|
||||||
("as_mut",
|
("as_mut",
|
||||||
1,
|
1,
|
||||||
RefMutSelf,
|
SelfKind::RefMut,
|
||||||
RefType,
|
OutType::Ref,
|
||||||
"std::convert::AsMut"),
|
"std::convert::AsMut"),
|
||||||
("eq",
|
("eq",
|
||||||
2,
|
2,
|
||||||
RefSelf,
|
SelfKind::Ref,
|
||||||
BoolType,
|
OutType::Bool,
|
||||||
"std::cmp::PartialEq"),
|
"std::cmp::PartialEq"),
|
||||||
("cmp",
|
("cmp",
|
||||||
2,
|
2,
|
||||||
RefSelf,
|
SelfKind::Ref,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::cmp::Ord"),
|
"std::cmp::Ord"),
|
||||||
("default",
|
("default",
|
||||||
0,
|
0,
|
||||||
NoSelf,
|
SelfKind::No,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::default::Default"),
|
"std::default::Default"),
|
||||||
("hash",
|
("hash",
|
||||||
2,
|
2,
|
||||||
RefSelf,
|
SelfKind::Ref,
|
||||||
UnitType,
|
OutType::Unit,
|
||||||
"std::hash::Hash"),
|
"std::hash::Hash"),
|
||||||
("next",
|
("next",
|
||||||
1,
|
1,
|
||||||
RefMutSelf,
|
SelfKind::RefMut,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::iter::Iterator"),
|
"std::iter::Iterator"),
|
||||||
("into_iter",
|
("into_iter",
|
||||||
1,
|
1,
|
||||||
ValueSelf,
|
SelfKind::Value,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::iter::IntoIterator"),
|
"std::iter::IntoIterator"),
|
||||||
("from_iter",
|
("from_iter",
|
||||||
1,
|
1,
|
||||||
NoSelf,
|
SelfKind::No,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::iter::FromIterator"),
|
"std::iter::FromIterator"),
|
||||||
("from_str",
|
("from_str",
|
||||||
1,
|
1,
|
||||||
NoSelf,
|
SelfKind::No,
|
||||||
AnyType,
|
OutType::Any,
|
||||||
"std::str::FromStr")];
|
"std::str::FromStr")];
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum SelfKind {
|
enum SelfKind {
|
||||||
ValueSelf,
|
Value,
|
||||||
RefSelf,
|
Ref,
|
||||||
RefMutSelf,
|
RefMut,
|
||||||
NoSelf,
|
No,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SelfKind {
|
impl SelfKind {
|
||||||
fn matches(&self, slf: &ExplicitSelf_, allow_value_for_ref: bool) -> bool {
|
fn matches(&self, slf: &ExplicitSelf_, allow_value_for_ref: bool) -> bool {
|
||||||
match (self, slf) {
|
match (self, slf) {
|
||||||
(&ValueSelf, &SelfValue(_)) => true,
|
(&SelfKind::Value, &SelfValue(_)) => true,
|
||||||
(&RefSelf, &SelfRegion(_, Mutability::MutImmutable, _)) => true,
|
(&SelfKind::Ref, &SelfRegion(_, Mutability::MutImmutable, _)) => true,
|
||||||
(&RefMutSelf, &SelfRegion(_, Mutability::MutMutable, _)) => true,
|
(&SelfKind::RefMut, &SelfRegion(_, Mutability::MutMutable, _)) => true,
|
||||||
(&RefSelf, &SelfValue(_)) => allow_value_for_ref,
|
(&SelfKind::Ref, &SelfValue(_)) => allow_value_for_ref,
|
||||||
(&RefMutSelf, &SelfValue(_)) => allow_value_for_ref,
|
(&SelfKind::RefMut, &SelfValue(_)) => allow_value_for_ref,
|
||||||
(&NoSelf, &SelfStatic) => true,
|
(&SelfKind::No, &SelfStatic) => true,
|
||||||
(_, &SelfExplicit(ref ty, _)) => self.matches_explicit_type(ty, allow_value_for_ref),
|
(_, &SelfExplicit(ref ty, _)) => self.matches_explicit_type(ty, allow_value_for_ref),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
@ -912,41 +909,41 @@ impl SelfKind {
|
||||||
|
|
||||||
fn matches_explicit_type(&self, ty: &Ty, allow_value_for_ref: bool) -> bool {
|
fn matches_explicit_type(&self, ty: &Ty, allow_value_for_ref: bool) -> bool {
|
||||||
match (self, &ty.node) {
|
match (self, &ty.node) {
|
||||||
(&ValueSelf, &TyPath(..)) => true,
|
(&SelfKind::Value, &TyPath(..)) => true,
|
||||||
(&RefSelf, &TyRptr(_, MutTy { mutbl: Mutability::MutImmutable, .. })) => true,
|
(&SelfKind::Ref, &TyRptr(_, MutTy { mutbl: Mutability::MutImmutable, .. })) => true,
|
||||||
(&RefMutSelf, &TyRptr(_, MutTy { mutbl: Mutability::MutMutable, .. })) => true,
|
(&SelfKind::RefMut, &TyRptr(_, MutTy { mutbl: Mutability::MutMutable, .. })) => true,
|
||||||
(&RefSelf, &TyPath(..)) => allow_value_for_ref,
|
(&SelfKind::Ref, &TyPath(..)) => allow_value_for_ref,
|
||||||
(&RefMutSelf, &TyPath(..)) => allow_value_for_ref,
|
(&SelfKind::RefMut, &TyPath(..)) => allow_value_for_ref,
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn description(&self) -> &'static str {
|
fn description(&self) -> &'static str {
|
||||||
match *self {
|
match *self {
|
||||||
ValueSelf => "self by value",
|
SelfKind::Value => "self by value",
|
||||||
RefSelf => "self by reference",
|
SelfKind::Ref => "self by reference",
|
||||||
RefMutSelf => "self by mutable reference",
|
SelfKind::RefMut => "self by mutable reference",
|
||||||
NoSelf => "no self",
|
SelfKind::No => "no self",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
#[derive(Clone, Copy)]
|
||||||
enum OutType {
|
enum OutType {
|
||||||
UnitType,
|
Unit,
|
||||||
BoolType,
|
Bool,
|
||||||
AnyType,
|
Any,
|
||||||
RefType,
|
Ref,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl OutType {
|
impl OutType {
|
||||||
fn matches(&self, ty: &FunctionRetTy) -> bool {
|
fn matches(&self, ty: &FunctionRetTy) -> bool {
|
||||||
match (self, ty) {
|
match (self, ty) {
|
||||||
(&UnitType, &DefaultReturn(_)) => true,
|
(&OutType::Unit, &DefaultReturn(_)) => true,
|
||||||
(&UnitType, &Return(ref ty)) if ty.node == TyTup(vec![].into()) => true,
|
(&OutType::Unit, &Return(ref ty)) if ty.node == TyTup(vec![].into()) => true,
|
||||||
(&BoolType, &Return(ref ty)) if is_bool(ty) => true,
|
(&OutType::Bool, &Return(ref ty)) if is_bool(ty) => true,
|
||||||
(&AnyType, &Return(ref ty)) if ty.node != TyTup(vec![].into()) => true,
|
(&OutType::Any, &Return(ref ty)) if ty.node != TyTup(vec![].into()) => true,
|
||||||
(&RefType, &Return(ref ty)) => {
|
(&OutType::Ref, &Return(ref ty)) => {
|
||||||
if let TyRptr(_, _) = ty.node {
|
if let TyRptr(_, _) = ty.node {
|
||||||
true
|
true
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -35,8 +35,8 @@ impl LateLintPass for ZeroDivZeroPass {
|
||||||
// TODO - constant_simple does not fold many operations involving floats.
|
// TODO - constant_simple does not fold many operations involving floats.
|
||||||
// That's probably fine for this lint - it's pretty unlikely that someone would
|
// That's probably fine for this lint - it's pretty unlikely that someone would
|
||||||
// do something like 0.0/(2.0 - 2.0), but it would be nice to warn on that case too.
|
// do something like 0.0/(2.0 - 2.0), but it would be nice to warn on that case too.
|
||||||
let Some(Constant::ConstantFloat(ref lhs_value, lhs_width)) = constant_simple(left),
|
let Some(Constant::Float(ref lhs_value, lhs_width)) = constant_simple(left),
|
||||||
let Some(Constant::ConstantFloat(ref rhs_value, rhs_width)) = constant_simple(right),
|
let Some(Constant::Float(ref rhs_value, rhs_width)) = constant_simple(right),
|
||||||
let Some(0.0) = lhs_value.parse().ok(),
|
let Some(0.0) = lhs_value.parse().ok(),
|
||||||
let Some(0.0) = rhs_value.parse().ok()
|
let Some(0.0) = rhs_value.parse().ok()
|
||||||
],
|
],
|
||||||
|
|
|
@ -3,8 +3,8 @@
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
enum Baz {
|
enum Baz {
|
||||||
Baz1,
|
One,
|
||||||
Baz2,
|
Two,
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Test {
|
struct Test {
|
||||||
|
@ -14,11 +14,11 @@ struct Test {
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
use Baz::*;
|
use Baz::*;
|
||||||
let x = Test { t: Some(0), b: Baz1 };
|
let x = Test { t: Some(0), b: One };
|
||||||
|
|
||||||
match x {
|
match x {
|
||||||
Test { t: Some(_), b: Baz1 } => unreachable!(),
|
Test { t: Some(_), b: One } => unreachable!(),
|
||||||
Test { t: Some(42), b: Baz2 } => unreachable!(),
|
Test { t: Some(42), b: Two } => unreachable!(),
|
||||||
Test { t: None, .. } => unreachable!(),
|
Test { t: None, .. } => unreachable!(),
|
||||||
Test { .. } => unreachable!(),
|
Test { .. } => unreachable!(),
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,8 @@ struct S {
|
||||||
struct TS(Vec<Vec<Box<(u32, u32, u32, u32)>>>); //~ERROR very complex type
|
struct TS(Vec<Vec<Box<(u32, u32, u32, u32)>>>); //~ERROR very complex type
|
||||||
|
|
||||||
enum E {
|
enum E {
|
||||||
V1(Vec<Vec<Box<(u32, u32, u32, u32)>>>), //~ERROR very complex type
|
Tuple(Vec<Vec<Box<(u32, u32, u32, u32)>>>), //~ERROR very complex type
|
||||||
V2 { f: Vec<Vec<Box<(u32, u32, u32, u32)>>> }, //~ERROR very complex type
|
Struct { f: Vec<Vec<Box<(u32, u32, u32, u32)>>> }, //~ERROR very complex type
|
||||||
}
|
}
|
||||||
|
|
||||||
impl S {
|
impl S {
|
||||||
|
|
|
@ -11,8 +11,8 @@ struct Struct {
|
||||||
field: i32
|
field: i32
|
||||||
}
|
}
|
||||||
enum Enum {
|
enum Enum {
|
||||||
TupleVariant(i32),
|
Tuple(i32),
|
||||||
StructVariant { field: i32 },
|
Struct { field: i32 },
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_number() -> i32 { 0 }
|
fn get_number() -> i32 { 0 }
|
||||||
|
@ -26,14 +26,14 @@ fn main() {
|
||||||
Tuple(0); //~ERROR statement with no effect
|
Tuple(0); //~ERROR statement with no effect
|
||||||
Struct { field: 0 }; //~ERROR statement with no effect
|
Struct { field: 0 }; //~ERROR statement with no effect
|
||||||
Struct { ..s }; //~ERROR statement with no effect
|
Struct { ..s }; //~ERROR statement with no effect
|
||||||
Enum::TupleVariant(0); //~ERROR statement with no effect
|
Enum::Tuple(0); //~ERROR statement with no effect
|
||||||
Enum::StructVariant { field: 0 }; //~ERROR statement with no effect
|
Enum::Struct { field: 0 }; //~ERROR statement with no effect
|
||||||
|
|
||||||
// Do not warn
|
// Do not warn
|
||||||
get_number();
|
get_number();
|
||||||
Tuple(get_number());
|
Tuple(get_number());
|
||||||
Struct { field: get_number() };
|
Struct { field: get_number() };
|
||||||
Struct { ..get_struct() };
|
Struct { ..get_struct() };
|
||||||
Enum::TupleVariant(get_number());
|
Enum::Tuple(get_number());
|
||||||
Enum::StructVariant { field: get_number() };
|
Enum::Struct { field: get_number() };
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,17 +50,17 @@ fn multiple_underscores(__foo: u32) -> u32 {
|
||||||
fn _fn_test() {}
|
fn _fn_test() {}
|
||||||
struct _StructTest;
|
struct _StructTest;
|
||||||
enum _EnumTest {
|
enum _EnumTest {
|
||||||
_FieldA,
|
_Empty,
|
||||||
_FieldB(_StructTest)
|
_Value(_StructTest)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Test that we do not lint for non-variable bindings
|
/// Test that we do not lint for non-variable bindings
|
||||||
fn non_variables() {
|
fn non_variables() {
|
||||||
_fn_test();
|
_fn_test();
|
||||||
let _s = _StructTest;
|
let _s = _StructTest;
|
||||||
let _e = match _EnumTest::_FieldB(_StructTest) {
|
let _e = match _EnumTest::_Value(_StructTest) {
|
||||||
_EnumTest::_FieldA => 0,
|
_EnumTest::_Empty => 0,
|
||||||
_EnumTest::_FieldB(_st) => 1,
|
_EnumTest::_Value(_st) => 1,
|
||||||
};
|
};
|
||||||
let f = _fn_test;
|
let f = _fn_test;
|
||||||
f();
|
f();
|
||||||
|
|
|
@ -18,7 +18,6 @@ use syntax::ast::StrStyle::*;
|
||||||
use syntax::ast::Sign::*;
|
use syntax::ast::Sign::*;
|
||||||
|
|
||||||
use clippy::consts::{constant_simple, Constant};
|
use clippy::consts::{constant_simple, Constant};
|
||||||
use clippy::consts::Constant::*;
|
|
||||||
|
|
||||||
fn spanned<T>(t: T) -> Spanned<T> {
|
fn spanned<T>(t: T) -> Spanned<T> {
|
||||||
Spanned{ node: t, span: COMMAND_LINE_SP }
|
Spanned{ node: t, span: COMMAND_LINE_SP }
|
||||||
|
@ -45,18 +44,18 @@ fn check(expect: Constant, expr: &Expr) {
|
||||||
assert_eq!(Some(expect), constant_simple(expr))
|
assert_eq!(Some(expect), constant_simple(expr))
|
||||||
}
|
}
|
||||||
|
|
||||||
const TRUE : Constant = ConstantBool(true);
|
const TRUE : Constant = Constant::Bool(true);
|
||||||
const FALSE : Constant = ConstantBool(false);
|
const FALSE : Constant = Constant::Bool(false);
|
||||||
const ZERO : Constant = ConstantInt(0, UnsuffixedIntLit(Plus));
|
const ZERO : Constant = Constant::Int(0, UnsuffixedIntLit(Plus));
|
||||||
const ONE : Constant = ConstantInt(1, UnsuffixedIntLit(Plus));
|
const ONE : Constant = Constant::Int(1, UnsuffixedIntLit(Plus));
|
||||||
const TWO : Constant = ConstantInt(2, UnsuffixedIntLit(Plus));
|
const TWO : Constant = Constant::Int(2, UnsuffixedIntLit(Plus));
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_lit() {
|
fn test_lit() {
|
||||||
check(TRUE, &lit(LitBool(true)));
|
check(TRUE, &lit(LitBool(true)));
|
||||||
check(FALSE, &lit(LitBool(false)));
|
check(FALSE, &lit(LitBool(false)));
|
||||||
check(ZERO, &lit(LitInt(0, UnsuffixedIntLit(Plus))));
|
check(ZERO, &lit(LitInt(0, UnsuffixedIntLit(Plus))));
|
||||||
check(ConstantStr("cool!".into(), CookedStr), &lit(LitStr(
|
check(Constant::Str("cool!".into(), CookedStr), &lit(LitStr(
|
||||||
InternedString::new("cool!"), CookedStr)));
|
InternedString::new("cool!"), CookedStr)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue