Skip to content

Commit d82debb

Browse files
committed
Auto merge of #4883 - krishna-veerareddy:issue-4818-cast-sign-loss-false-positive, r=flip1995
Fix false positive with cast_sign_loss lint `cast_sign_loss` lint incorrectly suggests that the result of `checked_abs`, `rem_euclid` and `checked_rem_euclid` cannot be casted to an unsigned integer without loss. Fixes #4818 #4764 #4743 changelog: Fix false positives in `cast_sign_loss` lint
2 parents 221bf65 + c0fb74b commit d82debb

File tree

2 files changed

+64
-10
lines changed

2 files changed

+64
-10
lines changed

clippy_lints/src/types.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ use crate::consts::{constant, Constant};
2626
use crate::utils::paths;
2727
use crate::utils::{
2828
clip, comparisons, differing_macro_contexts, higher, in_constant, int_bits, last_path_segment, match_def_path,
29-
match_path, multispan_sugg, qpath_res, same_tys, sext, snippet, snippet_opt, snippet_with_applicability,
30-
snippet_with_macro_callsite, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext,
29+
match_path, method_chain_args, multispan_sugg, qpath_res, same_tys, sext, snippet, snippet_opt,
30+
snippet_with_applicability, snippet_with_macro_callsite, span_help_and_lint, span_lint, span_lint_and_sugg,
31+
span_lint_and_then, unsext,
3132
};
3233

3334
declare_clippy_lint! {
@@ -1021,14 +1022,22 @@ fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_fro
10211022
}
10221023
}
10231024

1024-
// don't lint for the result of `abs`
1025-
// `abs` is an inherent impl of `i{N}`, so a method call with ident `abs` will always
1026-
// resolve to that spesific method
1027-
if_chain! {
1028-
if let ExprKind::MethodCall(ref path, _, _) = op.kind;
1029-
if path.ident.name.as_str() == "abs";
1030-
then {
1031-
return
1025+
// don't lint for the result of methods that always return non-negative values
1026+
if let ExprKind::MethodCall(ref path, _, _) = op.kind {
1027+
let mut method_name = path.ident.name.as_str();
1028+
let whitelisted_methods = ["abs", "checked_abs", "rem_euclid", "checked_rem_euclid"];
1029+
1030+
if_chain! {
1031+
if method_name == "unwrap";
1032+
if let Some(arglist) = method_chain_args(op, &["unwrap"]);
1033+
if let ExprKind::MethodCall(ref inner_path, _, _) = &arglist[0][0].kind;
1034+
then {
1035+
method_name = inner_path.ident.name.as_str();
1036+
}
1037+
}
1038+
1039+
if whitelisted_methods.iter().any(|&name| method_name == name) {
1040+
return;
10321041
}
10331042
}
10341043

tests/ui/cast.rs

+45
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,54 @@ fn main() {
4242
i32::max_value() as u32;
4343
i64::max_value() as u64;
4444
i128::max_value() as u128;
45+
4546
(-1i8).abs() as u8;
4647
(-1i16).abs() as u16;
4748
(-1i32).abs() as u32;
4849
(-1i64).abs() as u64;
4950
(-1isize).abs() as usize;
51+
52+
(-1i8).checked_abs().unwrap() as u8;
53+
(-1i16).checked_abs().unwrap() as u16;
54+
(-1i32).checked_abs().unwrap() as u32;
55+
(-1i64).checked_abs().unwrap() as u64;
56+
(-1isize).checked_abs().unwrap() as usize;
57+
58+
(-1i8).rem_euclid(1i8) as u8;
59+
(-1i8).rem_euclid(1i8) as u16;
60+
(-1i16).rem_euclid(1i16) as u16;
61+
(-1i16).rem_euclid(1i16) as u32;
62+
(-1i32).rem_euclid(1i32) as u32;
63+
(-1i32).rem_euclid(1i32) as u64;
64+
(-1i64).rem_euclid(1i64) as u64;
65+
(-1i64).rem_euclid(1i64) as u128;
66+
(-1isize).rem_euclid(1isize) as usize;
67+
(1i8).rem_euclid(-1i8) as u8;
68+
(1i8).rem_euclid(-1i8) as u16;
69+
(1i16).rem_euclid(-1i16) as u16;
70+
(1i16).rem_euclid(-1i16) as u32;
71+
(1i32).rem_euclid(-1i32) as u32;
72+
(1i32).rem_euclid(-1i32) as u64;
73+
(1i64).rem_euclid(-1i64) as u64;
74+
(1i64).rem_euclid(-1i64) as u128;
75+
(1isize).rem_euclid(-1isize) as usize;
76+
77+
(-1i8).checked_rem_euclid(1i8).unwrap() as u8;
78+
(-1i8).checked_rem_euclid(1i8).unwrap() as u16;
79+
(-1i16).checked_rem_euclid(1i16).unwrap() as u16;
80+
(-1i16).checked_rem_euclid(1i16).unwrap() as u32;
81+
(-1i32).checked_rem_euclid(1i32).unwrap() as u32;
82+
(-1i32).checked_rem_euclid(1i32).unwrap() as u64;
83+
(-1i64).checked_rem_euclid(1i64).unwrap() as u64;
84+
(-1i64).checked_rem_euclid(1i64).unwrap() as u128;
85+
(-1isize).checked_rem_euclid(1isize).unwrap() as usize;
86+
(1i8).checked_rem_euclid(-1i8).unwrap() as u8;
87+
(1i8).checked_rem_euclid(-1i8).unwrap() as u16;
88+
(1i16).checked_rem_euclid(-1i16).unwrap() as u16;
89+
(1i16).checked_rem_euclid(-1i16).unwrap() as u32;
90+
(1i32).checked_rem_euclid(-1i32).unwrap() as u32;
91+
(1i32).checked_rem_euclid(-1i32).unwrap() as u64;
92+
(1i64).checked_rem_euclid(-1i64).unwrap() as u64;
93+
(1i64).checked_rem_euclid(-1i64).unwrap() as u128;
94+
(1isize).checked_rem_euclid(-1isize).unwrap() as usize;
5095
}

0 commit comments

Comments
 (0)