2020-01-10 17:14:17 +01:00
|
|
|
use crate::utils::match_var;
|
|
|
|
|
use rustc::hir::map::Map;
|
2018-12-29 16:04:45 +01:00
|
|
|
use rustc::ty;
|
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
2020-01-07 01:39:50 +09:00
|
|
|
use rustc_hir::def::Res;
|
2020-01-10 17:14:17 +01:00
|
|
|
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
|
2020-01-07 01:39:50 +09:00
|
|
|
use rustc_hir::*;
|
2020-02-17 11:07:26 +09:00
|
|
|
use rustc_infer::infer::TyCtxtInferExt;
|
2020-01-12 15:08:41 +09:00
|
|
|
use rustc_lint::LateContext;
|
2020-01-10 17:14:17 +01:00
|
|
|
use rustc_span::symbol::Ident;
|
2019-11-29 11:12:19 +01:00
|
|
|
use rustc_typeck::expr_use_visitor::*;
|
2020-01-10 17:14:17 +01:00
|
|
|
use syntax::ast;
|
2018-05-28 00:02:38 +02:00
|
|
|
|
2019-01-31 01:15:29 +00:00
|
|
|
/// Returns a set of mutated local variable IDs, or `None` if mutations could not be determined.
|
2019-12-27 16:12:26 +09:00
|
|
|
pub fn mutated_variables<'a, 'tcx>(expr: &'tcx Expr<'_>, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<HirId>> {
|
2018-05-28 00:02:38 +02:00
|
|
|
let mut delegate = MutVarsDelegate {
|
2018-09-12 01:34:52 +02:00
|
|
|
used_mutably: FxHashSet::default(),
|
2018-05-28 00:02:38 +02:00
|
|
|
skip: false,
|
|
|
|
|
};
|
|
|
|
|
let def_id = def_id::DefId::local(expr.hir_id.owner);
|
2019-11-29 11:12:19 +01:00
|
|
|
cx.tcx.infer_ctxt().enter(|infcx| {
|
|
|
|
|
ExprUseVisitor::new(&mut delegate, &infcx, def_id, cx.param_env, cx.tables).walk_expr(expr);
|
|
|
|
|
});
|
2018-05-28 00:02:38 +02:00
|
|
|
|
|
|
|
|
if delegate.skip {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
Some(delegate.used_mutably)
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 16:12:26 +09:00
|
|
|
pub fn is_potentially_mutated<'a, 'tcx>(
|
2019-12-30 13:02:10 +09:00
|
|
|
variable: &'tcx Path<'_>,
|
2019-12-27 16:12:26 +09:00
|
|
|
expr: &'tcx Expr<'_>,
|
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
|
) -> bool {
|
2019-06-02 18:30:40 +02:00
|
|
|
if let Res::Local(id) = variable.res {
|
|
|
|
|
mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
|
|
|
|
|
} else {
|
2019-06-24 04:00:05 +02:00
|
|
|
true
|
2019-06-02 18:30:40 +02:00
|
|
|
}
|
2018-05-28 00:02:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct MutVarsDelegate {
|
2019-03-01 13:26:06 +01:00
|
|
|
used_mutably: FxHashSet<HirId>,
|
2018-05-28 00:02:38 +02:00
|
|
|
skip: bool,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx> MutVarsDelegate {
|
2018-10-11 15:18:58 -07:00
|
|
|
#[allow(clippy::similar_names)]
|
2019-11-28 07:33:12 -08:00
|
|
|
fn update(&mut self, cat: &Place<'tcx>) {
|
|
|
|
|
match cat.base {
|
|
|
|
|
PlaceBase::Local(id) => {
|
2018-05-28 00:02:38 +02:00
|
|
|
self.used_mutably.insert(id);
|
|
|
|
|
},
|
2019-11-28 07:33:12 -08:00
|
|
|
PlaceBase::Upvar(_) => {
|
2018-05-28 00:02:38 +02: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 {
|
2019-11-28 07:09:02 -08:00
|
|
|
fn consume(&mut self, _: &Place<'tcx>, _: ConsumeMode) {}
|
2018-05-28 00:02:38 +02:00
|
|
|
|
2019-11-28 07:09:02 -08:00
|
|
|
fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) {
|
2018-05-28 00:02:38 +02:00
|
|
|
if let ty::BorrowKind::MutBorrow = bk {
|
2019-11-28 07:33:12 -08:00
|
|
|
self.update(&cmt)
|
2018-05-28 00:02:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-28 07:09:02 -08:00
|
|
|
fn mutate(&mut self, cmt: &Place<'tcx>) {
|
2019-11-28 07:33:12 -08:00
|
|
|
self.update(&cmt)
|
2018-05-28 00:02:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-01-10 17:14:17 +01:00
|
|
|
|
|
|
|
|
pub struct UsedVisitor {
|
|
|
|
|
pub var: ast::Name, // var to look for
|
|
|
|
|
pub used: bool, // has the var been used otherwise?
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn nested_visit_map(&mut self) -> NestedVisitorMap<'_, Self::Map> {
|
|
|
|
|
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
|
|
|
|
|
}
|