Skip to content
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

Adds lint for Vec<Box<T: Sized>> #3545

Merged
merged 6 commits into from
Dec 14, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions clippy_lints/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,12 +290,14 @@ fn check_ty(cx: &LateContext<'_, '_>, ast_ty: &hir::Ty, is_local: bool) {
let boxed_type = cx.tcx.type_of(def_id);
if boxed_type.is_sized(cx.tcx.at(DUMMY_SP), cx.param_env);
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
then {
span_help_and_lint(
span_lint_and_sugg(
cx,
VEC_BOX_SIZED,
ast_ty.span,
"you seem to be trying to use `Vec<Box<T>>`, but T is Sized. Consider using just `Vec<T>`",
"`Vec<T>` is already on the heap, `Vec<Box<T>>` makes an extra allocation.",
"you seem to be trying to use `Vec<Box<T>>`, but T is Sized. `Vec<T>` is already on the heap, `Vec<Box<T>>` makes an extra allocation.",
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
flip1995 marked this conversation as resolved.
Show resolved Hide resolved
"try",
format!("Vec<{}>", boxed_type),
Applicability::MachineApplicable
Kampfkarren marked this conversation as resolved.
Show resolved Hide resolved
)
}
}
Expand Down
5 changes: 2 additions & 3 deletions tests/ui/vec_box_sized.stderr
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
error: you seem to be trying to use `Vec<Box<T>>`, but T is Sized. Consider using just `Vec<T>`
error: you seem to be trying to use `Vec<Box<T>>`, but T is Sized. `Vec<T>` is already on the heap, `Vec<Box<T>>` makes an extra allocation.
--> $DIR/vec_box_sized.rs:10:14
|
10 | sized_type: Vec<Box<SizedStruct>>,
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^ help: try: `Vec<SizedStruct>`
|
= note: `-D clippy::vec-box-sized` implied by `-D warnings`
= help: `Vec<T>` is already on the heap, `Vec<Box<T>>` makes an extra allocation.

error: aborting due to previous error