Rollup merge of #135249 - s-cerevisiae:fix-overflowing-literals-help, r=chenyukang
Fix overflows in the implementation of `overflowing_literals` lint's help This PR fixes two overflow problems that cause the `overflowing_literals` lint to behave incorrectly in some edge cases. 1. When an integer literal is between `i128::MAX` and `u128::MAX`, an overflowing `as` cast can cause the suggested type to be overly small. It's fixed by using checked type conversion and returning `u128` when it's the only choice. (Fixes #135248) 2. When an integer literal is `i128::MIN` but is of a smaller type, an overflowing negation cause the compiler to panic in debug build. Fixed by checking the number size beforehand and `wrapping_neg`. (Fixes #131849) Edit: extracted the type conversion part into a standalone function to separate the concern of overflowing.
This commit is contained in:
commit
8f9ccc5d1b
3 changed files with 94 additions and 16 deletions
|
@ -204,20 +204,35 @@ fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static
|
|||
match t.kind() {
|
||||
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
|
||||
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
|
||||
ty::Int(_) if negative => Some(Integer::fit_signed(-(val as i128)).int_ty_str()),
|
||||
ty::Int(int) => {
|
||||
let signed = Integer::fit_signed(val as i128);
|
||||
let unsigned = Integer::fit_unsigned(val);
|
||||
Some(if Some(unsigned.size().bits()) == int.bit_width() {
|
||||
unsigned.uint_ty_str()
|
||||
ty::Int(_) => {
|
||||
let signed = literal_to_i128(val, negative).map(Integer::fit_signed);
|
||||
if negative {
|
||||
signed.map(Integer::int_ty_str)
|
||||
} else {
|
||||
signed.int_ty_str()
|
||||
})
|
||||
let unsigned = Integer::fit_unsigned(val);
|
||||
Some(if let Some(signed) = signed {
|
||||
if unsigned.size() < signed.size() {
|
||||
unsigned.uint_ty_str()
|
||||
} else {
|
||||
signed.int_ty_str()
|
||||
}
|
||||
} else {
|
||||
unsigned.uint_ty_str()
|
||||
})
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn literal_to_i128(val: u128, negative: bool) -> Option<i128> {
|
||||
if negative {
|
||||
(val <= i128::MAX as u128 + 1).then(|| val.wrapping_neg() as i128)
|
||||
} else {
|
||||
val.try_into().ok()
|
||||
}
|
||||
}
|
||||
|
||||
fn lint_int_literal<'tcx>(
|
||||
cx: &LateContext<'tcx>,
|
||||
type_limits: &TypeLimits,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue