Skip to content

Commit 23c2392

Browse files
author
chansuke
committed
Allow casts from the result of checked_abs to unsigned
1 parent 180f870 commit 23c2392

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

clippy_lints/src/types.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,15 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro
10311031
}
10321032
}
10331033

1034+
// don't lint for the result of `checked_abs`
1035+
if_chain! {
1036+
if let ExprKind::MethodCall(ref path, _, _) = op.kind;
1037+
if path.ident.name.as_str() == "checked_abs";
1038+
then {
1039+
return
1040+
}
1041+
}
1042+
10341043
span_lint(
10351044
cx,
10361045
CAST_SIGN_LOSS,

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() as u8;
51+
(-1i16).checked_abs() as u16;
52+
(-1i32).checked_abs() as u32;
53+
(-1i64).checked_abs() as u64;
54+
(-1isize).checked_abs() as usize;
5055
}

0 commit comments

Comments
 (0)