]> git.lizzy.rs Git - rust.git/blob - tests/ui/lint/function-item-references.rs
Auto merge of #106884 - clubby789:fieldless-enum-debug, r=michaelwoerister
[rust.git] / tests / ui / lint / function-item-references.rs
1 // check-pass
2 #![feature(c_variadic)]
3 #![warn(function_item_references)]
4 use std::fmt::Pointer;
5 use std::fmt::Formatter;
6
7 fn nop() { }
8 fn foo() -> u32 { 42 }
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]) { }
19
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); }
23
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); }
29
30 //case found in tinyvec that triggered a compiler error in an earlier version of the lint checker
31 trait HasItem {
32   type Item;
33   fn assoc_item(&self) -> Self::Item;
34 }
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)
39 }
40
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 {
43     let zst_ref = &foo;
44     Pointer::fmt(&zst_ref, f)
45     //~^ WARNING taking a reference to a function item does not give a function pointer
46 }
47
48 fn main() {
49     //`let` bindings with function references shouldn't lint
50     let _ = &foo;
51     let _ = &mut foo;
52
53     let zst_ref = &foo;
54     let fn_item = foo;
55     let indirect_ref = &fn_item;
56
57     let _mut_zst_ref = &mut foo;
58     let mut mut_fn_item = foo;
59     let _mut_indirect_ref = &mut mut_fn_item;
60
61     let cast_zst_ptr = &foo as *const _;
62     let coerced_zst_ptr: *const _ = &foo;
63
64     let _mut_cast_zst_ptr = &mut foo as *mut _;
65     let _mut_coerced_zst_ptr: *mut _ = &mut foo;
66
67     let _cast_zst_ref = &foo as &dyn Fn() -> u32;
68     let _coerced_zst_ref: &dyn Fn() -> u32 = &foo;
69
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;
72
73     //the suggested way to cast to a function pointer
74     let fn_ptr = foo as fn() -> u32;
75
76     //correct ways to print function pointers
77     println!("{:p}", foo as fn() -> u32);
78     println!("{:p}", fn_ptr);
79
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
83     print!("{:p}", &foo);
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
87
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
96
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
101
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
126
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
131
132     //using a function reference to call a function shouldn't lint
133     (&bar)(1);
134
135     //passing a function reference to an arbitrary function shouldn't lint
136     call_fn(&bar, 1);
137     parameterized_call_fn(&bar, 1);
138     std::mem::size_of_val(&foo);
139
140     unsafe {
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
149
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));
153     }
154
155     //function references as arguments required to be bound by std::fmt::Pointer should lint
156     print_ptr(&bar);
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
164
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));
169 }