Skip to content

Commit

Permalink
Rollup merge of rust-lang#4764 - chansuke:fix_checked_abs, r=flip1995
Browse files Browse the repository at this point in the history
Allow casts from the result of `checked_abs` to unsigned

Fixes rust-lang#4743.
  • Loading branch information
flip1995 committed Nov 23, 2019
2 parents a164a37 + c3b0ece commit a988649
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
4 changes: 2 additions & 2 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1020,12 +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";
if ["abs", "checked_abs"].contains(path.ident.name);
then {
return
}
Expand Down
5 changes: 5 additions & 0 deletions tests/ui/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ fn main() {
(-1i32).abs() as u32;
(-1i64).abs() as u64;
(-1isize).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;
}

0 comments on commit a988649

Please sign in to comment.