diff options
| author | Phil Hansch <dev@phansch.net> | 2019-10-04 08:08:59 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-10-04 08:08:59 +0200 |
| commit | 19c58d260bca9702a4fb4079e360b61248ac8738 (patch) | |
| tree | a2d02733357a4e1603e357df74f4a46f38be833f | |
| parent | 8d2912ec00a42698e44db924495bcc30a090ee2b (diff) | |
| parent | 0e1dd65c14743b5b58c546ccaadc209f390af7df (diff) | |
| download | rust-19c58d260bca9702a4fb4079e360b61248ac8738.tar.gz rust-19c58d260bca9702a4fb4079e360b61248ac8738.zip | |
Rollup merge of #4614 - HMPerson1:abs_cast_unsigned, r=flip1995
Allow casts from the result of `abs` to unsigned changelog: Allow casts from the result of `abs` to unsigned in `cast_sign_loss` Fixes #4605
| -rw-r--r-- | clippy_lints/src/types.rs | 11 | ||||
| -rw-r--r-- | tests/ui/cast.rs | 5 |
2 files changed, 16 insertions, 0 deletions
diff --git a/clippy_lints/src/types.rs b/clippy_lints/src/types.rs index e976b055791..133001e35f7 100644 --- a/clippy_lints/src/types.rs +++ b/clippy_lints/src/types.rs @@ -986,6 +986,17 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro } } + // don't lint for the result of `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 + } + } + span_lint( cx, CAST_SIGN_LOSS, diff --git a/tests/ui/cast.rs b/tests/ui/cast.rs index 79705071999..80329a52c2d 100644 --- a/tests/ui/cast.rs +++ b/tests/ui/cast.rs @@ -42,4 +42,9 @@ fn main() { i32::max_value() as u32; i64::max_value() as u64; i128::max_value() as u128; + (-1i8).abs() as u8; + (-1i16).abs() as u16; + (-1i32).abs() as u32; + (-1i64).abs() as u64; + (-1isize).abs() as usize; } |
