From a3383514d9db48295124b349323379952c33a2c4 Mon Sep 17 00:00:00 2001 From: kenta7777 Date: Mon, 10 Dec 2018 23:45:33 +0900 Subject: [PATCH 1/2] reduce some code repetitions --- src/librustc_lint/types.rs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index 642681a73a8a0..6e394f78226cd 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -21,6 +21,8 @@ use syntax::source_map; use rustc::hir; +use rustc::mir::interpret::{sign_extend, truncate}; + declare_lint! { UNUSED_COMPARISONS, Warn, @@ -368,14 +370,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { let (t, actually) = match ty { ty::Int(t) => { let ity = attr::IntType::SignedInt(t); - let bits = layout::Integer::from_attr(&cx.tcx, ity).size().bits(); - let actually = (val << (128 - bits)) as i128 >> (128 - bits); + let size = layout::Integer::from_attr(&cx.tcx, ity).size(); + let actually = sign_extend(val, size); (format!("{:?}", t), actually.to_string()) } ty::Uint(t) => { let ity = attr::IntType::UnsignedInt(t); - let bits = layout::Integer::from_attr(&cx.tcx, ity).size().bits(); - let actually = (val << (128 - bits)) >> (128 - bits); + let size = layout::Integer::from_attr(&cx.tcx, ity).size(); + let actually = truncate(val, size); (format!("{:?}", t), actually.to_string()) } _ => bug!(), From b80332ed4c142496096ff2bf9aa378cc6b3bf806 Mon Sep 17 00:00:00 2001 From: kenta7777 Date: Sun, 20 Jan 2019 10:45:25 +0900 Subject: [PATCH 2/2] cast the sign_extend result to i128. --- src/librustc_lint/types.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_lint/types.rs b/src/librustc_lint/types.rs index 6e394f78226cd..9d3275ffde2c0 100644 --- a/src/librustc_lint/types.rs +++ b/src/librustc_lint/types.rs @@ -371,7 +371,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits { ty::Int(t) => { let ity = attr::IntType::SignedInt(t); let size = layout::Integer::from_attr(&cx.tcx, ity).size(); - let actually = sign_extend(val, size); + let actually = sign_extend(val, size) as i128; (format!("{:?}", t), actually.to_string()) } ty::Uint(t) => {