2021-03-25 18:29:11 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
|
|
|
use clippy_utils::source::snippet;
|
|
|
|
use if_chain::if_chain;
|
2020-02-21 08:39:38 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::mir::interpret::ConstValue;
|
2021-08-30 14:38:27 +00:00
|
|
|
use rustc_middle::ty::layout::LayoutOf;
|
2020-03-30 09:02:14 +00:00
|
|
|
use rustc_middle::ty::{self, ConstKind};
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2019-11-12 22:22:53 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for local arrays that may be too large.
|
2019-11-12 22:22:53 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Large local arrays may cause stack overflow.
|
2019-11-12 22:22:53 +00:00
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Example
|
2019-11-12 22:22:53 +00:00
|
|
|
/// ```rust,ignore
|
|
|
|
/// let a = [0u32; 1_000_000];
|
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "1.41.0"]
|
2019-11-12 22:22:53 +00:00
|
|
|
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]);
|
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
|
2019-11-12 22:22:53 +00:00
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Repeat(_, _) = expr.kind;
|
2020-08-03 22:18:29 +00:00
|
|
|
if let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind();
|
2022-02-02 03:24:45 +00:00
|
|
|
if let ConstKind::Value(ConstValue::Scalar(element_count)) = cst.val();
|
2019-11-12 22:22:53 +00:00
|
|
|
if let Ok(element_count) = element_count.to_machine_usize(&cx.tcx);
|
Overhaul `TyS` and `Ty`.
Specifically, change `Ty` from this:
```
pub type Ty<'tcx> = &'tcx TyS<'tcx>;
```
to this
```
pub struct Ty<'tcx>(Interned<'tcx, TyS<'tcx>>);
```
There are two benefits to this.
- It's now a first class type, so we can define methods on it. This
means we can move a lot of methods away from `TyS`, leaving `TyS` as a
barely-used type, which is appropriate given that it's not meant to
be used directly.
- The uniqueness requirement is now explicit, via the `Interned` type.
E.g. the pointer-based `Eq` and `Hash` comes from `Interned`, rather
than via `TyS`, which wasn't obvious at all.
Much of this commit is boring churn. The interesting changes are in
these files:
- compiler/rustc_middle/src/arena.rs
- compiler/rustc_middle/src/mir/visit.rs
- compiler/rustc_middle/src/ty/context.rs
- compiler/rustc_middle/src/ty/mod.rs
Specifically:
- Most mentions of `TyS` are removed. It's very much a dumb struct now;
`Ty` has all the smarts.
- `TyS` now has `crate` visibility instead of `pub`.
- `TyS::make_for_test` is removed in favour of the static `BOOL_TY`,
which just works better with the new structure.
- The `Eq`/`Ord`/`Hash` impls are removed from `TyS`. `Interned`s impls
of `Eq`/`Hash` now suffice. `Ord` is now partly on `Interned`
(pointer-based, for the `Equal` case) and partly on `TyS`
(contents-based, for the other cases).
- There are many tedious sigil adjustments, i.e. adding or removing `*`
or `&`. They seem to be unavoidable.
2022-01-25 03:13:38 +00:00
|
|
|
if let Ok(element_size) = cx.layout_of(*element_type).map(|l| l.size.bytes());
|
2019-11-12 22:22:53 +00:00
|
|
|
if self.maximum_allowed_size < element_count * element_size;
|
|
|
|
then {
|
2020-01-27 01:56:22 +00:00
|
|
|
span_lint_and_help(
|
2019-11-12 22:22:53 +00:00
|
|
|
cx,
|
|
|
|
LARGE_STACK_ARRAYS,
|
|
|
|
expr.span,
|
|
|
|
&format!(
|
|
|
|
"allocating a local array larger than {} bytes",
|
|
|
|
self.maximum_allowed_size
|
|
|
|
),
|
2020-04-18 10:28:29 +00:00
|
|
|
None,
|
2019-11-12 22:22:53 +00:00
|
|
|
&format!(
|
2020-01-06 06:30:43 +00:00
|
|
|
"consider allocating on the heap with `vec!{}.into_boxed_slice()`",
|
2019-11-12 22:22:53 +00:00
|
|
|
snippet(cx, expr.span, "[...]")
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|