|
1 | 1 | use clippy_utils::diagnostics::span_lint_and_help; |
2 | 2 | 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}; |
5 | 4 | use rustc_lint::{LateContext, LateLintPass}; |
6 | 5 | use rustc_middle::ty::layout::LayoutOf; |
7 | 6 | use rustc_middle::ty::{self, ConstKind}; |
@@ -39,29 +38,28 @@ impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]); |
39 | 38 |
|
40 | 39 | impl<'tcx> LateLintPass<'tcx> for LargeStackArrays { |
41 | 40 | 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 | + } |
66 | 64 | } |
67 | 65 | } |
0 commit comments