Skip to content

Commit c791c64

Browse files
committed
Added suggestion to function_item_references lint and fixed warning message
Also updated tests accordingly and tweaked some wording in the lint declaration.
1 parent 935fc36 commit c791c64

File tree

4 files changed

+102
-95
lines changed

4 files changed

+102
-95
lines changed

compiler/rustc_mir/src/transform/function_item_references.rs

+17-10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_errors::Applicability;
12
use rustc_hir::def_id::DefId;
23
use rustc_middle::mir::visit::Visitor;
34
use rustc_middle::mir::*;
@@ -183,16 +184,22 @@ impl<'a, 'tcx> FunctionItemRefChecker<'a, 'tcx> {
183184
let variadic = if fn_sig.c_variadic() { ", ..." } else { "" };
184185
let ret = if fn_sig.output().skip_binder().is_unit() { "" } else { " -> _" };
185186
self.tcx.struct_span_lint_hir(FUNCTION_ITEM_REFERENCES, lint_root, span, |lint| {
186-
lint.build(&format!(
187-
"cast `{}` with `as {}{}fn({}{}){}` to obtain a function pointer",
188-
ident,
189-
unsafety,
190-
abi,
191-
vec!["_"; num_args].join(", "),
192-
variadic,
193-
ret,
194-
))
195-
.emit();
187+
lint.build("taking a reference to a function item does not give a function pointer")
188+
.span_suggestion(
189+
span,
190+
&format!("cast `{}` to obtain a function pointer", ident),
191+
format!(
192+
"{} as {}{}fn({}{}){}",
193+
ident,
194+
unsafety,
195+
abi,
196+
vec!["_"; num_args].join(", "),
197+
variadic,
198+
ret,
199+
),
200+
Applicability::Unspecified,
201+
)
202+
.emit();
196203
});
197204
}
198205
}

compiler/rustc_session/src/lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2674,7 +2674,7 @@ declare_lint! {
26742674
/// arguments bound by [`fmt::Pointer`] or transmuted.
26752675
pub FUNCTION_ITEM_REFERENCES,
26762676
Warn,
2677-
"suggest casting functions to pointers when attempting to take references",
2677+
"suggest casting to a function pointer when attempting to take references to function items",
26782678
}
26792679

26802680
declare_lint! {

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

+28-28
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn _format_assoc_item<T: HasItem>(data: T, f: &mut Formatter) -> std::fmt::Resul
3838
fn _call_pointer_fmt(f: &mut Formatter) -> std::fmt::Result {
3939
let zst_ref = &foo;
4040
Pointer::fmt(&zst_ref, f)
41-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
41+
//~^ WARNING taking a reference to a function item does not give a function pointer
4242
}
4343

4444
fn main() {
@@ -75,47 +75,47 @@ fn main() {
7575

7676
//potential ways to incorrectly try printing function pointers
7777
println!("{:p}", &foo);
78-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
78+
//~^ WARNING taking a reference to a function item does not give a function pointer
7979
print!("{:p}", &foo);
80-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
80+
//~^ WARNING taking a reference to a function item does not give a function pointer
8181
format!("{:p}", &foo);
82-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
82+
//~^ WARNING taking a reference to a function item does not give a function pointer
8383

8484
println!("{:p}", &foo as *const _);
85-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
85+
//~^ WARNING taking a reference to a function item does not give a function pointer
8686
println!("{:p}", zst_ref);
87-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
87+
//~^ WARNING taking a reference to a function item does not give a function pointer
8888
println!("{:p}", cast_zst_ptr);
89-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
89+
//~^ WARNING taking a reference to a function item does not give a function pointer
9090
println!("{:p}", coerced_zst_ptr);
91-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
91+
//~^ WARNING taking a reference to a function item does not give a function pointer
9292

9393
println!("{:p}", &fn_item);
94-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
94+
//~^ WARNING taking a reference to a function item does not give a function pointer
9595
println!("{:p}", indirect_ref);
96-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
96+
//~^ WARNING taking a reference to a function item does not give a function pointer
9797

9898
println!("{:p}", &nop);
99-
//~^ WARNING cast `nop` with `as fn()` to obtain a function pointer
99+
//~^ WARNING taking a reference to a function item does not give a function pointer
100100
println!("{:p}", &bar);
101-
//~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
101+
//~^ WARNING taking a reference to a function item does not give a function pointer
102102
println!("{:p}", &baz);
103-
//~^ WARNING cast `baz` with `as fn(_, _) -> _` to obtain a function pointer
103+
//~^ WARNING taking a reference to a function item does not give a function pointer
104104
println!("{:p}", &unsafe_fn);
105-
//~^ WARNING cast `unsafe_fn` with `as unsafe fn()` to obtain a function pointer
105+
//~^ WARNING taking a reference to a function item does not give a function pointer
106106
println!("{:p}", &c_fn);
107-
//~^ WARNING cast `c_fn` with `as extern "C" fn()` to obtain a function pointer
107+
//~^ WARNING taking a reference to a function item does not give a function pointer
108108
println!("{:p}", &unsafe_c_fn);
109-
//~^ WARNING cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to obtain a function pointer
109+
//~^ WARNING taking a reference to a function item does not give a function pointer
110110
println!("{:p}", &variadic);
111-
//~^ WARNING cast `variadic` with `as unsafe extern "C" fn(_, ...)` to obtain a function pointer
111+
//~^ WARNING taking a reference to a function item does not give a function pointer
112112
println!("{:p}", &std::env::var::<String>);
113-
//~^ WARNING cast `var` with `as fn(_) -> _` to obtain a function pointer
113+
//~^ WARNING taking a reference to a function item does not give a function pointer
114114

115115
println!("{:p} {:p} {:p}", &nop, &foo, &bar);
116-
//~^ WARNING cast `nop` with `as fn()` to obtain a function pointer
117-
//~^^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
118-
//~^^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
116+
//~^ WARNING taking a reference to a function item does not give a function pointer
117+
//~^^ WARNING taking a reference to a function item does not give a function pointer
118+
//~^^^ WARNING taking a reference to a function item does not give a function pointer
119119

120120
//using a function reference to call a function shouldn't lint
121121
(&bar)(1);
@@ -128,10 +128,10 @@ fn main() {
128128
unsafe {
129129
//potential ways to incorrectly try transmuting function pointers
130130
std::mem::transmute::<_, usize>(&foo);
131-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
131+
//~^ WARNING taking a reference to a function item does not give a function pointer
132132
std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
133-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
134-
//~^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
133+
//~^ WARNING taking a reference to a function item does not give a function pointer
134+
//~^^ WARNING taking a reference to a function item does not give a function pointer
135135

136136
//the correct way to transmute function pointers
137137
std::mem::transmute::<_, usize>(foo as fn() -> u32);
@@ -140,12 +140,12 @@ fn main() {
140140

141141
//function references as arguments required to be bound by std::fmt::Pointer should lint
142142
print_ptr(&bar);
143-
//~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
143+
//~^ WARNING taking a reference to a function item does not give a function pointer
144144
bound_by_ptr_trait(&bar);
145-
//~^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
145+
//~^ WARNING taking a reference to a function item does not give a function pointer
146146
bound_by_ptr_trait_tuple((&foo, &bar));
147-
//~^ WARNING cast `foo` with `as fn() -> _` to obtain a function pointer
148-
//~^^ WARNING cast `bar` with `as fn(_) -> _` to obtain a function pointer
147+
//~^ WARNING taking a reference to a function item does not give a function pointer
148+
//~^^ WARNING taking a reference to a function item does not give a function pointer
149149
implicit_ptr_trait(&bar); // ignore
150150

151151
//correct ways to pass function pointers as arguments bound by std::fmt::Pointer
Original file line numberDiff line numberDiff line change
@@ -1,176 +1,176 @@
1-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
1+
warning: taking a reference to a function item does not give a function pointer
22
--> $DIR/function-item-references.rs:40:18
33
|
44
LL | Pointer::fmt(&zst_ref, f)
5-
| ^^^^^^^^
5+
| ^^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
66
|
77
note: the lint level is defined here
88
--> $DIR/function-item-references.rs:3:9
99
|
1010
LL | #![warn(function_item_references)]
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
13+
warning: taking a reference to a function item does not give a function pointer
1414
--> $DIR/function-item-references.rs:77:22
1515
|
1616
LL | println!("{:p}", &foo);
17-
| ^^^^
17+
| ^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
1818

19-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
19+
warning: taking a reference to a function item does not give a function pointer
2020
--> $DIR/function-item-references.rs:79:20
2121
|
2222
LL | print!("{:p}", &foo);
23-
| ^^^^
23+
| ^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
2424

25-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
25+
warning: taking a reference to a function item does not give a function pointer
2626
--> $DIR/function-item-references.rs:81:21
2727
|
2828
LL | format!("{:p}", &foo);
29-
| ^^^^
29+
| ^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
3030

31-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
31+
warning: taking a reference to a function item does not give a function pointer
3232
--> $DIR/function-item-references.rs:84:22
3333
|
3434
LL | println!("{:p}", &foo as *const _);
35-
| ^^^^^^^^^^^^^^^^
35+
| ^^^^^^^^^^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
3636

37-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
37+
warning: taking a reference to a function item does not give a function pointer
3838
--> $DIR/function-item-references.rs:86:22
3939
|
4040
LL | println!("{:p}", zst_ref);
41-
| ^^^^^^^
41+
| ^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
4242

43-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
43+
warning: taking a reference to a function item does not give a function pointer
4444
--> $DIR/function-item-references.rs:88:22
4545
|
4646
LL | println!("{:p}", cast_zst_ptr);
47-
| ^^^^^^^^^^^^
47+
| ^^^^^^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
4848

49-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
49+
warning: taking a reference to a function item does not give a function pointer
5050
--> $DIR/function-item-references.rs:90:22
5151
|
5252
LL | println!("{:p}", coerced_zst_ptr);
53-
| ^^^^^^^^^^^^^^^
53+
| ^^^^^^^^^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
5454

55-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
55+
warning: taking a reference to a function item does not give a function pointer
5656
--> $DIR/function-item-references.rs:93:22
5757
|
5858
LL | println!("{:p}", &fn_item);
59-
| ^^^^^^^^
59+
| ^^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
6060

61-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
61+
warning: taking a reference to a function item does not give a function pointer
6262
--> $DIR/function-item-references.rs:95:22
6363
|
6464
LL | println!("{:p}", indirect_ref);
65-
| ^^^^^^^^^^^^
65+
| ^^^^^^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
6666

67-
warning: cast `nop` with `as fn()` to obtain a function pointer
67+
warning: taking a reference to a function item does not give a function pointer
6868
--> $DIR/function-item-references.rs:98:22
6969
|
7070
LL | println!("{:p}", &nop);
71-
| ^^^^
71+
| ^^^^ help: cast `nop` to obtain a function pointer: `nop as fn()`
7272

73-
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
73+
warning: taking a reference to a function item does not give a function pointer
7474
--> $DIR/function-item-references.rs:100:22
7575
|
7676
LL | println!("{:p}", &bar);
77-
| ^^^^
77+
| ^^^^ help: cast `bar` to obtain a function pointer: `bar as fn(_) -> _`
7878

79-
warning: cast `baz` with `as fn(_, _) -> _` to obtain a function pointer
79+
warning: taking a reference to a function item does not give a function pointer
8080
--> $DIR/function-item-references.rs:102:22
8181
|
8282
LL | println!("{:p}", &baz);
83-
| ^^^^
83+
| ^^^^ help: cast `baz` to obtain a function pointer: `baz as fn(_, _) -> _`
8484

85-
warning: cast `unsafe_fn` with `as unsafe fn()` to obtain a function pointer
85+
warning: taking a reference to a function item does not give a function pointer
8686
--> $DIR/function-item-references.rs:104:22
8787
|
8888
LL | println!("{:p}", &unsafe_fn);
89-
| ^^^^^^^^^^
89+
| ^^^^^^^^^^ help: cast `unsafe_fn` to obtain a function pointer: `unsafe_fn as unsafe fn()`
9090

91-
warning: cast `c_fn` with `as extern "C" fn()` to obtain a function pointer
91+
warning: taking a reference to a function item does not give a function pointer
9292
--> $DIR/function-item-references.rs:106:22
9393
|
9494
LL | println!("{:p}", &c_fn);
95-
| ^^^^^
95+
| ^^^^^ help: cast `c_fn` to obtain a function pointer: `c_fn as extern "C" fn()`
9696

97-
warning: cast `unsafe_c_fn` with `as unsafe extern "C" fn()` to obtain a function pointer
97+
warning: taking a reference to a function item does not give a function pointer
9898
--> $DIR/function-item-references.rs:108:22
9999
|
100100
LL | println!("{:p}", &unsafe_c_fn);
101-
| ^^^^^^^^^^^^
101+
| ^^^^^^^^^^^^ help: cast `unsafe_c_fn` to obtain a function pointer: `unsafe_c_fn as unsafe extern "C" fn()`
102102

103-
warning: cast `variadic` with `as unsafe extern "C" fn(_, ...)` to obtain a function pointer
103+
warning: taking a reference to a function item does not give a function pointer
104104
--> $DIR/function-item-references.rs:110:22
105105
|
106106
LL | println!("{:p}", &variadic);
107-
| ^^^^^^^^^
107+
| ^^^^^^^^^ help: cast `variadic` to obtain a function pointer: `variadic as unsafe extern "C" fn(_, ...)`
108108

109-
warning: cast `var` with `as fn(_) -> _` to obtain a function pointer
109+
warning: taking a reference to a function item does not give a function pointer
110110
--> $DIR/function-item-references.rs:112:22
111111
|
112112
LL | println!("{:p}", &std::env::var::<String>);
113-
| ^^^^^^^^^^^^^^^^^^^^^^^^
113+
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: cast `var` to obtain a function pointer: `var as fn(_) -> _`
114114

115-
warning: cast `nop` with `as fn()` to obtain a function pointer
115+
warning: taking a reference to a function item does not give a function pointer
116116
--> $DIR/function-item-references.rs:115:32
117117
|
118118
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
119-
| ^^^^
119+
| ^^^^ help: cast `nop` to obtain a function pointer: `nop as fn()`
120120

121-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
121+
warning: taking a reference to a function item does not give a function pointer
122122
--> $DIR/function-item-references.rs:115:38
123123
|
124124
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
125-
| ^^^^
125+
| ^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
126126

127-
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
127+
warning: taking a reference to a function item does not give a function pointer
128128
--> $DIR/function-item-references.rs:115:44
129129
|
130130
LL | println!("{:p} {:p} {:p}", &nop, &foo, &bar);
131-
| ^^^^
131+
| ^^^^ help: cast `bar` to obtain a function pointer: `bar as fn(_) -> _`
132132

133-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
133+
warning: taking a reference to a function item does not give a function pointer
134134
--> $DIR/function-item-references.rs:130:41
135135
|
136136
LL | std::mem::transmute::<_, usize>(&foo);
137-
| ^^^^
137+
| ^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
138138

139-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
139+
warning: taking a reference to a function item does not give a function pointer
140140
--> $DIR/function-item-references.rs:132:50
141141
|
142142
LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
143-
| ^^^^^^^^^^^^
143+
| ^^^^^^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
144144

145-
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
145+
warning: taking a reference to a function item does not give a function pointer
146146
--> $DIR/function-item-references.rs:132:50
147147
|
148148
LL | std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
149-
| ^^^^^^^^^^^^
149+
| ^^^^^^^^^^^^ help: cast `bar` to obtain a function pointer: `bar as fn(_) -> _`
150150

151-
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
151+
warning: taking a reference to a function item does not give a function pointer
152152
--> $DIR/function-item-references.rs:142:15
153153
|
154154
LL | print_ptr(&bar);
155-
| ^^^^
155+
| ^^^^ help: cast `bar` to obtain a function pointer: `bar as fn(_) -> _`
156156

157-
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
157+
warning: taking a reference to a function item does not give a function pointer
158158
--> $DIR/function-item-references.rs:144:24
159159
|
160160
LL | bound_by_ptr_trait(&bar);
161-
| ^^^^
161+
| ^^^^ help: cast `bar` to obtain a function pointer: `bar as fn(_) -> _`
162162

163-
warning: cast `bar` with `as fn(_) -> _` to obtain a function pointer
163+
warning: taking a reference to a function item does not give a function pointer
164164
--> $DIR/function-item-references.rs:146:30
165165
|
166166
LL | bound_by_ptr_trait_tuple((&foo, &bar));
167-
| ^^^^^^^^^^^^
167+
| ^^^^^^^^^^^^ help: cast `bar` to obtain a function pointer: `bar as fn(_) -> _`
168168

169-
warning: cast `foo` with `as fn() -> _` to obtain a function pointer
169+
warning: taking a reference to a function item does not give a function pointer
170170
--> $DIR/function-item-references.rs:146:30
171171
|
172172
LL | bound_by_ptr_trait_tuple((&foo, &bar));
173-
| ^^^^^^^^^^^^
173+
| ^^^^^^^^^^^^ help: cast `foo` to obtain a function pointer: `foo as fn() -> _`
174174

175175
warning: 28 warnings emitted
176176

0 commit comments

Comments
 (0)