Skip to content

Commit 78857cc

Browse files
committed
Auto merge of #5907 - wiomoc:feature/useless-vec-max-size, r=flip1995
appreciative too_large_for_stack in useless `vec!` Fixes: #5847 changelog: Add `too_large_for_stack ` configuration option for `USELESS_VEC`
2 parents dff7e74 + 8514b84 commit 78857cc

File tree

5 files changed

+76
-39
lines changed

5 files changed

+76
-39
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,11 +933,11 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
933933
store.register_late_pass(move || box cognitive_complexity::CognitiveComplexity::new(cognitive_complexity_threshold));
934934
let too_large_for_stack = conf.too_large_for_stack;
935935
store.register_late_pass(move || box escape::BoxedLocal{too_large_for_stack});
936+
store.register_late_pass(move || box vec::UselessVec{too_large_for_stack});
936937
store.register_late_pass(|| box panic_unimplemented::PanicUnimplemented);
937938
store.register_late_pass(|| box strings::StringLitAsBytes);
938939
store.register_late_pass(|| box derive::Derive);
939940
store.register_late_pass(|| box types::CharLitAsU8);
940-
store.register_late_pass(|| box vec::UselessVec);
941941
store.register_late_pass(|| box drop_bounds::DropBounds);
942942
store.register_late_pass(|| box get_last_with_len::GetLastWithLen);
943943
store.register_late_pass(|| box drop_forget_ref::DropForgetRef);

clippy_lints/src/utils/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ define_Conf! {
138138
(type_complexity_threshold, "type_complexity_threshold": u64, 250),
139139
/// Lint: MANY_SINGLE_CHAR_NAMES. The maximum number of single char bindings a scope may have
140140
(single_char_binding_names_threshold, "single_char_binding_names_threshold": u64, 4),
141-
/// Lint: BOXED_LOCAL. The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
141+
/// Lint: BOXED_LOCAL, USELESS_VEC. The maximum size of objects (in bytes) that will be linted. Larger objects are ok on the heap
142142
(too_large_for_stack, "too_large_for_stack": u64, 200),
143143
/// Lint: ENUM_VARIANT_NAMES. The minimum number of enum variants for the lints about variant names to trigger
144144
(enum_variant_name_threshold, "enum_variant_name_threshold": u64, 3),

clippy_lints/src/vec.rs

Lines changed: 60 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
use crate::consts::constant;
1+
use crate::consts::{constant, Constant};
2+
use crate::rustc_target::abi::LayoutOf;
23
use crate::utils::{higher, is_copy, snippet_with_applicability, span_lint_and_sugg};
34
use if_chain::if_chain;
45
use rustc_errors::Applicability;
56
use rustc_hir::{BorrowKind, Expr, ExprKind};
67
use rustc_lint::{LateContext, LateLintPass};
78
use rustc_middle::ty::{self, Ty};
8-
use rustc_session::{declare_lint_pass, declare_tool_lint};
9+
use rustc_session::{declare_tool_lint, impl_lint_pass};
910
use rustc_span::source_map::Span;
1011

12+
#[allow(clippy::module_name_repetitions)]
13+
#[derive(Copy, Clone)]
14+
pub struct UselessVec {
15+
pub too_large_for_stack: u64,
16+
}
17+
1118
declare_clippy_lint! {
1219
/// **What it does:** Checks for usage of `&vec![..]` when using `&[..]` would
1320
/// be possible.
@@ -31,7 +38,7 @@ declare_clippy_lint! {
3138
"useless `vec!`"
3239
}
3340

34-
declare_lint_pass!(UselessVec => [USELESS_VEC]);
41+
impl_lint_pass!(UselessVec => [USELESS_VEC]);
3542

3643
impl<'tcx> LateLintPass<'tcx> for UselessVec {
3744
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
@@ -42,7 +49,7 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
4249
if let ExprKind::AddrOf(BorrowKind::Ref, _, ref addressee) = expr.kind;
4350
if let Some(vec_args) = higher::vec_macro(cx, addressee);
4451
then {
45-
check_vec_macro(cx, &vec_args, expr.span);
52+
self.check_vec_macro(cx, &vec_args, expr.span);
4653
}
4754
}
4855

@@ -60,46 +67,62 @@ impl<'tcx> LateLintPass<'tcx> for UselessVec {
6067
.ctxt()
6168
.outer_expn_data()
6269
.call_site;
63-
check_vec_macro(cx, &vec_args, span);
70+
self.check_vec_macro(cx, &vec_args, span);
6471
}
6572
}
6673
}
6774
}
6875

69-
fn check_vec_macro<'tcx>(cx: &LateContext<'tcx>, vec_args: &higher::VecArgs<'tcx>, span: Span) {
70-
let mut applicability = Applicability::MachineApplicable;
71-
let snippet = match *vec_args {
72-
higher::VecArgs::Repeat(elem, len) => {
73-
if constant(cx, cx.typeck_results(), len).is_some() {
74-
format!(
75-
"&[{}; {}]",
76-
snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
77-
snippet_with_applicability(cx, len.span, "len", &mut applicability)
78-
)
79-
} else {
80-
return;
81-
}
82-
},
83-
higher::VecArgs::Vec(args) => {
84-
if let Some(last) = args.iter().last() {
85-
let span = args[0].span.to(last.span);
76+
impl UselessVec {
77+
fn check_vec_macro<'tcx>(self, cx: &LateContext<'tcx>, vec_args: &higher::VecArgs<'tcx>, span: Span) {
78+
let mut applicability = Applicability::MachineApplicable;
79+
let snippet = match *vec_args {
80+
higher::VecArgs::Repeat(elem, len) => {
81+
if let Some((Constant::Int(len_constant), _)) = constant(cx, cx.typeck_results(), len) {
82+
#[allow(clippy::cast_possible_truncation)]
83+
if len_constant as u64 * size_of(cx, elem) > self.too_large_for_stack {
84+
return;
85+
}
8686

87-
format!("&[{}]", snippet_with_applicability(cx, span, "..", &mut applicability))
88-
} else {
89-
"&[]".into()
90-
}
91-
},
92-
};
87+
format!(
88+
"&[{}; {}]",
89+
snippet_with_applicability(cx, elem.span, "elem", &mut applicability),
90+
snippet_with_applicability(cx, len.span, "len", &mut applicability)
91+
)
92+
} else {
93+
return;
94+
}
95+
},
96+
higher::VecArgs::Vec(args) => {
97+
if let Some(last) = args.iter().last() {
98+
#[allow(clippy::cast_possible_truncation)]
99+
if args.len() as u64 * size_of(cx, last) > self.too_large_for_stack {
100+
return;
101+
}
102+
let span = args[0].span.to(last.span);
103+
104+
format!("&[{}]", snippet_with_applicability(cx, span, "..", &mut applicability))
105+
} else {
106+
"&[]".into()
107+
}
108+
},
109+
};
110+
111+
span_lint_and_sugg(
112+
cx,
113+
USELESS_VEC,
114+
span,
115+
"useless use of `vec!`",
116+
"you can use a slice directly",
117+
snippet,
118+
applicability,
119+
);
120+
}
121+
}
93122

94-
span_lint_and_sugg(
95-
cx,
96-
USELESS_VEC,
97-
span,
98-
"useless use of `vec!`",
99-
"you can use a slice directly",
100-
snippet,
101-
applicability,
102-
);
123+
fn size_of(cx: &LateContext<'_>, expr: &Expr<'_>) -> u64 {
124+
let ty = cx.typeck_results().expr_ty_adjusted(expr);
125+
cx.layout_of(ty).map_or(0, |l| l.size.bytes())
103126
}
104127

105128
/// Returns the item type of the vector (i.e., the `T` in `Vec<T>`).

tests/ui/vec.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ fn main() {
5252
for a in vec![NonCopy, NonCopy] {
5353
println!("{:?}", a);
5454
}
55+
56+
on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
57+
58+
// Ok
59+
for a in vec![1; 201] {
60+
println!("{:?}", a);
61+
}
5562
}

tests/ui/vec.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,11 @@ fn main() {
5252
for a in vec![NonCopy, NonCopy] {
5353
println!("{:?}", a);
5454
}
55+
56+
on_vec(&vec![1; 201]); // Ok, size of `vec` higher than `too_large_for_stack`
57+
58+
// Ok
59+
for a in vec![1; 201] {
60+
println!("{:?}", a);
61+
}
5562
}

0 commit comments

Comments
 (0)