Skip to content

Commit cdf26de

Browse files
committed
Auto merge of rust-lang#9466 - lukaslueg:issue9460, r=dswij
Don't lint `large_stack_array` inside static items We now check if the linted `Expr` is inside an `ItemKind::Static`, which can't take the suggested `Box<[...]`. I _think_ this is the correct fix for rust-lang#9460 I removed `if_chain` while I was at it. changelog: Don't lint `large_stack_array` inside static items
2 parents 5e0663e + 6f41a44 commit cdf26de

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

clippy_lints/src/large_stack_arrays.rs

+24-26
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::source::snippet;
3-
use if_chain::if_chain;
4-
use rustc_hir::{Expr, ExprKind};
3+
use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node};
54
use rustc_lint::{LateContext, LateLintPass};
65
use rustc_middle::ty::layout::LayoutOf;
76
use rustc_middle::ty::{self, ConstKind};
@@ -39,29 +38,28 @@ impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
3938

4039
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
4140
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
42-
if_chain! {
43-
if let ExprKind::Repeat(_, _) = expr.kind;
44-
if let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind();
45-
if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();
46-
if let Ok(element_count) = element_count.try_to_machine_usize(cx.tcx);
47-
if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
48-
if self.maximum_allowed_size < element_count * element_size;
49-
then {
50-
span_lint_and_help(
51-
cx,
52-
LARGE_STACK_ARRAYS,
53-
expr.span,
54-
&format!(
55-
"allocating a local array larger than {} bytes",
56-
self.maximum_allowed_size
57-
),
58-
None,
59-
&format!(
60-
"consider allocating on the heap with `vec!{}.into_boxed_slice()`",
61-
snippet(cx, expr.span, "[...]")
62-
),
63-
);
64-
}
65-
}
41+
if let ExprKind::Repeat(_, _) = expr.kind
42+
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
43+
&& let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind()
44+
&& let Ok(element_count) = element_count.try_to_machine_usize(cx.tcx)
45+
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
46+
&& !cx.tcx.hir().parent_iter(expr.hir_id)
47+
.any(|(_, node)| matches!(node, Node::Item(Item { kind: ItemKind::Static(..), .. })))
48+
&& self.maximum_allowed_size < element_count * element_size {
49+
span_lint_and_help(
50+
cx,
51+
LARGE_STACK_ARRAYS,
52+
expr.span,
53+
&format!(
54+
"allocating a local array larger than {} bytes",
55+
self.maximum_allowed_size
56+
),
57+
None,
58+
&format!(
59+
"consider allocating on the heap with `vec!{}.into_boxed_slice()`",
60+
snippet(cx, expr.span, "[...]")
61+
),
62+
);
63+
}
6664
}
6765
}

tests/ui/large_stack_arrays.rs

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ enum E {
1212
T(u32),
1313
}
1414

15+
pub static DOESNOTLINT: [u8; 512_001] = [0; 512_001];
16+
pub static DOESNOTLINT2: [u8; 512_001] = {
17+
let x = 0;
18+
[x; 512_001]
19+
};
20+
1521
fn main() {
1622
let bad = (
1723
[0u32; 20_000_000],

tests/ui/large_stack_arrays.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: allocating a local array larger than 512000 bytes
2-
--> $DIR/large_stack_arrays.rs:17:9
2+
--> $DIR/large_stack_arrays.rs:23:9
33
|
44
LL | [0u32; 20_000_000],
55
| ^^^^^^^^^^^^^^^^^^
@@ -8,23 +8,23 @@ LL | [0u32; 20_000_000],
88
= help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()`
99

1010
error: allocating a local array larger than 512000 bytes
11-
--> $DIR/large_stack_arrays.rs:18:9
11+
--> $DIR/large_stack_arrays.rs:24:9
1212
|
1313
LL | [S { data: [0; 32] }; 5000],
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1515
|
1616
= help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()`
1717

1818
error: allocating a local array larger than 512000 bytes
19-
--> $DIR/large_stack_arrays.rs:19:9
19+
--> $DIR/large_stack_arrays.rs:25:9
2020
|
2121
LL | [Some(""); 20_000_000],
2222
| ^^^^^^^^^^^^^^^^^^^^^^
2323
|
2424
= help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()`
2525

2626
error: allocating a local array larger than 512000 bytes
27-
--> $DIR/large_stack_arrays.rs:20:9
27+
--> $DIR/large_stack_arrays.rs:26:9
2828
|
2929
LL | [E::T(0); 5000],
3030
| ^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)