diff --git a/src/consts.rs b/src/consts.rs index 06f790d8308f..3e08f1b74ff8 100644 --- a/src/consts.rs +++ b/src/consts.rs @@ -85,7 +85,7 @@ impl PartialEq for Constant { (&Constant::Str(ref ls, ref lsty), &Constant::Str(ref rs, ref rsty)) => ls == rs && lsty == rsty, (&Constant::Binary(ref l), &Constant::Binary(ref r)) => l == r, (&Constant::Char(l), &Constant::Char(r)) => l == r, - (&Constant::Int(l), &Constant::Int(r)) => l == r, + (&Constant::Int(l), &Constant::Int(r)) => l.is_negative() == r.is_negative() && l.to_u64_unchecked() == r.to_u64_unchecked(), (&Constant::Float(ref ls, _), &Constant::Float(ref rs, _)) => { // we want `Fw32 == FwAny` and `FwAny == Fw64`, by transitivity we must have // `Fw32 == Fw64` so don’t compare them @@ -119,7 +119,8 @@ impl Hash for Constant { c.hash(state); } Constant::Int(i) => { - i.hash(state); + i.to_u64_unchecked().hash(state); + i.is_negative().hash(state); } Constant::Float(ref f, _) => { // don’t use the width here because of PartialEq implementation diff --git a/src/identity_op.rs b/src/identity_op.rs index 21167ce768d5..9ade801abb37 100644 --- a/src/identity_op.rs +++ b/src/identity_op.rs @@ -55,11 +55,11 @@ impl LateLintPass for IdentityOp { fn check(cx: &LateContext, e: &Expr, m: i8, span: Span, arg: Span) { - if let Some(Constant::Int(v)) = constant_simple(e) { + if let Some(v @ Constant::Int(_)) = constant_simple(e) { if match m { - 0 => v == ConstInt::Infer(0), - -1 => v == ConstInt::InferSigned(-1), - 1 => v == ConstInt::Infer(1), + 0 => v == Constant::Int(ConstInt::Infer(0)), + -1 => v == Constant::Int(ConstInt::InferSigned(-1)), + 1 => v == Constant::Int(ConstInt::Infer(1)), _ => unreachable!(), } { span_lint(cx, diff --git a/tests/consts.rs b/tests/consts.rs index 78853f7c1e57..3a774f674734 100644 --- a/tests/consts.rs +++ b/tests/consts.rs @@ -86,4 +86,8 @@ fn test_ops() { assert_eq!(half_any, half32); assert_eq!(half_any, half64); assert_eq!(half32, half64); // for transitivity + + assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::U8(0))); + assert_eq!(Constant::Int(ConstInt::Infer(0)), Constant::Int(ConstInt::I8(0))); + assert_eq!(Constant::Int(ConstInt::InferSigned(-1)), Constant::Int(ConstInt::I8(-1))); }