Skip to content

Commit 432ebd5

Browse files
committed
Removed test for unhandled case in function_item_references lint
Removed test for the unhandled case of calls to `fn f<T>(x: &T)` where `x` is a function reference and is formatted as a pointer in `f`. This compiles since `&T` implements `Pointer`, but is unlikely to occur in practice. Also tweaked the lint's wording and modified tests accordingly.
1 parent 511fe04 commit 432ebd5

File tree

3 files changed

+86
-89
lines changed

3 files changed

+86
-89
lines changed

compiler/rustc_mir/src/transform/function_references.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<'a, 'tcx> FunctionItemRefChecker<'a, 'tcx> {
164164
let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" };
165165
self.tcx.struct_span_lint_hir(FUNCTION_ITEM_REFERENCES, lint_root, span, |lint| {
166166
lint.build(&format!(
167-
"cast `{}` with `as {}{}fn({}{}){}` to use it as a pointer",
167+
"cast `{}` with `as {}{}fn({}{}){}` to obtain a function pointer",
168168
ident,
169169
unsafety,
170170
abi,

src/test/ui/lint/function-references.rs

+29-32
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn baz(x: u32, y: u32) -> u32 { x + y }
1010
unsafe fn unsafe_fn() { }
1111
extern "C" fn c_fn() { }
1212
unsafe extern "C" fn unsafe_c_fn() { }
13-
unsafe extern fn variadic_fn(_x: u32, _args: ...) { }
13+
unsafe extern fn variadic(_x: u32, _args: ...) { }
1414

1515
//function references passed to these functions should never lint
1616
fn call_fn(f: &dyn Fn(u32) -> u32, x: u32) { f(x); }
@@ -20,7 +20,6 @@ fn parameterized_call_fn<F: Fn(u32) -> u32>(f: &F, x: u32) { f(x); }
2020
fn print_ptr<F: Pointer>(f: F) { println!("{:p}", f); }
2121
fn bound_by_ptr_trait<F: Pointer>(_f: F) { }
2222
fn bound_by_ptr_trait_tuple<F: Pointer, G: Pointer>(_t: (F, G)) { }
23-
fn implicit_ptr_trait<F>(f: &F) { println!("{:p}", f); }
2423

2524
fn main() {
2625
//`let` bindings with function references shouldn't lint
@@ -56,47 +55,47 @@ fn main() {
5655

5756
//potential ways to incorrectly try printing function pointers
5857
println!("{:p}", &foo);
59-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
58+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
6059
print!("{:p}", &foo);
61-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
60+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
6261
format!("{:p}", &foo);
63-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
62+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
6463

6564
println!("{:p}", &foo as *const _);
66-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
65+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
6766
println!("{:p}", zst_ref);
68-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
67+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
6968
println!("{:p}", cast_zst_ptr);
70-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
69+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
7170
println!("{:p}", coerced_zst_ptr);
72-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
71+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
7372

7473
println!("{:p}", &fn_item);
75-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
74+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
7675
println!("{:p}", indirect_ref);
77-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
76+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
7877

7978
println!("{:p}", &nop);
80-
//~^ WARNING cast `nop` with `as fn()` to use it as a pointer
79+
//~^ WARNING cast `nop` with `as fn()` to obtain a function pointer
8180
println!("{:p}", &bar);
82-
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
81+
//~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
8382
println!("{:p}", &baz);
84-
//~^ WARNING cast `baz` with `as fn(_, _) -> _` to use it as a pointer
83+
//~^ WARNING cast `baz` with `as fn(_, _) -> _` to obtain a function pointer
8584
println!("{:p}", &unsafe_fn);
86-
//~^ WARNING cast `unsafe_fn` with `as unsafe fn()` to use it as a pointer
85+
//~^ WARNING cast `unsafe_fn` with `as unsafe fn()` to obtain a function pointer
8786
println!("{:p}", &c_fn);
88-
//~^ WARNING cast `c_fn` with `as extern "C" fn()` to use it as a pointer
87+
//~^ WARNING cast `c_fn` with `as extern "C" fn()` to obtain a function pointer
8988
println!("{:p}", &unsafe_c_fn);
90-
//~^ WARNING cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to use it as a pointer
91-
println!("{:p}", &variadic_fn);
92-
//~^ WARNING cast `variadic_fn` with `as unsafe extern "C" fn(_, ...)` to use it as a pointer
89+
//~^ WARNING cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to obtain a function pointer
90+
println!("{:p}", &variadic);
91+
//~^ WARNING cast `variadic` with `as unsafe extern "C" fn(_, ...)` to obtain a function pointer
9392
println!("{:p}", &std::env::var::<String>);
94-
//~^ WARNING cast `var` with `as fn(_) -> _` to use it as a pointer
93+
//~^ WARNING cast `var` with `as fn(_) -> _` to obtain a function pointer
9594

9695
println!("{:p} {:p} {:p}", &nop, &foo, &bar);
97-
//~^ WARNING cast `nop` with `as fn()` to use it as a pointer
98-
//~^^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
99-
//~^^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
96+
//~^ WARNING cast `nop` with `as fn()` to obtain a function pointer
97+
//~^^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
98+
//~^^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
10099

101100
//using a function reference to call a function shouldn't lint
102101
(&bar)(1);
@@ -109,10 +108,10 @@ fn main() {
109108
unsafe {
110109
//potential ways to incorrectly try transmuting function pointers
111110
std::mem::transmute::<_, usize>(&foo);
112-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
111+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
113112
std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
114-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
115-
//~^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
113+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
114+
//~^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
116115

117116
//the correct way to transmute function pointers
118117
std::mem::transmute::<_, usize>(foo as fn() -> u32);
@@ -121,14 +120,12 @@ fn main() {
121120

122121
//function references as arguments required to be bound by std::fmt::Pointer should lint
123122
print_ptr(&bar);
124-
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
123+
//~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
125124
bound_by_ptr_trait(&bar);
126-
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
125+
//~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
127126
bound_by_ptr_trait_tuple((&foo, &bar));
128-
//~^ WARNING cast `foo` with `as fn() -> _` to use it as a pointer
129-
//~^^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
130-
implicit_ptr_trait(&bar);
131-
//~^ WARNING cast `bar` with `as fn(_) -> _` to use it as a pointer
127+
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
128+
//~^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
132129

133130
//correct ways to pass function pointers as arguments bound by std::fmt::Pointer
134131
print_ptr(bar as fn(u32) -> u32);

src/test/ui/lint/function-references.stderr

+56-56
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
2-
--> $DIR/function-references.rs:58:22
1+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
2+
--> $DIR/function-references.rs:57:22
33
|
44
LL | println!("{:p}", &foo);
55
| ^^^^
@@ -10,158 +10,158 @@ note: the lint level is defined here
1010
LL | #![warn(function_item_references)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
14-
--> $DIR/function-references.rs:60:20
13+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
14+
--> $DIR/function-references.rs:59:20
1515
|
1616
LL | print!("{:p}", &foo);
1717
| ^^^^
1818

19-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
20-
--> $DIR/function-references.rs:62:21
19+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
20+
--> $DIR/function-references.rs:61:21
2121
|
2222
LL | format!("{:p}", &foo);
2323
| ^^^^
2424

25-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
26-
--> $DIR/function-references.rs:65:22
25+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
26+
--> $DIR/function-references.rs:64:22
2727
|
2828
LL | println!("{:p}", &foo as *const _);
2929
| ^^^^^^^^^^^^^^^^
3030

31-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
32-
--> $DIR/function-references.rs:67:22
31+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
32+
--> $DIR/function-references.rs:66:22
3333
|
3434
LL | println!("{:p}", zst_ref);
3535
| ^^^^^^^
3636

37-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
38-
--> $DIR/function-references.rs:69:22
37+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
38+
--> $DIR/function-references.rs:68:22
3939
|
4040
LL | println!("{:p}", cast_zst_ptr);
4141
| ^^^^^^^^^^^^
4242

43-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
44-
--> $DIR/function-references.rs:71:22
43+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
44+
--> $DIR/function-references.rs:70:22
4545
|
4646
LL | println!("{:p}", coerced_zst_ptr);
4747
| ^^^^^^^^^^^^^^^
4848

49-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
50-
--> $DIR/function-references.rs:74:22
49+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
50+
--> $DIR/function-references.rs:73:22
5151
|
5252
LL | println!("{:p}", &fn_item);
5353
| ^^^^^^^^
5454

55-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
56-
--> $DIR/function-references.rs:76:22
55+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
56+
--> $DIR/function-references.rs:75:22
5757
|
5858
LL | println!("{:p}", indirect_ref);
5959
| ^^^^^^^^^^^^
6060

61-
warning: cast `nop` with `as fn()` to use it as a pointer
62-
--> $DIR/function-references.rs:79:22
61+
warning: cast `nop` with `as fn()` to obtain a function pointer
62+
--> $DIR/function-references.rs:78:22
6363
|
6464
LL | println!("{:p}", &nop);
6565
| ^^^^
6666

67-
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
68-
--> $DIR/function-references.rs:81:22
67+
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
68+
--> $DIR/function-references.rs:80:22
6969
|
7070
LL | println!("{:p}", &bar);
7171
| ^^^^
7272

73-
warning: cast `baz` with `as fn(_, _) -> _` to use it as a pointer
74-
--> $DIR/function-references.rs:83:22
73+
warning: cast `baz` with `as fn(_, _) -> _` to obtain a function pointer
74+
--> $DIR/function-references.rs:82:22
7575
|
7676
LL | println!("{:p}", &baz);
7777
| ^^^^
7878

79-
warning: cast `unsafe_fn` with `as unsafe fn()` to use it as a pointer
80-
--> $DIR/function-references.rs:85:22
79+
warning: cast `unsafe_fn` with `as unsafe fn()` to obtain a function pointer
80+
--> $DIR/function-references.rs:84:22
8181
|
8282
LL | println!("{:p}", &unsafe_fn);
8383
| ^^^^^^^^^^
8484

85-
warning: cast `c_fn` with `as extern "C" fn()` to use it as a pointer
86-
--> $DIR/function-references.rs:87:22
85+
warning: cast `c_fn` with `as extern "C" fn()` to obtain a function pointer
86+
--> $DIR/function-references.rs:86:22
8787
|
8888
LL | println!("{:p}", &c_fn);
8989
| ^^^^^
9090

91-
warning: cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to use it as a pointer
92-
--> $DIR/function-references.rs:89:22
91+
warning: cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to obtain a function pointer
92+
--> $DIR/function-references.rs:88:22
9393
|
9494
LL | println!("{:p}", &unsafe_c_fn);
9595
| ^^^^^^^^^^^^
9696

97-
warning: cast `variadic_fn` with `as unsafe extern "C" fn(_, ...)` to use it as a pointer
98-
--> $DIR/function-references.rs:91:22
97+
warning: cast `variadic` with `as unsafe extern "C" fn(_, ...)` to obtain a function pointer
98+
--> $DIR/function-references.rs:90:22
9999
|
100-
LL | println!("{:p}", &variadic_fn);
101-
| ^^^^^^^^^^^^
100+
LL | println!("{:p}", &variadic);
101+
| ^^^^^^^^^
102102

103-
warning: cast `var` with `as fn(_) -> _` to use it as a pointer
104-
--> $DIR/function-references.rs:93:22
103+
warning: cast `var` with `as fn(_) -> _` to obtain a function pointer
104+
--> $DIR/function-references.rs:92:22
105105
|
106106
LL | println!("{:p}", &std::env::var::<String>);
107107
| ^^^^^^^^^^^^^^^^^^^^^^^^
108108

109-
warning: cast `nop` with `as fn()` to use it as a pointer
110-
--> $DIR/function-references.rs:96:32
109+
warning: cast `nop` with `as fn()` to obtain a function pointer
110+
--> $DIR/function-references.rs:95:32
111111
|
112112
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
113113
| ^^^^
114114

115-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
116-
--> $DIR/function-references.rs:96:38
115+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
116+
--> $DIR/function-references.rs:95:38
117117
|
118118
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
119119
| ^^^^
120120

121-
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
122-
--> $DIR/function-references.rs:96:44
121+
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
122+
--> $DIR/function-references.rs:95:44
123123
|
124124
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
125125
| ^^^^
126126

127-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
128-
--> $DIR/function-references.rs:111:41
127+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
128+
--> $DIR/function-references.rs:110:41
129129
|
130130
LL | std::mem::transmute::<_, usize>(&foo);
131131
| ^^^^
132132

133-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
134-
--> $DIR/function-references.rs:113:50
133+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
134+
--> $DIR/function-references.rs:112:50
135135
|
136136
LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
137137
| ^^^^^^^^^^^^
138138

139-
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
140-
--> $DIR/function-references.rs:113:50
139+
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
140+
--> $DIR/function-references.rs:112:50
141141
|
142142
LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
143143
| ^^^^^^^^^^^^
144144

145-
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
146-
--> $DIR/function-references.rs:123:15
145+
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
146+
--> $DIR/function-references.rs:122:15
147147
|
148148
LL | print_ptr(&bar);
149149
| ^^^^
150150

151-
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
152-
--> $DIR/function-references.rs:125:24
151+
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
152+
--> $DIR/function-references.rs:124:24
153153
|
154154
LL | bound_by_ptr_trait(&bar);
155155
| ^^^^
156156

157-
warning: cast `bar` with `as fn(_) -> _` to use it as a pointer
158-
--> $DIR/function-references.rs:127:30
157+
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
158+
--> $DIR/function-references.rs:126:30
159159
|
160160
LL | bound_by_ptr_trait_tuple((&foo, &bar));
161161
| ^^^^^^^^^^^^
162162

163-
warning: cast `foo` with `as fn() -> _` to use it as a pointer
164-
--> $DIR/function-references.rs:127:30
163+
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
164+
--> $DIR/function-references.rs:126:30
165165
|
166166
LL | bound_by_ptr_trait_tuple((&foo, &bar));
167167
| ^^^^^^^^^^^^

0 commit comments

Comments
 (0)