Skip to content

Commit 00d23cf

Browse files
committed
Make {u,}int_range functions a bit nicer
.into() guarantees safety of the conversion. Furthermore, the minimum value of all uints is known to be 0.
1 parent c647735 commit 00d23cf

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

compiler/rustc_lint/src/types.rs

+15-14
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,25 @@ fn lint_overflowing_range_endpoint<'tcx>(
170170
// warnings are consistent between 32- and 64-bit platforms.
171171
fn int_ty_range(int_ty: ast::IntTy) -> (i128, i128) {
172172
match int_ty {
173-
ast::IntTy::Isize => (i64::MIN as i128, i64::MAX as i128),
174-
ast::IntTy::I8 => (i8::MIN as i64 as i128, i8::MAX as i128),
175-
ast::IntTy::I16 => (i16::MIN as i64 as i128, i16::MAX as i128),
176-
ast::IntTy::I32 => (i32::MIN as i64 as i128, i32::MAX as i128),
177-
ast::IntTy::I64 => (i64::MIN as i128, i64::MAX as i128),
178-
ast::IntTy::I128 => (i128::MIN as i128, i128::MAX),
173+
ast::IntTy::Isize => (i64::MIN.into(), i64::MAX.into()),
174+
ast::IntTy::I8 => (i8::MIN.into(), i8::MAX.into()),
175+
ast::IntTy::I16 => (i16::MIN.into(), i16::MAX.into()),
176+
ast::IntTy::I32 => (i32::MIN.into(), i32::MAX.into()),
177+
ast::IntTy::I64 => (i64::MIN.into(), i64::MAX.into()),
178+
ast::IntTy::I128 => (i128::MIN, i128::MAX),
179179
}
180180
}
181181

182182
fn uint_ty_range(uint_ty: ast::UintTy) -> (u128, u128) {
183-
match uint_ty {
184-
ast::UintTy::Usize => (u64::MIN as u128, u64::MAX as u128),
185-
ast::UintTy::U8 => (u8::MIN as u128, u8::MAX as u128),
186-
ast::UintTy::U16 => (u16::MIN as u128, u16::MAX as u128),
187-
ast::UintTy::U32 => (u32::MIN as u128, u32::MAX as u128),
188-
ast::UintTy::U64 => (u64::MIN as u128, u64::MAX as u128),
189-
ast::UintTy::U128 => (u128::MIN, u128::MAX),
190-
}
183+
let max = match uint_ty {
184+
ast::UintTy::Usize => u64::MAX.into(),
185+
ast::UintTy::U8 => u8::MAX.into(),
186+
ast::UintTy::U16 => u16::MAX.into(),
187+
ast::UintTy::U32 => u32::MAX.into(),
188+
ast::UintTy::U64 => u64::MAX.into(),
189+
ast::UintTy::U128 => u128::MAX,
190+
};
191+
(0, max)
191192
}
192193

193194
fn get_bin_hex_repr(cx: &LateContext<'_>, lit: &hir::Lit) -> Option<String> {

0 commit comments

Comments
 (0)