Skip to content

Commit d365742

Browse files
committed
Fix FP in fn_to_numeric_cast_with_truncation
We only want this lint to check casts to numeric, as per the lint title. Rust already has a built-in check for all other casts [here][rust_check]. [rust_check]: https://github.com/rust-lang/rust/blob/5472b0718f286266ab89acdf234c3552de7e973c/src/librustc_typeck/check/cast.rs#L430-L433
1 parent 1e0729d commit d365742

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

clippy_lints/src/types.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CastPass {
10881088
}
10891089

10901090
fn lint_fn_to_numeric_cast(cx: &LateContext<'_, '_>, expr: &Expr, cast_expr: &Expr, cast_from: Ty<'_>, cast_to: Ty<'_>) {
1091+
// We only want to check casts to `ty::Uint` or `ty::Int`
1092+
match cast_to.sty {
1093+
ty::Uint(_) | ty::Int(..) => { /* continue on */ },
1094+
_ => return
1095+
}
10911096
match cast_from.sty {
10921097
ty::FnDef(..) | ty::FnPtr(_) => {
10931098
let from_snippet = snippet(cx, cast_expr.span, "x");

tests/ui/fn_to_numeric_cast.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ fn test_function_to_numeric_cast() {
3131

3232
// Casting to usize is OK and should not warn
3333
let _ = foo as usize;
34+
35+
// Cast `f` (a `FnDef`) to `fn()` should not warn
36+
fn f() {}
37+
let _ = f as fn();
3438
}
3539

3640
fn test_function_var_to_numeric_cast() {

tests/ui/fn_to_numeric_cast.stderr

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -69,75 +69,75 @@ error: casting function pointer `foo` to `u128`
6969
| ^^^^^^^^^^^ help: try: `foo as usize`
7070

7171
error: casting function pointer `abc` to `i8`, which truncates the value
72-
--> $DIR/fn_to_numeric_cast.rs:39:13
72+
--> $DIR/fn_to_numeric_cast.rs:43:13
7373
|
74-
39 | let _ = abc as i8;
74+
43 | let _ = abc as i8;
7575
| ^^^^^^^^^ help: try: `abc as usize`
7676

7777
error: casting function pointer `abc` to `i16`, which truncates the value
78-
--> $DIR/fn_to_numeric_cast.rs:40:13
78+
--> $DIR/fn_to_numeric_cast.rs:44:13
7979
|
80-
40 | let _ = abc as i16;
80+
44 | let _ = abc as i16;
8181
| ^^^^^^^^^^ help: try: `abc as usize`
8282

8383
error: casting function pointer `abc` to `i32`, which truncates the value
84-
--> $DIR/fn_to_numeric_cast.rs:41:13
84+
--> $DIR/fn_to_numeric_cast.rs:45:13
8585
|
86-
41 | let _ = abc as i32;
86+
45 | let _ = abc as i32;
8787
| ^^^^^^^^^^ help: try: `abc as usize`
8888

8989
error: casting function pointer `abc` to `i64`
90-
--> $DIR/fn_to_numeric_cast.rs:42:13
90+
--> $DIR/fn_to_numeric_cast.rs:46:13
9191
|
92-
42 | let _ = abc as i64;
92+
46 | let _ = abc as i64;
9393
| ^^^^^^^^^^ help: try: `abc as usize`
9494

9595
error: casting function pointer `abc` to `i128`
96-
--> $DIR/fn_to_numeric_cast.rs:43:13
96+
--> $DIR/fn_to_numeric_cast.rs:47:13
9797
|
98-
43 | let _ = abc as i128;
98+
47 | let _ = abc as i128;
9999
| ^^^^^^^^^^^ help: try: `abc as usize`
100100

101101
error: casting function pointer `abc` to `isize`
102-
--> $DIR/fn_to_numeric_cast.rs:44:13
102+
--> $DIR/fn_to_numeric_cast.rs:48:13
103103
|
104-
44 | let _ = abc as isize;
104+
48 | let _ = abc as isize;
105105
| ^^^^^^^^^^^^ help: try: `abc as usize`
106106

107107
error: casting function pointer `abc` to `u8`, which truncates the value
108-
--> $DIR/fn_to_numeric_cast.rs:46:13
108+
--> $DIR/fn_to_numeric_cast.rs:50:13
109109
|
110-
46 | let _ = abc as u8;
110+
50 | let _ = abc as u8;
111111
| ^^^^^^^^^ help: try: `abc as usize`
112112

113113
error: casting function pointer `abc` to `u16`, which truncates the value
114-
--> $DIR/fn_to_numeric_cast.rs:47:13
114+
--> $DIR/fn_to_numeric_cast.rs:51:13
115115
|
116-
47 | let _ = abc as u16;
116+
51 | let _ = abc as u16;
117117
| ^^^^^^^^^^ help: try: `abc as usize`
118118

119119
error: casting function pointer `abc` to `u32`, which truncates the value
120-
--> $DIR/fn_to_numeric_cast.rs:48:13
120+
--> $DIR/fn_to_numeric_cast.rs:52:13
121121
|
122-
48 | let _ = abc as u32;
122+
52 | let _ = abc as u32;
123123
| ^^^^^^^^^^ help: try: `abc as usize`
124124

125125
error: casting function pointer `abc` to `u64`
126-
--> $DIR/fn_to_numeric_cast.rs:49:13
126+
--> $DIR/fn_to_numeric_cast.rs:53:13
127127
|
128-
49 | let _ = abc as u64;
128+
53 | let _ = abc as u64;
129129
| ^^^^^^^^^^ help: try: `abc as usize`
130130

131131
error: casting function pointer `abc` to `u128`
132-
--> $DIR/fn_to_numeric_cast.rs:50:13
132+
--> $DIR/fn_to_numeric_cast.rs:54:13
133133
|
134-
50 | let _ = abc as u128;
134+
54 | let _ = abc as u128;
135135
| ^^^^^^^^^^^ help: try: `abc as usize`
136136

137137
error: casting function pointer `f` to `i32`, which truncates the value
138-
--> $DIR/fn_to_numeric_cast.rs:57:5
138+
--> $DIR/fn_to_numeric_cast.rs:61:5
139139
|
140-
57 | f as i32
140+
61 | f as i32
141141
| ^^^^^^^^ help: try: `f as usize`
142142

143143
error: aborting due to 23 previous errors

0 commit comments

Comments
 (0)