Skip to content

Commit 5ca7f4a

Browse files
committed
Fix large_stack_arrays triggering for static/constants
1 parent db1bda3 commit 5ca7f4a

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

clippy_lints/src/large_stack_arrays.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ declare_clippy_lint! {
3030
pub struct LargeStackArrays {
3131
maximum_allowed_size: u64,
3232
prev_vec_macro_callsite: Option<Span>,
33+
is_in_const_item: bool,
3334
}
3435

3536
impl LargeStackArrays {
3637
pub fn new(conf: &'static Conf) -> Self {
3738
Self {
3839
maximum_allowed_size: conf.array_size_threshold,
3940
prev_vec_macro_callsite: None,
41+
is_in_const_item: false,
4042
}
4143
}
4244

@@ -60,8 +62,21 @@ impl LargeStackArrays {
6062
impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
6163

6264
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
65+
fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
66+
if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
67+
self.is_in_const_item = true;
68+
}
69+
}
70+
71+
fn check_item_post(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
72+
if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
73+
self.is_in_const_item = false;
74+
}
75+
}
76+
6377
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
64-
if let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
78+
if !self.is_in_const_item
79+
&& let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
6580
&& !self.is_from_vec_macro(cx, expr.span)
6681
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
6782
&& let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()

tests/ui-toml/array_size_threshold/array_size_threshold.stderr

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,15 @@ LL | const ABOVE: [u8; 11] = [0; 11];
99
= note: `-D clippy::large-const-arrays` implied by `-D warnings`
1010
= help: to override `-D warnings` add `#[allow(clippy::large_const_arrays)]`
1111

12-
error: allocating a local array larger than 10 bytes
13-
--> tests/ui-toml/array_size_threshold/array_size_threshold.rs:4:25
14-
|
15-
LL | const ABOVE: [u8; 11] = [0; 11];
16-
| ^^^^^^^
17-
|
18-
= help: consider allocating on the heap with `vec![0; 11].into_boxed_slice()`
19-
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
20-
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
21-
2212
error: allocating a local array larger than 10 bytes
2313
--> tests/ui-toml/array_size_threshold/array_size_threshold.rs:8:17
2414
|
2515
LL | let above = [0u8; 11];
2616
| ^^^^^^^^^
2717
|
2818
= help: consider allocating on the heap with `vec![0u8; 11].into_boxed_slice()`
19+
= note: `-D clippy::large-stack-arrays` implied by `-D warnings`
20+
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
2921

30-
error: aborting due to 3 previous errors
22+
error: aborting due to 2 previous errors
3123

tests/ui/large_stack_arrays.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,5 @@ fn issue_12586() {
103103
let y = vec![proc_macros::make_it_big!([x; 10])];
104104
let y = vec![create_then_move![x; 5]; 5];
105105
}
106+
107+
const STATIC_PROMOTED_LARGE_ARRAY: &[u8; 512001] = &[0; 512001];

0 commit comments

Comments
 (0)