mirror of
https://github.com/rust-lang/rust-clippy
synced 2025-02-17 06:28:42 +00:00
fix Eq
+Hash
for Constant
This commit is contained in:
parent
432d9fec38
commit
64110f16dd
3 changed files with 11 additions and 6 deletions
|
@ -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
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue