2 #![feature(c_variadic)]
3 #![warn(function_item_references)]
5 use std::fmt::Formatter;
9 fn bar(x: u32) -> u32 { x }
10 fn baz(x: u32, y: u32) -> u32 { x + y }
11 unsafe fn unsafe_fn() { }
12 extern "C" fn c_fn() { }
13 unsafe extern "C" fn unsafe_c_fn() { }
14 unsafe extern fn variadic(_x: u32, _args: ...) { }
15 fn take_generic_ref<'a, T>(_x: &'a T) { }
16 fn take_generic_array<T, const N: usize>(_x: [T; N]) { }
17 fn multiple_generic<T, U>(_x: T, _y: U) { }
18 fn multiple_generic_arrays<T, U, const N: usize, const M: usize>(_x: [T; N], _y: [U; M]) { }
20 //function references passed to these functions should never lint
21 fn call_fn(f: &dyn Fn(u32) -> u32, x: u32) { f(x); }
22 fn parameterized_call_fn<F: Fn(u32) -> u32>(f: &F, x: u32) { f(x); }
24 //function references passed to these functions should lint
25 fn print_ptr<F: Pointer>(f: F) { println!("{:p}", f); }
26 fn bound_by_ptr_trait<F: Pointer>(_f: F) { }
27 fn bound_by_ptr_trait_tuple<F: Pointer, G: Pointer>(_t: (F, G)) { }
28 fn implicit_ptr_trait<F>(f: &F) { println!("{:p}", f); }
30 //case found in tinyvec that triggered a compiler error in an earlier version of the lint checker
33 fn assoc_item(&self) -> Self::Item;
35 fn _format_assoc_item<T: HasItem>(data: T, f: &mut Formatter) -> std::fmt::Result
36 where T::Item: Pointer {
37 //when the arg type bound by `Pointer` is an associated type, we shouldn't attempt to normalize
38 Pointer::fmt(&data.assoc_item(), f)
41 //simple test to make sure that calls to `Pointer::fmt` aren't double counted
42 fn _call_pointer_fmt(f: &mut Formatter) -> std::fmt::Result {
44 Pointer::fmt(&zst_ref, f)
45 //~^ WARNING taking a reference to a function item does not give a function pointer
49 //`let` bindings with function references shouldn't lint
55 let indirect_ref = &fn_item;
57 let _mut_zst_ref = &mut foo;
58 let mut mut_fn_item = foo;
59 let _mut_indirect_ref = &mut mut_fn_item;
61 let cast_zst_ptr = &foo as *const _;
62 let coerced_zst_ptr: *const _ = &foo;
64 let _mut_cast_zst_ptr = &mut foo as *mut _;
65 let _mut_coerced_zst_ptr: *mut _ = &mut foo;
67 let _cast_zst_ref = &foo as &dyn Fn() -> u32;
68 let _coerced_zst_ref: &dyn Fn() -> u32 = &foo;
70 let _mut_cast_zst_ref = &mut foo as &mut dyn Fn() -> u32;
71 let _mut_coerced_zst_ref: &mut dyn Fn() -> u32 = &mut foo;
73 //the suggested way to cast to a function pointer
74 let fn_ptr = foo as fn() -> u32;
76 //correct ways to print function pointers
77 println!("{:p}", foo as fn() -> u32);
78 println!("{:p}", fn_ptr);
80 //potential ways to incorrectly try printing function pointers
81 println!("{:p}", &foo);
82 //~^ WARNING taking a reference to a function item does not give a function pointer
84 //~^ WARNING taking a reference to a function item does not give a function pointer
85 format!("{:p}", &foo);
86 //~^ WARNING taking a reference to a function item does not give a function pointer
88 println!("{:p}", &foo as *const _);
89 //~^ WARNING taking a reference to a function item does not give a function pointer
90 println!("{:p}", zst_ref);
91 //~^ WARNING taking a reference to a function item does not give a function pointer
92 println!("{:p}", cast_zst_ptr);
93 //~^ WARNING taking a reference to a function item does not give a function pointer
94 println!("{:p}", coerced_zst_ptr);
95 //~^ WARNING taking a reference to a function item does not give a function pointer
97 println!("{:p}", &fn_item);
98 //~^ WARNING taking a reference to a function item does not give a function pointer
99 println!("{:p}", indirect_ref);
100 //~^ WARNING taking a reference to a function item does not give a function pointer
102 println!("{:p}", &nop);
103 //~^ WARNING taking a reference to a function item does not give a function pointer
104 println!("{:p}", &bar);
105 //~^ WARNING taking a reference to a function item does not give a function pointer
106 println!("{:p}", &baz);
107 //~^ WARNING taking a reference to a function item does not give a function pointer
108 println!("{:p}", &unsafe_fn);
109 //~^ WARNING taking a reference to a function item does not give a function pointer
110 println!("{:p}", &c_fn);
111 //~^ WARNING taking a reference to a function item does not give a function pointer
112 println!("{:p}", &unsafe_c_fn);
113 //~^ WARNING taking a reference to a function item does not give a function pointer
114 println!("{:p}", &variadic);
115 //~^ WARNING taking a reference to a function item does not give a function pointer
116 println!("{:p}", &take_generic_ref::<u32>);
117 //~^ WARNING taking a reference to a function item does not give a function pointer
118 println!("{:p}", &take_generic_array::<u32, 4>);
119 //~^ WARNING taking a reference to a function item does not give a function pointer
120 println!("{:p}", &multiple_generic::<u32, f32>);
121 //~^ WARNING taking a reference to a function item does not give a function pointer
122 println!("{:p}", &multiple_generic_arrays::<u32, f32, 4, 8>);
123 //~^ WARNING taking a reference to a function item does not give a function pointer
124 println!("{:p}", &std::env::var::<String>);
125 //~^ WARNING taking a reference to a function item does not give a function pointer
127 println!("{:p} {:p} {:p}", &nop, &foo, &bar);
128 //~^ WARNING taking a reference to a function item does not give a function pointer
129 //~^^ WARNING taking a reference to a function item does not give a function pointer
130 //~^^^ WARNING taking a reference to a function item does not give a function pointer
132 //using a function reference to call a function shouldn't lint
135 //passing a function reference to an arbitrary function shouldn't lint
137 parameterized_call_fn(&bar, 1);
138 std::mem::size_of_val(&foo);
141 //potential ways to incorrectly try transmuting function pointers
142 std::mem::transmute::<_, usize>(&foo);
143 //~^ WARNING taking a reference to a function item does not give a function pointer
144 std::mem::transmute::<_, (usize, usize)>((&foo, &bar));
145 //~^ WARNING taking a reference to a function item does not give a function pointer
146 //~^^ WARNING taking a reference to a function item does not give a function pointer
147 std::mem::transmute::<_, usize>(&take_generic_ref::<u32>);
148 //~^ WARNING taking a reference to a function item does not give a function pointer
150 //the correct way to transmute function pointers
151 std::mem::transmute::<_, usize>(foo as fn() -> u32);
152 std::mem::transmute::<_, (usize, usize)>((foo as fn() -> u32, bar as fn(u32) -> u32));
155 //function references as arguments required to be bound by std::fmt::Pointer should lint
157 //~^ WARNING taking a reference to a function item does not give a function pointer
158 bound_by_ptr_trait(&bar);
159 //~^ WARNING taking a reference to a function item does not give a function pointer
160 bound_by_ptr_trait_tuple((&foo, &bar));
161 //~^ WARNING taking a reference to a function item does not give a function pointer
162 //~^^ WARNING taking a reference to a function item does not give a function pointer
163 implicit_ptr_trait(&bar); // ignore
165 //correct ways to pass function pointers as arguments bound by std::fmt::Pointer
166 print_ptr(bar as fn(u32) -> u32);
167 bound_by_ptr_trait(bar as fn(u32) -> u32);
168 bound_by_ptr_trait_tuple((foo as fn() -> u32, bar as fn(u32) -> u32));