-use rustc::hir::*;
+use crate::utils::span_lint;
use rustc::hir::intravisit as visit;
-use rustc::hir::map::Node::{NodeExpr, NodeStmt};
-use rustc::lint::*;
+use rustc::hir::*;
+use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::middle::expr_use_visitor::*;
use rustc::middle::mem_categorization::{cmt_, Categorization};
-use rustc::ty::{self, Ty};
use rustc::ty::layout::LayoutOf;
+use rustc::ty::{self, Ty};
use rustc::util::nodemap::NodeSet;
+use rustc::{declare_tool_lint, lint_array};
use syntax::ast::NodeId;
-use syntax::codemap::Span;
-use crate::utils::span_lint;
+use syntax::source_map::Span;
pub struct Pass {
pub too_large_for_stack: u64,
"using `Box<T>` where unnecessary"
}
-fn is_non_trait_box(ty: Ty) -> bool {
+fn is_non_trait_box(ty: Ty<'_>) -> bool {
ty.is_box() && !ty.boxed_ty().is_trait()
}
_: Span,
node_id: NodeId,
) {
- let fn_def_id = cx.tcx.hir.local_def_id(node_id);
+ // If the method is an impl for a trait, don't warn
+ let parent_id = cx.tcx.hir().get_parent(node_id);
+ let parent_node = cx.tcx.hir().find(parent_id);
+
+ if let Some(Node::Item(item)) = parent_node {
+ if let ItemKind::Impl(_, _, _, _, Some(..), _, _) = item.node {
+ return;
+ }
+ }
+
let mut v = EscapeDelegate {
cx,
- set: NodeSet(),
+ set: NodeSet::default(),
too_large_for_stack: self.too_large_for_stack,
};
+ let fn_def_id = cx.tcx.hir().local_def_id(node_id);
let region_scope_tree = &cx.tcx.region_scope_tree(fn_def_id);
ExprUseVisitor::new(&mut v, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).consume_body(body);
span_lint(
cx,
BOXED_LOCAL,
- cx.tcx.hir.span(node),
+ cx.tcx.hir().span(node),
"local variable doesn't need to be boxed here",
);
}
}
fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
fn consume_pat(&mut self, consume_pat: &Pat, cmt: &cmt_<'tcx>, _: ConsumeMode) {
- let map = &self.cx.tcx.hir;
+ let map = &self.cx.tcx.hir();
if map.is_argument(consume_pat.id) {
// Skip closure arguments
- if let Some(NodeExpr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
+ if let Some(Node::Expr(..)) = map.find(map.get_parent_node(consume_pat.id)) {
return;
}
if is_non_trait_box(cmt.ty) && !self.is_large_box(cmt.ty) {
}
if let Categorization::Rvalue(..) = cmt.cat {
let id = map.hir_to_node_id(cmt.hir_id);
- if let Some(NodeStmt(st)) = map.find(map.get_parent_node(id)) {
- if let StmtKind::Decl(ref decl, _) = st.node {
- if let DeclKind::Local(ref loc) = decl.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) {
- // let x = box (...)
- self.set.insert(consume_pat.id);
- }
- // TODO Box::new
- // TODO vec![]
- // TODO "foo".to_owned() and friends
+ if let Some(Node::Stmt(st)) = map.find(map.get_parent_node(id)) {
+ 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) {
+ // let x = box (...)
+ self.set.insert(consume_pat.id);
}
+ // TODO Box::new
+ // TODO vec![]
+ // TODO "foo".to_owned() and friends
}
}
}
}
}
}
- fn borrow(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region, _: ty::BorrowKind, loan_cause: LoanCause) {
+ fn borrow(
+ &mut self,
+ _: NodeId,
+ _: Span,
+ cmt: &cmt_<'tcx>,
+ _: ty::Region<'_>,
+ _: ty::BorrowKind,
+ loan_cause: LoanCause,
+ ) {
if let Categorization::Local(lid) = cmt.cat {
match loan_cause {
// x.foo()