rust/clippy_lints/src/utils/usage.rs

109 lines
3.1 KiB
Rust
Raw Normal View History

2020-01-10 10:14:17 -06:00
use crate::utils::match_var;
use rustc::hir::map::Map;
use rustc::ty;
2020-02-29 21:23:33 -06:00
use rustc_ast::ast;
use rustc_data_structures::fx::FxHashSet;
2020-01-06 10:39:50 -06:00
use rustc_hir::def::Res;
2020-01-10 10:14:17 -06:00
use rustc_hir::intravisit::{walk_expr, NestedVisitorMap, Visitor};
2020-02-21 02:39:38 -06:00
use rustc_hir::{def_id, Expr, 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-01-10 10:14:17 -06:00
use rustc_span::symbol::Ident;
2020-02-21 02:39:38 -06:00
use rustc_typeck::expr_use_visitor::{ConsumeMode, Delegate, ExprUseVisitor, Place, PlaceBase};
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-12-27 01:12:26 -06:00
pub fn mutated_variables<'a, 'tcx>(expr: &'tcx Expr<'_>, cx: &'a LateContext<'a, 'tcx>) -> Option<FxHashSet<HirId>> {
let mut delegate = MutVarsDelegate {
used_mutably: FxHashSet::default(),
skip: false,
};
let def_id = def_id::DefId::local(expr.hir_id.owner);
2019-11-29 04:12:19 -06:00
cx.tcx.infer_ctxt().enter(|infcx| {
ExprUseVisitor::new(&mut delegate, &infcx, def_id, cx.param_env, cx.tables).walk_expr(expr);
});
if delegate.skip {
return None;
}
Some(delegate.used_mutably)
}
2019-12-27 01:12:26 -06:00
pub fn is_potentially_mutated<'a, 'tcx>(
2019-12-29 22:02:10 -06:00
variable: &'tcx Path<'_>,
2019-12-27 01:12:26 -06:00
expr: &'tcx Expr<'_>,
cx: &'a LateContext<'a, 'tcx>,
) -> bool {
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
}
}
struct MutVarsDelegate {
2019-03-01 06:26:06 -06:00
used_mutably: FxHashSet<HirId>,
skip: bool,
}
impl<'tcx> MutVarsDelegate {
#[allow(clippy::similar_names)]
2019-11-28 09:33:12 -06:00
fn update(&mut self, cat: &Place<'tcx>) {
match cat.base {
PlaceBase::Local(id) => {
self.used_mutably.insert(id);
},
2019-11-28 09:33:12 -06:00
PlaceBase::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
},
_ => {},
}
}
}
impl<'tcx> Delegate<'tcx> for MutVarsDelegate {
2019-11-28 09:09:02 -06:00
fn consume(&mut self, _: &Place<'tcx>, _: ConsumeMode) {}
2019-11-28 09:09:02 -06:00
fn borrow(&mut self, cmt: &Place<'tcx>, bk: ty::BorrowKind) {
if let ty::BorrowKind::MutBorrow = bk {
2019-11-28 09:33:12 -06:00
self.update(&cmt)
}
}
2019-11-28 09:09:02 -06:00
fn mutate(&mut self, cmt: &Place<'tcx>) {
2019-11-28 09:33:12 -06:00
self.update(&cmt)
}
}
2020-01-10 10:14:17 -06: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> {
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
}