]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/missing_const_for_fn/could_be_const.rs
Tweak move error
[rust.git] / src / tools / clippy / tests / ui / missing_const_for_fn / could_be_const.rs
1 #![warn(clippy::missing_const_for_fn)]
2 #![allow(incomplete_features, clippy::let_and_return)]
3 #![feature(custom_inner_attributes)]
4
5 use std::mem::transmute;
6
7 struct Game {
8     guess: i32,
9 }
10
11 impl Game {
12     // Could be const
13     pub fn new() -> Self {
14         Self { guess: 42 }
15     }
16
17     fn const_generic_params<'a, T, const N: usize>(&self, b: &'a [T; N]) -> &'a [T; N] {
18         b
19     }
20 }
21
22 // Could be const
23 fn one() -> i32 {
24     1
25 }
26
27 // Could also be const
28 fn two() -> i32 {
29     let abc = 2;
30     abc
31 }
32
33 // Could be const (since Rust 1.39)
34 fn string() -> String {
35     String::new()
36 }
37
38 // Could be const
39 unsafe fn four() -> i32 {
40     4
41 }
42
43 // Could also be const
44 fn generic<T>(t: T) -> T {
45     t
46 }
47
48 fn sub(x: u32) -> usize {
49     unsafe { transmute(&x) }
50 }
51
52 // NOTE: This is currently not yet allowed to be const
53 // Once implemented, Clippy should be able to suggest this as const, too.
54 fn generic_arr<T: Copy>(t: [T; 1]) -> T {
55     t[0]
56 }
57
58 mod with_drop {
59     pub struct A;
60     pub struct B;
61     impl Drop for A {
62         fn drop(&mut self) {}
63     }
64
65     impl B {
66         // This can be const, because `a` is passed by reference
67         pub fn b(self, a: &A) -> B {
68             B
69         }
70     }
71 }
72
73 mod const_fn_stabilized_before_msrv {
74     #![clippy::msrv = "1.47.0"]
75
76     // This could be const because `u8::is_ascii_digit` is a stable const function in 1.47.
77     fn const_fn_stabilized_before_msrv(byte: u8) {
78         byte.is_ascii_digit();
79     }
80 }
81
82 // Should not be const
83 fn main() {}