]> git.lizzy.rs Git - rust.git/blob - tests/ui/missing_const_for_fn/could_be_const.rs
Fix missing_const_for_fn false positive
[rust.git] / tests / ui / missing_const_for_fn / could_be_const.rs
1 #![warn(clippy::missing_const_for_fn)]
2 #![allow(clippy::let_and_return)]
3
4 use std::mem::transmute;
5
6 struct Game {
7     guess: i32,
8 }
9
10 impl Game {
11     // Could be const
12     pub fn new() -> Self {
13         Self { guess: 42 }
14     }
15 }
16
17 // Could be const
18 fn one() -> i32 {
19     1
20 }
21
22 // Could also be const
23 fn two() -> i32 {
24     let abc = 2;
25     abc
26 }
27
28 // FIXME: This is a false positive in the `is_min_const_fn` function.
29 // At least until the `const_string_new` feature is stabilzed.
30 fn string() -> String {
31     String::new()
32 }
33
34 // Could be const
35 unsafe fn four() -> i32 {
36     4
37 }
38
39 // Could also be const
40 fn generic<T>(t: T) -> T {
41     t
42 }
43
44 // FIXME: Depends on the `const_transmute` and `const_fn` feature gates.
45 // In the future Clippy should be able to suggest this as const, too.
46 fn sub(x: u32) -> usize {
47     unsafe { transmute(&x) }
48 }
49
50 // NOTE: This is currently not yet allowed to be const
51 // Once implemented, Clippy should be able to suggest this as const, too.
52 fn generic_arr<T: Copy>(t: [T; 1]) -> T {
53     t[0]
54 }
55
56 mod with_drop {
57     pub struct A;
58     pub struct B;
59     impl Drop for A {
60         fn drop(&mut self) {}
61     }
62
63     impl B {
64         // This can be const, because `a` is passed by reference
65         pub fn b(self, a: &A) -> B {
66             B
67         }
68     }
69 }
70
71 // Should not be const
72 fn main() {}