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