From 23c23922f5f2f0d350dd41f32347bade9c9413a4 Mon Sep 17 00:00:00 2001 From: chansuke Date: Fri, 1 Nov 2019 23:45:29 +0900 Subject: [PATCH 1/3] Allow casts from the result of `checked_abs` to unsigned --- clippy_lints/src/types.rs | 9 +++++++++ tests/ui/cast.rs | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 6a105a87e623..8e25ed45d93c 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -1031,6 +1031,15 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro } } + // don't lint for the result of `checked_abs` + if_chain! { + if let ExprKind::MethodCall(ref path, _, _) = op.kind; + if path.ident.name.as_str() == "checked_abs"; + then { + return + } + } + span_lint( cx, CAST_SIGN_LOSS, diff --git a/tests/ui/cast.rs b/tests/ui/cast.rs index 80329a52c2d0..e592c4e05c88 100644 --- a/tests/ui/cast.rs +++ b/tests/ui/cast.rs @@ -47,4 +47,9 @@ fn main() { (-1i32).abs() as u32; (-1i64).abs() as u64; (-1isize).abs() as usize; + (-1i8).checked_abs() as u8; + (-1i16).checked_abs() as u16; + (-1i32).checked_abs() as u32; + (-1i64).checked_abs() as u64; + (-1isize).checked_abs() as usize; } From a2875a0d135457d66eeb67e29b58458c74ea9c28 Mon Sep 17 00:00:00 2001 From: chansuke Date: Sun, 10 Nov 2019 01:22:50 +0900 Subject: [PATCH 2/3] Add unwrap() to handle contained values --- tests/ui/cast.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/ui/cast.rs b/tests/ui/cast.rs index e592c4e05c88..b8ada45eeaaf 100644 --- a/tests/ui/cast.rs +++ b/tests/ui/cast.rs @@ -47,9 +47,9 @@ fn main() { (-1i32).abs() as u32; (-1i64).abs() as u64; (-1isize).abs() as usize; - (-1i8).checked_abs() as u8; - (-1i16).checked_abs() as u16; - (-1i32).checked_abs() as u32; - (-1i64).checked_abs() as u64; - (-1isize).checked_abs() as usize; + (-1i8).checked_abs().unwrap() as u8; + (-1i16).checked_abs().unwrap() as u16; + (-1i32).checked_abs().unwrap() as u32; + (-1i64).checked_abs().unwrap() as u64; + (-1isize).checked_abs().unwrap() as usize; } From c3b0ecec4f86c2a5cd60f000f32d16aef0ec8f6f Mon Sep 17 00:00:00 2001 From: chansuke Date: Wed, 13 Nov 2019 20:54:50 +0900 Subject: [PATCH 3/3] Deduplicate if_chain method --- clippy_lints/src/types.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index 8e25ed45d93c..be23af41eb59 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -1020,21 +1020,12 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro } } - // don't lint for the result of `abs` + // don't lint for the result of `abs` & `checked_abs` // `abs` is an inherent impl of `i{N}`, so a method call with ident `abs` will always // resolve to that spesific method if_chain! { if let ExprKind::MethodCall(ref path, _, _) = op.kind; - if path.ident.name.as_str() == "abs"; - then { - return - } - } - - // don't lint for the result of `checked_abs` - if_chain! { - if let ExprKind::MethodCall(ref path, _, _) = op.kind; - if path.ident.name.as_str() == "checked_abs"; + if ["abs", "checked_abs"].contains(path.ident.name); then { return }