2018-10-06 16:18:06 +00:00
|
|
|
// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2018-09-15 07:21:58 +00:00
|
|
|
use crate::rustc::lint::LateContext;
|
2018-05-27 22:02:38 +00:00
|
|
|
|
2018-09-15 07:21:58 +00:00
|
|
|
use crate::rustc::hir::def::Def;
|
|
|
|
use crate::rustc::hir::*;
|
|
|
|
use crate::rustc::middle::expr_use_visitor::*;
|
|
|
|
use crate::rustc::middle::mem_categorization::cmt_;
|
|
|
|
use crate::rustc::middle::mem_categorization::Categorization;
|
|
|
|
use crate::rustc::ty;
|
|
|
|
use crate::rustc_data_structures::fx::FxHashSet;
|
|
|
|
use crate::syntax::ast::NodeId;
|
|
|
|
use crate::syntax::source_map::Span;
|
2018-05-27 22:02:38 +00:00
|
|
|
|
|
|
|
/// Returns a set of mutated local variable ids or None if mutations could not be determined.
|
2018-09-11 23:34:52 +00:00
|
|
|
pub fn mutated_variables<'a, 'tcx: 'a>(expr: &'tcx Expr, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<NodeId>> {
|
2018-05-27 22:02:38 +00:00
|
|
|
let mut delegate = MutVarsDelegate {
|
2018-09-11 23:34:52 +00:00
|
|
|
used_mutably: FxHashSet::default(),
|
2018-05-27 22:02:38 +00: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);
|
|
|
|
ExprUseVisitor::new(&mut delegate, cx.tcx, cx.param_env, region_scope_tree, cx.tables, None).walk_expr(expr);
|
|
|
|
|
|
|
|
if delegate.skip {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
Some(delegate.used_mutably)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_potentially_mutated<'a, 'tcx: 'a>(
|
|
|
|
variable: &'tcx Path,
|
|
|
|
expr: &'tcx Expr,
|
|
|
|
cx: &'a LateContext<'a, 'tcx>,
|
|
|
|
) -> bool {
|
|
|
|
let id = match variable.def {
|
|
|
|
Def::Local(id) | Def::Upvar(id, ..) => id,
|
|
|
|
_ => return true,
|
|
|
|
};
|
|
|
|
mutated_variables(expr, cx).map_or(true, |mutated| mutated.contains(&id))
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MutVarsDelegate {
|
2018-09-11 23:34:52 +00:00
|
|
|
used_mutably: FxHashSet<NodeId>,
|
2018-05-27 22:02:38 +00:00
|
|
|
skip: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MutVarsDelegate {
|
2018-10-11 22:18:58 +00:00
|
|
|
#[allow(clippy::similar_names)]
|
2018-07-23 11:01:12 +00:00
|
|
|
fn update(&mut self, cat: &'tcx Categorization<'_>) {
|
2018-05-27 22:02:38 +00: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 {
|
|
|
|
fn consume(&mut self, _: NodeId, _: Span, _: &cmt_<'tcx>, _: ConsumeMode) {}
|
|
|
|
|
|
|
|
fn matched_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: MatchMode) {}
|
|
|
|
|
|
|
|
fn consume_pat(&mut self, _: &Pat, _: &cmt_<'tcx>, _: ConsumeMode) {}
|
|
|
|
|
2018-07-23 11:01:12 +00:00
|
|
|
fn borrow(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, _: ty::Region<'_>, bk: ty::BorrowKind, _: LoanCause) {
|
2018-05-27 22:02:38 +00:00
|
|
|
if let ty::BorrowKind::MutBorrow = bk {
|
|
|
|
self.update(&cmt.cat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mutate(&mut self, _: NodeId, _: Span, cmt: &cmt_<'tcx>, _: MutateMode) {
|
|
|
|
self.update(&cmt.cat)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn decl_without_init(&mut self, _: NodeId, _: Span) {}
|
|
|
|
}
|