Skip to content

Commit 19c58d2

Browse files
authored
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
2 parents 8d2912e + 0e1dd65 commit 19c58d2

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

clippy_lints/src/types.rs

+11
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,17 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro
986986
}
987987
}
988988

989+
// don't lint for the result of `abs`
990+
// `abs` is an inherent impl of `i{N}`, so a method call with ident `abs` will always
991+
// resolve to that spesific method
992+
if_chain! {
993+
if let ExprKind::MethodCall(ref path, _, _) = op.kind;
994+
if path.ident.name.as_str() == "abs";
995+
then {
996+
return
997+
}
998+
}
999+
9891000
span_lint(
9901001
cx,
9911002
CAST_SIGN_LOSS,

tests/ui/cast.rs

+5
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ fn main() {
4242
i32::max_value() as u32;
4343
i64::max_value() as u64;
4444
i128::max_value() as u128;
45+
(-1i8).abs() as u8;
46+
(-1i16).abs() as u16;
47+
(-1i32).abs() as u32;
48+
(-1i64).abs() as u64;
49+
(-1isize).abs() as usize;
4550
}

0 commit comments

Comments
 (0)