Skip to content

Commit a988649

Browse files
authored
Rollup merge of #4764 - chansuke:fix_checked_abs, r=flip1995
Allow casts from the result of `checked_abs` to unsigned Fixes #4743.
2 parents a164a37 + c3b0ece commit a988649

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

clippy_lints/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,12 +1020,12 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro
10201020
}
10211021
}
10221022

1023-
// don't lint for the result of `abs`
1023+
// don't lint for the result of `abs` & `checked_abs`
10241024
// `abs` is an inherent impl of `i{N}`, so a method call with ident `abs` will always
10251025
// resolve to that spesific method
10261026
if_chain! {
10271027
if let ExprKind::MethodCall(ref path, _, _) = op.kind;
1028-
if path.ident.name.as_str() == "abs";
1028+
if ["abs", "checked_abs"].contains(path.ident.name);
10291029
then {
10301030
return
10311031
}

tests/ui/cast.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,9 @@ fn main() {
4747
(-1i32).abs() as u32;
4848
(-1i64).abs() as u64;
4949
(-1isize).abs() as usize;
50+
(-1i8).checked_abs().unwrap() as u8;
51+
(-1i16).checked_abs().unwrap() as u16;
52+
(-1i32).checked_abs().unwrap() as u32;
53+
(-1i64).checked_abs().unwrap() as u64;
54+
(-1isize).checked_abs().unwrap() as usize;
5055
}

0 commit comments

Comments
 (0)