2019-05-03 19:03:12 -05:00
|
|
|
use rustc::hir::def::Res;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::hir::*;
|
2019-01-30 19:15:29 -06:00
|
|
|
use rustc::lint::LateContext;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::middle::expr_use_visitor::*;
|
2019-11-28 09:09:02 -06:00
|
|
|
use rustc::middle::mem_categorization::Place;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc::middle::mem_categorization::Categorization;
|
|
|
|
use rustc::ty;
|
|
|
|
use rustc_data_structures::fx::FxHashSet;
|
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.
|
2019-06-19 13:36:23 -05:00
|
|
|
pub fn mutated_variables<'a, 'tcx>(expr: &'tcx Expr, cx: &'a LateContext<'a, '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,
|
|
|
|
};
|
|
|
|
let def_id = def_id::DefId::local(expr.hir_id.owner);
|
|
|
|
let region_scope_tree = &cx.tcx.region_scope_tree(def_id);
|
2019-06-02 11:58:11 -05:00
|
|
|
ExprUseVisitor::new(
|
|
|
|
&mut delegate,
|
|
|
|
cx.tcx,
|
|
|
|
def_id,
|
|
|
|
cx.param_env,
|
|
|
|
region_scope_tree,
|
|
|
|
cx.tables,
|
|
|
|
)
|
|
|
|
.walk_expr(expr);
|
2018-05-27 17:02:38 -05:00
|
|
|
|
|
|
|
if delegate.skip {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(delegate.used_mutably)
|
|
|
|
}
|
|
|
|
|
2019-06-19 13:36:23 -05:00
|
|
|
pub fn is_potentially_mutated<'a, 'tcx>(variable: &'tcx Path, expr: &'tcx Expr, cx: &'a LateContext<'a, '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)]
|
2018-07-23 06:01:12 -05:00
|
|
|
fn update(&mut self, cat: &'tcx Categorization<'_>) {
|
2018-05-27 17:02:38 -05:00
|
|
|
match *cat {
|
|
|
|
Categorization::Local(id) => {
|
|
|
|
self.used_mutably.insert(id);
|
|
|
|
},
|
|
|
|
Categorization::Upvar(_) => {
|
|
|
|
//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
|
|
|
|
},
|
|
|
|
Categorization::Deref(ref cmt, _) | Categorization::Interior(ref cmt, _) => self.update(&cmt.cat),
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
|
2019-11-28 09:09:02 -06:00
|
|
|
fn consume(&mut self, _: &Place<'tcx>, _: ConsumeMode) {}
|
2018-05-27 17:02:38 -05:00
|
|
|
|
2019-11-28 09:09:02 -06:00
|
|
|
fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) {
|
2018-05-27 17:02:38 -05:00
|
|
|
if let ty::BorrowKind::MutBorrow = bk {
|
|
|
|
self.update(&cmt.cat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-28 09:09:02 -06:00
|
|
|
fn mutate(&mut self, cmt: &Place<'tcx>) {
|
2018-05-27 17:02:38 -05:00
|
|
|
self.update(&cmt.cat)
|
|
|
|
}
|
|
|
|
}
|