-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added documentation minor style fix change as to ::from add ignore to doc include threshold in lint message/make suggestion more apparent/use Scalar api instead of matching style fix shange snippet_opt to snippet
- Loading branch information
Showing
9 changed files
with
150 additions
and
3 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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 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 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 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 | ||
|