Skip to content

Commit 3cf8c0b

Browse files
author
Michael Wright
committed
Fix cast_sign_loss false positive
This checks if the value is a non-negative constant before linting about losing the sign. Because the `constant` function doesn't handle const functions, we check if the value is from a call to a `max_value` function directly. A utility method called `get_def_path` was added to make checking for the function paths easier. Fixes #2728
1 parent 6ce78d1 commit 3cf8c0b

File tree

5 files changed

+70
-47
lines changed

5 files changed

+70
-47
lines changed

clippy_lints/src/types.rs

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::consts::{constant, Constant};
44
use crate::reexport::*;
55
use crate::utils::paths;
66
use crate::utils::{
7-
clip, comparisons, differing_macro_contexts, higher, in_constant, in_macro, int_bits, last_path_segment,
8-
match_def_path, match_path, multispan_sugg, opt_def_id, same_tys, sext, snippet, snippet_opt,
7+
clip, comparisons, differing_macro_contexts, get_def_path, higher, in_constant, in_macro, int_bits,
8+
last_path_segment, match_def_path, match_path, multispan_sugg, opt_def_id, same_tys, sext, snippet, snippet_opt,
99
snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext,
1010
AbsolutePathBuffer,
1111
};
@@ -1001,6 +1001,48 @@ enum ArchSuffix {
10011001
None,
10021002
}
10031003

1004+
fn check_loss_of_sign(cx: &LateContext<'_, '_>, expr: &Expr, op: &Expr, cast_from: Ty<'_>, cast_to: Ty<'_>) {
1005+
if !cast_from.is_signed() || cast_to.is_signed() {
1006+
return;
1007+
}
1008+
1009+
// don't lint for positive constants
1010+
let const_val = constant(cx, &cx.tables, op);
1011+
if_chain! {
1012+
if let Some((const_val, _)) = const_val;
1013+
if let Constant::Int(n) = const_val;
1014+
if let ty::Int(ity) = cast_from.sty;
1015+
if sext(cx.tcx, n, ity) >= 0;
1016+
then {
1017+
return
1018+
}
1019+
}
1020+
1021+
// don't lint for max_value const fns
1022+
if_chain! {
1023+
if let ExprKind::Call(callee, args) = &op.node;
1024+
if args.is_empty();
1025+
if let ExprKind::Path(qpath) = &callee.node;
1026+
let def = cx.tables.qpath_def(qpath, callee.hir_id);
1027+
if let Some(def_id) = def.opt_def_id();
1028+
let def_path = get_def_path(cx.tcx, def_id);
1029+
if let &["core", "num", impl_ty, "max_value"] = &def_path[..];
1030+
then {
1031+
if let "<impl i8>" | "<impl i16>" | "<impl i32>" |
1032+
"<impl i64>" | "<impl i128>" = impl_ty {
1033+
return;
1034+
}
1035+
}
1036+
}
1037+
1038+
span_lint(
1039+
cx,
1040+
CAST_SIGN_LOSS,
1041+
expr.span,
1042+
&format!("casting {} to {} may lose the sign of the value", cast_from, cast_to),
1043+
);
1044+
}
1045+
10041046
fn check_truncation_and_wrapping(cx: &LateContext<'_, '_>, expr: &Expr, cast_from: Ty<'_>, cast_to: Ty<'_>) {
10051047
let arch_64_suffix = " on targets with 64-bit wide pointers";
10061048
let arch_32_suffix = " on targets with 32-bit wide pointers";
@@ -1176,14 +1218,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
11761218
}
11771219
},
11781220
(true, true) => {
1179-
if cast_from.is_signed() && !cast_to.is_signed() {
1180-
span_lint(
1181-
cx,
1182-
CAST_SIGN_LOSS,
1183-
expr.span,
1184-
&format!("casting {} to {} may lose the sign of the value", cast_from, cast_to),
1185-
);
1186-
}
1221+
check_loss_of_sign(cx, expr, ex, cast_from, cast_to);
11871222
check_truncation_and_wrapping(cx, expr, cast_from, cast_to);
11881223
check_lossless(cx, expr, ex, cast_from, cast_to);
11891224
},

clippy_lints/src/utils/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ pub fn match_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId, path: &[&str]) ->
130130
apb.names.len() == path.len() && apb.names.into_iter().zip(path.iter()).all(|(a, &b)| *a == *b)
131131
}
132132

133+
pub fn get_def_path(tcx: TyCtxt<'_, '_, '_>, def_id: DefId) -> Vec<&'static str> {
134+
let mut apb = AbsolutePathBuffer { names: vec![] };
135+
tcx.push_item_path(&mut apb, def_id, false);
136+
apb.names.iter().map(|n| n.get()).collect()
137+
}
138+
133139
/// Check if type is struct, enum or union type with given def path.
134140
pub fn match_type(cx: &LateContext<'_, '_>, ty: Ty<'_>, path: &[&str]) -> bool {
135141
match ty.sty {

tests/ui/cast.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ fn main() {
3434
(1u8 + 1u8) as u16;
3535
// Test clippy::cast_sign_loss
3636
1i32 as u32;
37+
-1i32 as u32;
3738
1isize as usize;
39+
-1isize as usize;
40+
0i8 as u8;
41+
i8::max_value() as u8;
42+
i16::max_value() as u16;
43+
i32::max_value() as u32;
44+
i64::max_value() as u64;
45+
i128::max_value() as u128;
3846
// Extra checks for *size
3947
// Test cast_unnecessary
4048
1i32 as i32;

tests/ui/cast.stderr

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,6 @@ error: casting i32 to i8 may truncate the value
7070
LL | 1i32 as i8;
7171
| ^^^^^^^^^^
7272

73-
error: casting i32 to u8 may lose the sign of the value
74-
--> $DIR/cast.rs:22:5
75-
|
76-
LL | 1i32 as u8;
77-
| ^^^^^^^^^^
78-
7973
error: casting i32 to u8 may truncate the value
8074
--> $DIR/cast.rs:22:5
8175
|
@@ -147,36 +141,36 @@ LL | (1u8 + 1u8) as u16;
147141
| ^^^^^^^^^^^^^^^^^^ help: try: `u16::from(1u8 + 1u8)`
148142

149143
error: casting i32 to u32 may lose the sign of the value
150-
--> $DIR/cast.rs:36:5
144+
--> $DIR/cast.rs:37:5
151145
|
152-
LL | 1i32 as u32;
153-
| ^^^^^^^^^^^
146+
LL | -1i32 as u32;
147+
| ^^^^^^^^^^^^
154148

155149
error: casting isize to usize may lose the sign of the value
156-
--> $DIR/cast.rs:37:5
150+
--> $DIR/cast.rs:39:5
157151
|
158-
LL | 1isize as usize;
159-
| ^^^^^^^^^^^^^^^
152+
LL | -1isize as usize;
153+
| ^^^^^^^^^^^^^^^^
160154

161155
error: casting to the same type is unnecessary (`i32` -> `i32`)
162-
--> $DIR/cast.rs:40:5
156+
--> $DIR/cast.rs:48:5
163157
|
164158
LL | 1i32 as i32;
165159
| ^^^^^^^^^^^
166160
|
167161
= note: `-D clippy::unnecessary-cast` implied by `-D warnings`
168162

169163
error: casting to the same type is unnecessary (`f32` -> `f32`)
170-
--> $DIR/cast.rs:41:5
164+
--> $DIR/cast.rs:49:5
171165
|
172166
LL | 1f32 as f32;
173167
| ^^^^^^^^^^^
174168

175169
error: casting to the same type is unnecessary (`bool` -> `bool`)
176-
--> $DIR/cast.rs:42:5
170+
--> $DIR/cast.rs:50:5
177171
|
178172
LL | false as bool;
179173
| ^^^^^^^^^^^^^
180174

181-
error: aborting due to 28 previous errors
175+
error: aborting due to 27 previous errors
182176

tests/ui/cast_size.stderr

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ error: casting isize to i32 may truncate the value on targets with 64-bit wide p
3838
LL | 1isize as i32;
3939
| ^^^^^^^^^^^^^
4040

41-
error: casting isize to u32 may lose the sign of the value
42-
--> $DIR/cast_size.rs:17:5
43-
|
44-
LL | 1isize as u32;
45-
| ^^^^^^^^^^^^^
46-
|
47-
= note: `-D clippy::cast-sign-loss` implied by `-D warnings`
48-
4941
error: casting isize to u32 may truncate the value on targets with 64-bit wide pointers
5042
--> $DIR/cast_size.rs:17:5
5143
|
@@ -78,12 +70,6 @@ error: casting i64 to isize may truncate the value on targets with 32-bit wide p
7870
LL | 1i64 as isize;
7971
| ^^^^^^^^^^^^^
8072

81-
error: casting i64 to usize may lose the sign of the value
82-
--> $DIR/cast_size.rs:22:5
83-
|
84-
LL | 1i64 as usize;
85-
| ^^^^^^^^^^^^^
86-
8773
error: casting i64 to usize may truncate the value on targets with 32-bit wide pointers
8874
--> $DIR/cast_size.rs:22:5
8975
|
@@ -114,11 +100,5 @@ error: casting u32 to isize may wrap around the value on targets with 32-bit wid
114100
LL | 1u32 as isize;
115101
| ^^^^^^^^^^^^^
116102

117-
error: casting i32 to usize may lose the sign of the value
118-
--> $DIR/cast_size.rs:28:5
119-
|
120-
LL | 1i32 as usize;
121-
| ^^^^^^^^^^^^^
122-
123-
error: aborting due to 19 previous errors
103+
error: aborting due to 16 previous errors
124104

0 commit comments

Comments
 (0)