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

Allow Box<[JsValue]> parameter type #4371

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 14 additions & 1 deletion clippy_lints/src/escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::middle::expr_use_visitor::*;
use rustc::middle::mem_categorization::{cmt_, Categorization};
use rustc::ty::layout::LayoutOf;
use rustc::ty::query::TyCtxtAt;
use rustc::ty::{self, Ty};
use rustc::util::nodemap::HirIdSet;
use rustc::{declare_tool_lint, impl_lint_pass};
Expand Down Expand Up @@ -142,7 +143,10 @@ impl<'a, 'tcx> Delegate<'tcx> for EscapeDelegate<'a, 'tcx> {
if let StmtKind::Local(ref loc) = st.node {
if let Some(ref ex) = loc.init {
if let ExprKind::Box(..) = ex.node {
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
if is_non_trait_box(cmt.ty)
&& !self.is_large_box(cmt.ty)
&& !self.is_unsized_box(cmt.ty, cmt.span)
{
// let x = box (...)
self.set.insert(consume_pat.hir_id);
}
Expand Down Expand Up @@ -205,4 +209,13 @@ impl<'a, 'tcx> EscapeDelegate<'a, 'tcx> {
false
}
}

fn is_unsized_box(&self, ty: Ty<'tcx>, span: Span) -> bool {
if !ty.is_box() {
return false;
}

!ty.boxed_ty()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this function is clearer if you just write ty.is_box() && !ty.boxed_ty().is_sized(..)

Everything else LGTM.

.is_sized(TyCtxtAt { tcx: self.cx.tcx, span }, self.cx.param_env)
}
}
8 changes: 8 additions & 0 deletions tests/ui/escape_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ fn nowarn_large_array() {
}
}

fn nowarn_non_sized() {
let x: Box<[u32]> = box [1; 10000];
match &x {
// not moved
ref y => (),
}
}

/// ICE regression test
pub trait Foo {
type Item;
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/escape_analysis.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ LL | fn warn_arg(x: Box<A>) {
= note: `-D clippy::boxed-local` implied by `-D warnings`

error: local variable doesn't need to be boxed here
--> $DIR/escape_analysis.rs:125:12
--> $DIR/escape_analysis.rs:133:12
|
LL | pub fn new(_needs_name: Box<PeekableSeekable<&()>>) -> () {}
| ^^^^^^^^^^^

error: local variable doesn't need to be boxed here
--> $DIR/escape_analysis.rs:165:23
--> $DIR/escape_analysis.rs:173:23
|
LL | fn closure_borrow(x: Box<A>) {
| ^
Expand Down