Skip to content

Commit a338351

Browse files
committed
reduce some code repetitions
1 parent d38d6be commit a338351

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/librustc_lint/types.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ use syntax::source_map;
2121

2222
use rustc::hir;
2323

24+
use rustc::mir::interpret::{sign_extend, truncate};
25+
2426
declare_lint! {
2527
UNUSED_COMPARISONS,
2628
Warn,
@@ -368,14 +370,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TypeLimits {
368370
let (t, actually) = match ty {
369371
ty::Int(t) => {
370372
let ity = attr::IntType::SignedInt(t);
371-
let bits = layout::Integer::from_attr(&cx.tcx, ity).size().bits();
372-
let actually = (val << (128 - bits)) as i128 >> (128 - bits);
373+
let size = layout::Integer::from_attr(&cx.tcx, ity).size();
374+
let actually = sign_extend(val, size);
373375
(format!("{:?}", t), actually.to_string())
374376
}
375377
ty::Uint(t) => {
376378
let ity = attr::IntType::UnsignedInt(t);
377-
let bits = layout::Integer::from_attr(&cx.tcx, ity).size().bits();
378-
let actually = (val << (128 - bits)) >> (128 - bits);
379+
let size = layout::Integer::from_attr(&cx.tcx, ity).size();
380+
let actually = truncate(val, size);
379381
(format!("{:?}", t), actually.to_string())
380382
}
381383
_ => bug!(),

0 commit comments

Comments
 (0)