about summary refs log tree commit diff
path: root/clippy_lints/src/large_stack_arrays.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-10-06 11:24:28 +0000
committerbors <bors@rust-lang.org>2024-10-06 11:24:28 +0000
commit2d2e8cc21fe8327f9c4eb73b6d15e934d2f43370 (patch)
tree4ebfc9930693c8bb1e2fa6a40799014a0bc4abf8 /clippy_lints/src/large_stack_arrays.rs
parent753629bb33c9fff9fb693a2d1d4464bd696ccf62 (diff)
parent995eb95000f5c7010727f895d41f6d53fa6f242c (diff)
Auto merge of #13485 - GnomedDev:reduce-large-threshold, r=xFrednet
Reduce default 'large array' threshold

As-is this threshold is `512kb`, but as #9449 points out this is way too high for most people to consider sensible (why would you want to copy `256kb` of data around on the stack or duplicate it via `const`) and didn't get any discussion when originally added. This PR reduces it the threshold to `1kb`, which is higher than the issue says ("a few cpu words") but helps out for actual codebases.

While reducing this, I found that `large_stack_arrays` was triggering for statically promoted arrays in constants/statics, so I also fixed that up as seen in the difference to [array_size_threshold.stderr](https://github.com/rust-lang/rust-clippy/compare/master...GnomedDev:rust-clippy:reduce-large-threshold?expand=1#diff-4c2a2a855d9ff7777f1d385be0c1bede2a3fc8aaab94837cde27a35235233fc7).

Closes #9449.

changelog: [`large_stack_arrays`]: No longer triggers in `static`/`const` context
changelog: [`large_const_arrays`]: Changed the default of [`array-size-threshold`] from `512kb` to `16kb`
Diffstat (limited to 'clippy_lints/src/large_stack_arrays.rs')
-rw-r--r--clippy_lints/src/large_stack_arrays.rs17
1 files changed, 16 insertions, 1 deletions
diff --git a/clippy_lints/src/large_stack_arrays.rs b/clippy_lints/src/large_stack_arrays.rs
index 0f061d6de50..a269bca3210 100644
--- a/clippy_lints/src/large_stack_arrays.rs
+++ b/clippy_lints/src/large_stack_arrays.rs
@@ -30,6 +30,7 @@ declare_clippy_lint! {
 pub struct LargeStackArrays {
     maximum_allowed_size: u64,
     prev_vec_macro_callsite: Option<Span>,
+    is_in_const_item: bool,
 }
 
 impl LargeStackArrays {
@@ -37,6 +38,7 @@ impl LargeStackArrays {
         Self {
             maximum_allowed_size: conf.array_size_threshold,
             prev_vec_macro_callsite: None,
+            is_in_const_item: false,
         }
     }
 
@@ -60,8 +62,21 @@ impl LargeStackArrays {
 impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
 
 impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
+    fn check_item(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
+        if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
+            self.is_in_const_item = true;
+        }
+    }
+
+    fn check_item_post(&mut self, _: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
+        if matches!(item.kind, ItemKind::Static(..) | ItemKind::Const(..)) {
+            self.is_in_const_item = false;
+        }
+    }
+
     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
-        if let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
+        if !self.is_in_const_item
+            && let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
             && !self.is_from_vec_macro(cx, expr.span)
             && let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
             && let ConstKind::Value(_, ty::ValTree::Leaf(element_count)) = cst.kind()