Skip to content

Commit 330be17

Browse files
committed
Detect overflow when the literal is negative
1 parent be23697 commit 330be17

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

compiler/rustc_lint/src/types/literal.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,13 @@ fn get_type_suggestion(t: Ty<'_>, val: u128, negative: bool) -> Option<&'static
204204
match t.kind() {
205205
ty::Uint(ty::UintTy::Usize) | ty::Int(ty::IntTy::Isize) => None,
206206
ty::Uint(_) => Some(Integer::fit_unsigned(val).uint_ty_str()),
207-
ty::Int(_) if negative => Some(Integer::fit_signed(-(val as i128)).int_ty_str()),
207+
ty::Int(_) if negative => {
208+
if val > i128::MAX as u128 + 1 {
209+
None
210+
} else {
211+
Some(Integer::fit_signed(val.wrapping_neg() as i128).int_ty_str())
212+
}
213+
}
208214
ty::Int(int) => {
209215
let unsigned = Integer::fit_unsigned(val);
210216
Some(if let Ok(signed) = i128::try_from(val).map(Integer::fit_signed) {

0 commit comments

Comments
 (0)