]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/missing_const_for_fn/could_be_const.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / missing_const_for_fn / could_be_const.rs
CommitLineData
f20569fa
XL
1#![warn(clippy::missing_const_for_fn)]
2#![allow(incomplete_features, clippy::let_and_return)]
3#![feature(const_generics)]
cdc7bbd5 4#![feature(custom_inner_attributes)]
f20569fa
XL
5
6use std::mem::transmute;
7
8struct Game {
9 guess: i32,
10}
11
12impl 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
24fn one() -> i32 {
25 1
26}
27
28// Could also be const
29fn two() -> i32 {
30 let abc = 2;
31 abc
32}
33
34// Could be const (since Rust 1.39)
35fn string() -> String {
36 String::new()
37}
38
39// Could be const
40unsafe fn four() -> i32 {
41 4
42}
43
44// Could also be const
45fn generic<T>(t: T) -> T {
46 t
47}
48
49fn 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.
55fn generic_arr<T: Copy>(t: [T; 1]) -> T {
56 t[0]
57}
58
59mod 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
cdc7bbd5
XL
74mod 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
f20569fa
XL
83// Should not be const
84fn main() {}