Skip to content

Commit

Permalink
Rollup merge of #4614 - HMPerson1:abs_cast_unsigned, r=flip1995
Browse files Browse the repository at this point in the history
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
  • Loading branch information
phansch authored Oct 4, 2019
2 parents 8d2912e + 0e1dd65 commit 19c58d2
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
11 changes: 11 additions & 0 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

0 comments on commit 19c58d2

Please sign in to comment.