Skip to content

Commit efef920

Browse files
committed
add proc_macro test
1 parent 228f432 commit efef920

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed

clippy_lints/src/large_stack_arrays.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2+
use clippy_utils::is_from_proc_macro;
23
use clippy_utils::macros::macro_backtrace;
34
use clippy_utils::source::snippet;
45
use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node};
@@ -39,7 +40,7 @@ impl LargeStackArrays {
3940
impl_lint_pass!(LargeStackArrays => [LARGE_STACK_ARRAYS]);
4041

4142
impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
42-
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
43+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
4344
if let ExprKind::Repeat(_, _) | ExprKind::Array(_) = expr.kind
4445
&& !is_from_vec_macro(cx, expr.span)
4546
&& let ty::Array(element_type, cst) = cx.typeck_results().expr_ty(expr).kind()
@@ -66,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
6667
self.maximum_allowed_size
6768
),
6869
|diag| {
69-
if !expr.span.from_expansion() {
70+
if shoud_show_help(cx, expr) {
7071
diag.help(format!(
7172
"consider allocating on the heap with `vec!{}.into_boxed_slice()`",
7273
snippet(cx, expr.span, "[...]")
@@ -78,6 +79,20 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
7879
}
7980
}
8081

82+
/// Only giving help messages if the expr does not contains macro expanded codes.
83+
fn shoud_show_help<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> bool {
84+
match expr.kind {
85+
ExprKind::Array(elems) => !elems
86+
.iter()
87+
.any(|elem| elem.span.from_expansion() || is_from_proc_macro(cx, elem)),
88+
ExprKind::Repeat(elem, len) => {
89+
let len_span = cx.tcx.def_span(len.hir_id().owner);
90+
!(elem.span.from_expansion() || is_from_proc_macro(cx, elem) || len_span.from_expansion())
91+
},
92+
_ => false,
93+
}
94+
}
95+
8196
/// We shouldn't lint messages if the expr is already in a `vec!` call
8297
fn is_from_vec_macro(cx: &LateContext<'_>, expr_span: Span) -> bool {
8398
macro_backtrace(expr_span).any(|mac| cx.tcx.is_diagnostic_item(sym::vec_macro, mac.def_id))

tests/ui/auxiliary/proc_macros.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use proc_macro::token_stream::IntoIter;
99
use proc_macro::Delimiter::{self, Brace, Parenthesis};
1010
use proc_macro::Spacing::{self, Alone, Joint};
1111
use proc_macro::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree as TT};
12+
use syn::spanned::Spanned;
1213

1314
type Result<T> = core::result::Result<T, TokenStream>;
1415

@@ -124,6 +125,22 @@ fn write_with_span(s: Span, mut input: IntoIter, out: &mut TokenStream) -> Resul
124125
Ok(())
125126
}
126127

128+
/// Takes an array repeat expression such as `[0_u32; 2]`, and return the tokens with 10 times the
129+
/// original size, which turns to `[0_u32; 20]`.
130+
#[proc_macro]
131+
pub fn make_it_big(input: TokenStream) -> TokenStream {
132+
let mut expr_repeat = syn::parse_macro_input!(input as syn::ExprRepeat);
133+
let len_span = expr_repeat.len.span();
134+
if let syn::Expr::Lit(expr_lit) = &mut *expr_repeat.len {
135+
if let syn::Lit::Int(lit_int) = &expr_lit.lit {
136+
let orig_val = lit_int.base10_parse::<usize>().expect("not a valid length parameter");
137+
let new_val = orig_val.saturating_mul(10);
138+
expr_lit.lit = syn::parse_quote_spanned!( len_span => #new_val);
139+
}
140+
}
141+
quote::quote_spanned! { len_span => #expr_repeat }.into()
142+
}
143+
127144
/// Within the item this attribute is attached to, an `inline!` macro is available which expands the
128145
/// contained tokens as though they came from a macro expansion.
129146
///

tests/ui/large_stack_arrays.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::large_stack_arrays)]
23
#![allow(clippy::large_enum_variant)]
34

5+
extern crate proc_macros;
6+
47
#[derive(Clone, Copy)]
58
struct S {
69
pub data: [u64; 32],
@@ -88,4 +91,8 @@ fn issue_12586() {
8891
let y = dummy!(vec![dummy![x, x, x, x, x]]);
8992
let y = dummy![[x, x, x, x, x]];
9093
//~^ ERROR: allocating a local array larger than 512000 bytes
94+
95+
let y = proc_macros::make_it_big!([x; 1]);
96+
//~^ ERROR: allocating a local array larger than 512000 bytes
97+
let y = vec![proc_macros::make_it_big!([x; 10])];
9198
}

tests/ui/large_stack_arrays.stderr

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: allocating a local array larger than 512000 bytes
2-
--> tests/ui/large_stack_arrays.rs:29:14
2+
--> tests/ui/large_stack_arrays.rs:32:14
33
|
44
LL | let _x = [build(); 3];
55
| ^^^^^^^^^^^^
@@ -9,87 +9,104 @@ LL | let _x = [build(); 3];
99
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
1010

1111
error: allocating a local array larger than 512000 bytes
12-
--> tests/ui/large_stack_arrays.rs:32:14
12+
--> tests/ui/large_stack_arrays.rs:35:14
1313
|
1414
LL | let _y = [build(), build(), build()];
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
|
1717
= help: consider allocating on the heap with `vec![build(), build(), build()].into_boxed_slice()`
1818

1919
error: allocating a local array larger than 512000 bytes
20-
--> tests/ui/large_stack_arrays.rs:38:9
20+
--> tests/ui/large_stack_arrays.rs:41:9
2121
|
2222
LL | [0u32; 20_000_000],
2323
| ^^^^^^^^^^^^^^^^^^
2424
|
2525
= help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()`
2626

2727
error: allocating a local array larger than 512000 bytes
28-
--> tests/ui/large_stack_arrays.rs:40:9
28+
--> tests/ui/large_stack_arrays.rs:43:9
2929
|
3030
LL | [S { data: [0; 32] }; 5000],
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232
|
3333
= help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()`
3434

3535
error: allocating a local array larger than 512000 bytes
36-
--> tests/ui/large_stack_arrays.rs:42:9
36+
--> tests/ui/large_stack_arrays.rs:45:9
3737
|
3838
LL | [Some(""); 20_000_000],
3939
| ^^^^^^^^^^^^^^^^^^^^^^
4040
|
4141
= help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()`
4242

4343
error: allocating a local array larger than 512000 bytes
44-
--> tests/ui/large_stack_arrays.rs:44:9
44+
--> tests/ui/large_stack_arrays.rs:47:9
4545
|
4646
LL | [E::T(0); 5000],
4747
| ^^^^^^^^^^^^^^^
4848
|
4949
= help: consider allocating on the heap with `vec![E::T(0); 5000].into_boxed_slice()`
5050

5151
error: allocating a local array larger than 512000 bytes
52-
--> tests/ui/large_stack_arrays.rs:46:9
52+
--> tests/ui/large_stack_arrays.rs:49:9
5353
|
5454
LL | [0u8; usize::MAX],
5555
| ^^^^^^^^^^^^^^^^^
5656
|
5757
= help: consider allocating on the heap with `vec![0u8; usize::MAX].into_boxed_slice()`
5858

5959
error: allocating a local array larger than 512000 bytes
60-
--> tests/ui/large_stack_arrays.rs:81:25
60+
--> tests/ui/large_stack_arrays.rs:84:25
6161
|
6262
LL | let y = vec![dummy![[x, x, x, x, x]]];
6363
| ^^^^^^^^^^^^^^^
6464
|
6565
= help: consider allocating on the heap with `vec![x, x, x, x, x].into_boxed_slice()`
6666

6767
error: allocating a local array larger than 512000 bytes
68-
--> tests/ui/large_stack_arrays.rs:84:13
68+
--> tests/ui/large_stack_arrays.rs:87:13
6969
|
7070
LL | let y = [x, x, dummy!(x), x, x];
7171
| ^^^^^^^^^^^^^^^^^^^^^^^
7272
|
7373
= help: consider allocating on the heap with `vec![x, x, dummy!(x), x, x].into_boxed_slice()`
7474

7575
error: allocating a local array larger than 512000 bytes
76-
--> tests/ui/large_stack_arrays.rs:67:13
76+
--> tests/ui/large_stack_arrays.rs:70:13
7777
|
7878
LL | [$a, $b, $a, $b]
7979
| ^^^^^^^^^^^^^^^^
8080
...
8181
LL | let y = dummy![x => x];
8282
| -------------- in this macro invocation
8383
|
84+
= help: consider allocating on the heap with `vec![$a, $b, $a, $b].into_boxed_slice()`
8485
= note: this error originates in the macro `dummy` (in Nightly builds, run with -Z macro-backtrace for more info)
8586

8687
error: allocating a local array larger than 512000 bytes
87-
--> tests/ui/large_stack_arrays.rs:89:20
88+
--> tests/ui/large_stack_arrays.rs:92:20
8889
|
8990
LL | let y = dummy![[x, x, x, x, x]];
9091
| ^^^^^^^^^^^^^^^
9192
|
9293
= help: consider allocating on the heap with `vec![x, x, x, x, x].into_boxed_slice()`
9394

94-
error: aborting due to 11 previous errors
95+
error: allocating a local array larger than 512000 bytes
96+
--> tests/ui/large_stack_arrays.rs:95:39
97+
|
98+
LL | let y = proc_macros::make_it_big!([x; 1]);
99+
| ^^^^^^
100+
|
101+
= help: consider allocating on the heap with `vec![x; 1].into_boxed_slice()`
102+
103+
error: allocating a local array larger than 512000 bytes
104+
--> tests/ui/large_stack_arrays.rs:97:44
105+
|
106+
LL | let y = vec![proc_macros::make_it_big!([x; 10])];
107+
| ^^^^^^^
108+
|
109+
= help: consider allocating on the heap with `vec![x; 10].into_boxed_slice()`
110+
111+
error: aborting due to 13 previous errors
95112

0 commit comments

Comments
 (0)