about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/large_stack_frames.rs
blob: f32368f93975c36cadfbb03fab164246a9b0ce90 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#![allow(unused, incomplete_features)]
#![warn(clippy::large_stack_frames)]
#![feature(unsized_locals)]

use std::hint::black_box;

fn generic<T: Default>() {
    let x = T::default();
    black_box(&x);
}

fn unsized_local() {
    let x: dyn std::fmt::Display = *(Box::new(1) as Box<dyn std::fmt::Display>);
    black_box(&x);
}

struct ArrayDefault<const N: usize>([u8; N]);

impl<const N: usize> Default for ArrayDefault<N> {
    fn default() -> Self {
        Self([0; N])
    }
}

fn many_small_arrays() {
    //~^ ERROR: this function allocates a large amount of stack space
    //~| NOTE: allocating large amounts of stack space can overflow the stack
    let x = [0u8; 500_000];
    let x2 = [0u8; 500_000];
    let x3 = [0u8; 500_000];
    let x4 = [0u8; 500_000];
    let x5 = [0u8; 500_000];
    black_box((&x, &x2, &x3, &x4, &x5));
}

fn large_return_value() -> ArrayDefault<1_000_000> {
    //~^ ERROR: this function allocates a large amount of stack space
    //~| NOTE: allocating large amounts of stack space can overflow the stack
    Default::default()
}

fn large_fn_arg(x: ArrayDefault<1_000_000>) {
    //~^ ERROR: this function allocates a large amount of stack space
    //~| NOTE: allocating large amounts of stack space can overflow the stack
    black_box(&x);
}

fn main() {
    generic::<ArrayDefault<1_000_000>>();
}