-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Add large_stack_arrays
lint
#4807
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
use rustc::hir::*; | ||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; | ||
use rustc::mir::interpret::ConstValue; | ||
use rustc::ty; | ||
use rustc::{declare_tool_lint, impl_lint_pass}; | ||
|
||
use if_chain::if_chain; | ||
|
||
use crate::rustc_target::abi::LayoutOf; | ||
use crate::utils::{snippet, span_help_and_lint}; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for local arrays that may be too large. | ||
/// | ||
/// **Why is this bad?** Large local arrays may cause stack overflow. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// ```rust,ignore | ||
/// let a = [0u32; 1_000_000]; | ||
/// ``` | ||
pub LARGE_STACK_ARRAYS, | ||
pedantic, | ||
"allocating large arrays on stack may cause stack overflow" | ||
} | ||
|
||
pub struct LargeStackArrays { | ||
maximum_allowed_size: u64, | ||
} | ||
|
||
impl LargeStackArrays { | ||
#[must_use] | ||
pub fn new(maximum_allowed_size: u64) -> Self { | ||
Self { maximum_allowed_size } | ||
} | ||
} | ||
|
||
impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]); | ||
|
||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LargeStackArrays { | ||
fn check_expr(&mut self, cx: &LateContext<'_, '_>, expr: &Expr) { | ||
if_chain! { | ||
if let ExprKind::Repeat(_, _) = expr.kind; | ||
if let ty::Array(element_type, cst) = cx.tables.expr_ty(expr).kind; | ||
if let ConstValue::Scalar(element_count) = cst.val; | ||
if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx); | ||
if let Ok(element_size) = cx.layout_of(element_type).map(|l| l.size.bytes()); | ||
if self.maximum_allowed_size < element_count * element_size; | ||
then { | ||
span_help_and_lint( | ||
cx, | ||
LARGE_STACK_ARRAYS, | ||
expr.span, | ||
&format!( | ||
"allocating a local array larger than {} bytes", | ||
self.maximum_allowed_size | ||
), | ||
&format!( | ||
"consider allocating on the heap with vec!{}.into_boxed_slice()", | ||
snippet(cx, expr.span, "[...]") | ||
), | ||
); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `third-party` at line 5 column 1 | ||
error: error reading Clippy's configuration file `$DIR/clippy.toml`: unknown field `foobar`, expected one of `blacklisted-names`, `cognitive-complexity-threshold`, `cyclomatic-complexity-threshold`, `doc-valid-idents`, `too-many-arguments-threshold`, `type-complexity-threshold`, `single-char-binding-names-threshold`, `too-large-for-stack`, `enum-variant-name-threshold`, `enum-variant-size-threshold`, `verbose-bit-mask-threshold`, `literal-representation-threshold`, `trivial-copy-size-limit`, `too-many-lines-threshold`, `array-size-threshold`, `third-party` at line 5 column 1 | ||
|
||
error: aborting due to previous error | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#![warn(clippy::large_stack_arrays)] | ||
#![allow(clippy::large_enum_variant)] | ||
|
||
#[derive(Clone, Copy)] | ||
struct S { | ||
pub data: [u64; 32], | ||
} | ||
|
||
#[derive(Clone, Copy)] | ||
enum E { | ||
S(S), | ||
T(u32), | ||
} | ||
|
||
fn main() { | ||
let bad = ( | ||
[0u32; 20_000_000], | ||
[S { data: [0; 32] }; 5000], | ||
[Some(""); 20_000_000], | ||
[E::T(0); 5000], | ||
); | ||
|
||
let good = ( | ||
[0u32; 1000], | ||
[S { data: [0; 32] }; 1000], | ||
[Some(""); 1000], | ||
[E::T(0); 1000], | ||
[(); 20_000_000], | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
error: allocating a local array larger than 512000 bytes | ||
--> $DIR/large_stack_arrays.rs:17:9 | ||
| | ||
LL | [0u32; 20_000_000], | ||
| ^^^^^^^^^^^^^^^^^^ | ||
| | ||
= note: `-D clippy::large-stack-arrays` implied by `-D warnings` | ||
= help: consider allocating on the heap with vec![0u32; 20_000_000].into_boxed_slice() | ||
|
||
error: allocating a local array larger than 512000 bytes | ||
--> $DIR/large_stack_arrays.rs:18:9 | ||
| | ||
LL | [S { data: [0; 32] }; 5000], | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= 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 | ||
--> $DIR/large_stack_arrays.rs:19:9 | ||
| | ||
LL | [Some(""); 20_000_000], | ||
| ^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider allocating on the heap with vec![Some(""); 20_000_000].into_boxed_slice() | ||
|
||
error: allocating a local array larger than 512000 bytes | ||
--> $DIR/large_stack_arrays.rs:20:9 | ||
| | ||
LL | [E::T(0); 5000], | ||
| ^^^^^^^^^^^^^^^ | ||
| | ||
= help: consider allocating on the heap with vec![E::T(0); 5000].into_boxed_slice() | ||
|
||
error: aborting due to 4 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.