mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-24 05:33:27 +00:00
parent
c8c2a234b3
commit
6f41a44c3e
3 changed files with 34 additions and 30 deletions
|
@ -1,7 +1,6 @@
|
||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
use clippy_utils::source::snippet;
|
use clippy_utils::source::snippet;
|
||||||
use if_chain::if_chain;
|
use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node};
|
||||||
use rustc_hir::{Expr, ExprKind};
|
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_middle::ty::layout::LayoutOf;
|
use rustc_middle::ty::layout::LayoutOf;
|
||||||
use rustc_middle::ty::{self, ConstKind};
|
use rustc_middle::ty::{self, ConstKind};
|
||||||
|
@ -39,29 +38,28 @@ impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
|
||||||
|
|
||||||
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
|
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
||||||
if_chain! {
|
if let ExprKind::Repeat(_, _) = expr.kind
|
||||||
if let ExprKind::Repeat(_, _) = expr.kind;
|
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
|
||||||
if let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind();
|
&& let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind()
|
||||||
if let ConstKind::Value(ty::ValTree::Leaf(element_count)) = cst.kind();
|
&& let Ok(element_count) = element_count.try_to_machine_usize(cx.tcx)
|
||||||
if let Ok(element_count) = element_count.try_to_machine_usize(cx.tcx);
|
&& let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes())
|
||||||
if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
|
&& !cx.tcx.hir().parent_iter(expr.hir_id)
|
||||||
if self.maximum_allowed_size < element_count * element_size;
|
.any(|(_, node)| matches!(node, Node::Item(Item { kind: ItemKind::Static(..), .. })))
|
||||||
then {
|
&& self.maximum_allowed_size < element_count * element_size {
|
||||||
span_lint_and_help(
|
span_lint_and_help(
|
||||||
cx,
|
cx,
|
||||||
LARGE_STACK_ARRAYS,
|
LARGE_STACK_ARRAYS,
|
||||||
expr.span,
|
expr.span,
|
||||||
&format!(
|
&format!(
|
||||||
"allocating a local array larger than {} bytes",
|
"allocating a local array larger than {} bytes",
|
||||||
self.maximum_allowed_size
|
self.maximum_allowed_size
|
||||||
),
|
),
|
||||||
None,
|
None,
|
||||||
&format!(
|
&format!(
|
||||||
"consider allocating on the heap with `vec!{}.into_boxed_slice()`",
|
"consider allocating on the heap with `vec!{}.into_boxed_slice()`",
|
||||||
snippet(cx, expr.span, "[...]")
|
snippet(cx, expr.span, "[...]")
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,6 +12,12 @@ enum E {
|
||||||
T(u32),
|
T(u32),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub static DOESNOTLINT: [u8; 512_001] = [0; 512_001];
|
||||||
|
pub static DOESNOTLINT2: [u8; 512_001] = {
|
||||||
|
let x = 0;
|
||||||
|
[x; 512_001]
|
||||||
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let bad = (
|
let bad = (
|
||||||
[0u32; 20_000_000],
|
[0u32; 20_000_000],
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: allocating a local array larger than 512000 bytes
|
error: allocating a local array larger than 512000 bytes
|
||||||
--> $DIR/large_stack_arrays.rs:17:9
|
--> $DIR/large_stack_arrays.rs:23:9
|
||||||
|
|
|
|
||||||
LL | [0u32; 20_000_000],
|
LL | [0u32; 20_000_000],
|
||||||
| ^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -8,7 +8,7 @@ LL | [0u32; 20_000_000],
|
||||||
= help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()`
|
= help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()`
|
||||||
|
|
||||||
error: allocating a local array larger than 512000 bytes
|
error: allocating a local array larger than 512000 bytes
|
||||||
--> $DIR/large_stack_arrays.rs:18:9
|
--> $DIR/large_stack_arrays.rs:24:9
|
||||||
|
|
|
|
||||||
LL | [S { data: [0; 32] }; 5000],
|
LL | [S { data: [0; 32] }; 5000],
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -16,7 +16,7 @@ LL | [S { data: [0; 32] }; 5000],
|
||||||
= help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()`
|
= help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()`
|
||||||
|
|
||||||
error: allocating a local array larger than 512000 bytes
|
error: allocating a local array larger than 512000 bytes
|
||||||
--> $DIR/large_stack_arrays.rs:19:9
|
--> $DIR/large_stack_arrays.rs:25:9
|
||||||
|
|
|
|
||||||
LL | [Some(""); 20_000_000],
|
LL | [Some(""); 20_000_000],
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^^^^^^^^
|
||||||
|
@ -24,7 +24,7 @@ LL | [Some(""); 20_000_000],
|
||||||
= help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()`
|
= help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()`
|
||||||
|
|
||||||
error: allocating a local array larger than 512000 bytes
|
error: allocating a local array larger than 512000 bytes
|
||||||
--> $DIR/large_stack_arrays.rs:20:9
|
--> $DIR/large_stack_arrays.rs:26:9
|
||||||
|
|
|
|
||||||
LL | [E::T(0); 5000],
|
LL | [E::T(0); 5000],
|
||||||
| ^^^^^^^^^^^^^^^
|
| ^^^^^^^^^^^^^^^
|
||||||
|
|
Loading…
Reference in a new issue