2020-10-23 15:16:59 -05:00
|
|
|
use crate::utils;
|
2020-01-10 10:14:17 -06:00
|
|
|
use crate::utils::match_var;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-09-24 07:49:22 -05:00
|
|
|
use rustc_hir as hir;
|
2020-01-06 10:39:50 -06:00
|
|
|
use rustc_hir::def::Res;
|
2020-09-24 07:49:22 -05:00
|
|
|
use rustc_hir::intravisit;
|
2020-01-10 10:14:17 -06:00
|
|
|
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
2020-10-23 15:16:59 -05:00
|
|
|
use rustc_hir::{Expr, ExprKind, HirId, Path};
|
2020-02-16 20:07:26 -06:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2020-01-12 00:08:41 -06:00
|
|
|
use rustc_lint::LateContext;
|
2020-03-30 04:02:14 -05:00
|
|
|
use rustc_middle::hir::map::Map;
|
|
|
|
use rustc_middle::ty;
|
2020-05-08 06:57:01 -05:00
|
|
|
use rustc_span::symbol::{Ident, Symbol};
|
2020-06-23 10:05:22 -05:00
|
|
|
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, PlaceBase, PlaceWithHirId};
|
2018-05-27 17:02:38 -05:00
|
|
|
|
2019-01-30 19:15:29 -06:00
|
|
|
/// Returns a set of mutated local variable IDs, or `None` if mutations could not be determined.
|
2020-06-25 15:41:36 -05:00
|
|
|
pub fn mutated_variables<'tcx>(expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) -> Option<FxHashSet<HirId>> {
|
2018-05-27 17:02:38 -05:00
|
|
|
let mut delegate = MutVarsDelegate {
|
2018-09-11 18:34:52 -05:00
|
|
|
used_mutably: FxHashSet::default(),
|
2018-05-27 17:02:38 -05:00
|
|
|
skip: false,
|
|
|
|
};
|
2020-03-19 08:33:10 -05:00
|
|
|
let def_id = expr.hir_id.owner.to_def_id();
|
2019-11-29 04:12:19 -06:00
|
|
|
cx.tcx.infer_ctxt().enter(|infcx| {
|
2020-07-17 03:47:04 -05:00
|
|
|
ExprUseVisitor::new(
|
|
|
|
&mut delegate,
|
|
|
|
&infcx,
|
|
|
|
def_id.expect_local(),
|
|
|
|
cx.param_env,
|
|
|
|
cx.typeck_results(),
|
|
|
|
)
|
|
|
|
.walk_expr(expr);
|
2019-11-29 04:12:19 -06:00
|
|
|
});
|
2018-05-27 17:02:38 -05:00
|
|
|
|
|
|
|
if delegate.skip {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(delegate.used_mutably)
|
|
|
|
}
|
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
pub fn is_potentially_mutated<'tcx>(variable: &'tcx Path<'_>, expr: &'tcx Expr<'_>, cx: &LateContext<'tcx>) -> bool {
|
2019-06-02 11:30:40 -05:00
|
|
|
if let Res::Local(id) = variable.res {
|
|
|
|
mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
|
|
|
|
} else {
|
2019-06-23 21:00:05 -05:00
|
|
|
true
|
2019-06-02 11:30:40 -05:00
|
|
|
}
|
2018-05-27 17:02:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
struct MutVarsDelegate {
|
2019-03-01 06:26:06 -06:00
|
|
|
used_mutably: FxHashSet<HirId>,
|
2018-05-27 17:02:38 -05:00
|
|
|
skip: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MutVarsDelegate {
|
2018-10-11 17:18:58 -05:00
|
|
|
#[allow(clippy::similar_names)]
|
2020-06-17 17:13:05 -05:00
|
|
|
fn update(&mut self, cat: &PlaceWithHirId<'tcx>) {
|
|
|
|
match cat.place.base {
|
2019-11-28 09:33:12 -06:00
|
|
|
PlaceBase::Local(id) => {
|
2018-05-27 17:02:38 -05:00
|
|
|
self.used_mutably.insert(id);
|
|
|
|
},
|
2019-11-28 09:33:12 -06:00
|
|
|
PlaceBase::Upvar(_) => {
|
2018-05-27 17:02:38 -05:00
|
|
|
//FIXME: This causes false negatives. We can't get the `NodeId` from
|
|
|
|
//`Categorization::Upvar(_)`. So we search for any `Upvar`s in the
|
|
|
|
//`while`-body, not just the ones in the condition.
|
|
|
|
self.skip = true
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
|
2020-06-17 17:13:05 -05:00
|
|
|
fn consume(&mut self, _: &PlaceWithHirId<'tcx>, _: ConsumeMode) {}
|
2018-05-27 17:02:38 -05:00
|
|
|
|
2020-06-17 17:13:05 -05:00
|
|
|
fn borrow(&mut self, cmt: &PlaceWithHirId<'tcx>, bk: ty::BorrowKind) {
|
2018-05-27 17:02:38 -05:00
|
|
|
if let ty::BorrowKind::MutBorrow = bk {
|
2019-11-28 09:33:12 -06:00
|
|
|
self.update(&cmt)
|
2018-05-27 17:02:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-17 17:13:05 -05:00
|
|
|
fn mutate(&mut self, cmt: &PlaceWithHirId<'tcx>) {
|
2019-11-28 09:33:12 -06:00
|
|
|
self.update(&cmt)
|
2018-05-27 17:02:38 -05:00
|
|
|
}
|
|
|
|
}
|
2020-01-10 10:14:17 -06:00
|
|
|
|
|
|
|
pub struct UsedVisitor {
|
2020-05-17 10:36:26 -05:00
|
|
|
pub var: Symbol, // var to look for
|
|
|
|
pub used: bool, // has the var been used otherwise?
|
2020-01-10 10:14:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for UsedVisitor {
|
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
|
|
|
if match_var(expr, self.var) {
|
|
|
|
self.used = true;
|
|
|
|
} else {
|
|
|
|
walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-15 17:41:20 -05:00
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
2020-01-10 10:14:17 -06:00
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_unused<'tcx>(ident: &'tcx Ident, body: &'tcx Expr<'_>) -> bool {
|
|
|
|
let mut visitor = UsedVisitor {
|
|
|
|
var: ident.name,
|
|
|
|
used: false,
|
|
|
|
};
|
|
|
|
walk_expr(&mut visitor, body);
|
|
|
|
!visitor.used
|
|
|
|
}
|
2020-09-24 07:49:22 -05:00
|
|
|
|
|
|
|
pub struct ParamBindingIdCollector {
|
|
|
|
binding_hir_ids: Vec<hir::HirId>,
|
|
|
|
}
|
|
|
|
impl<'tcx> ParamBindingIdCollector {
|
|
|
|
fn collect_binding_hir_ids(body: &'tcx hir::Body<'tcx>) -> Vec<hir::HirId> {
|
|
|
|
let mut finder = ParamBindingIdCollector {
|
|
|
|
binding_hir_ids: Vec::new(),
|
|
|
|
};
|
|
|
|
finder.visit_body(body);
|
|
|
|
finder.binding_hir_ids
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<'tcx> intravisit::Visitor<'tcx> for ParamBindingIdCollector {
|
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
|
|
|
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
|
|
|
|
if let hir::PatKind::Binding(_, hir_id, ..) = param.pat.kind {
|
|
|
|
self.binding_hir_ids.push(hir_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
|
|
|
|
intravisit::NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct BindingUsageFinder<'a, 'tcx> {
|
|
|
|
cx: &'a LateContext<'tcx>,
|
|
|
|
binding_ids: Vec<hir::HirId>,
|
|
|
|
usage_found: bool,
|
|
|
|
}
|
|
|
|
impl<'a, 'tcx> BindingUsageFinder<'a, 'tcx> {
|
|
|
|
pub fn are_params_used(cx: &'a LateContext<'tcx>, body: &'tcx hir::Body<'tcx>) -> bool {
|
|
|
|
let mut finder = BindingUsageFinder {
|
|
|
|
cx,
|
|
|
|
binding_ids: ParamBindingIdCollector::collect_binding_hir_ids(body),
|
|
|
|
usage_found: false,
|
|
|
|
};
|
|
|
|
finder.visit_body(body);
|
|
|
|
finder.usage_found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl<'a, 'tcx> intravisit::Visitor<'tcx> for BindingUsageFinder<'a, 'tcx> {
|
|
|
|
type Map = Map<'tcx>;
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
|
|
|
|
if !self.usage_found {
|
|
|
|
intravisit::walk_expr(self, expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_path(&mut self, path: &'tcx hir::Path<'tcx>, _: hir::HirId) {
|
|
|
|
if let hir::def::Res::Local(id) = path.res {
|
|
|
|
if self.binding_ids.contains(&id) {
|
|
|
|
self.usage_found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
|
|
|
|
intravisit::NestedVisitorMap::OnlyBodies(self.cx.tcx.hir())
|
|
|
|
}
|
|
|
|
}
|
2020-10-23 15:16:59 -05:00
|
|
|
|
|
|
|
struct ReturnBreakContinueMacroVisitor {
|
|
|
|
seen_return_break_continue: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ReturnBreakContinueMacroVisitor {
|
|
|
|
fn new() -> ReturnBreakContinueMacroVisitor {
|
|
|
|
ReturnBreakContinueMacroVisitor {
|
|
|
|
seen_return_break_continue: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Visitor<'tcx> for ReturnBreakContinueMacroVisitor {
|
|
|
|
type Map = Map<'tcx>;
|
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
|
|
|
|
NestedVisitorMap::None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
|
|
|
|
if self.seen_return_break_continue {
|
|
|
|
// No need to look farther if we've already seen one of them
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
match &ex.kind {
|
|
|
|
ExprKind::Ret(..) | ExprKind::Break(..) | ExprKind::Continue(..) => {
|
|
|
|
self.seen_return_break_continue = true;
|
|
|
|
},
|
|
|
|
// Something special could be done here to handle while or for loop
|
|
|
|
// desugaring, as this will detect a break if there's a while loop
|
|
|
|
// or a for loop inside the expression.
|
|
|
|
_ => {
|
|
|
|
if utils::in_macro(ex.span) {
|
|
|
|
self.seen_return_break_continue = true;
|
|
|
|
} else {
|
|
|
|
rustc_hir::intravisit::walk_expr(self, ex);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn contains_return_break_continue_macro(expression: &Expr<'_>) -> bool {
|
|
|
|
let mut recursive_visitor = ReturnBreakContinueMacroVisitor::new();
|
|
|
|
recursive_visitor.visit_expr(expression);
|
|
|
|
recursive_visitor.seen_return_break_continue
|
|
|
|
}
|