From 371a195ccb59dae00a6e3cd8c8ee5e734b9d8149 Mon Sep 17 00:00:00 2001 From: Nikhil Shagrithaya Date: Fri, 30 Jun 2017 20:45:11 +0530 Subject: [PATCH] Refactored used_mut_nodes --- src/librustc/lint/builtin.rs | 9 +- src/librustc_borrowck/borrowck/check_loans.rs | 6 +- .../borrowck/gather_loans/mod.rs | 6 +- src/librustc_lint/lib.rs | 2 - src/librustc_lint/unused.rs | 91 ------------------- 5 files changed, 18 insertions(+), 96 deletions(-) diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index 811bf9776101d..8741d65986bf4 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -216,6 +216,12 @@ declare_lint! { "detects use of deprecated items" } +declare_lint! { + pub UNUSED_MUT, + Warn, + "detect mut variables which don't need to be mutable" +} + /// Does nothing as a lint pass, but registers some `Lint`s /// which are used by other parts of the compiler. #[derive(Copy, Clone)] @@ -256,7 +262,8 @@ impl LintPass for HardwiredLints { MISSING_FRAGMENT_SPECIFIER, PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES, LATE_BOUND_LIFETIME_ARGUMENTS, - DEPRECATED + DEPRECATED, + UNUSED_MUT ) } } diff --git a/src/librustc_borrowck/borrowck/check_loans.rs b/src/librustc_borrowck/borrowck/check_loans.rs index 7f31c53e63caf..27a00a03a568e 100644 --- a/src/librustc_borrowck/borrowck/check_loans.rs +++ b/src/librustc_borrowck/borrowck/check_loans.rs @@ -20,6 +20,7 @@ use self::UseError::*; use borrowck::*; use borrowck::InteriorKind::{InteriorElement, InteriorField}; +use rustc::lint::builtin::UNUSED_MUT; use rustc::middle::expr_use_visitor as euv; use rustc::middle::expr_use_visitor::MutateMode; use rustc::middle::mem_categorization as mc; @@ -847,7 +848,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> { let lp = opt_loan_path(&assignee_cmt).unwrap(); self.move_data.each_assignment_of(assignment_id, &lp, |assign| { if assignee_cmt.mutbl.is_mutable() { - self.tcx().used_mut_nodes.borrow_mut().insert(local_id); + self.bccx.tcx.lint_node(UNUSED_MUT, + local_id, + self.bccx.tcx.hir.span_if_local(local_id), + "unused mut variables"); } else { self.bccx.report_reassigned_immutable_variable( assignment_span, diff --git a/src/librustc_borrowck/borrowck/gather_loans/mod.rs b/src/librustc_borrowck/borrowck/gather_loans/mod.rs index 5689a30fd38c0..c641ebc2abd6a 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/mod.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/mod.rs @@ -17,6 +17,7 @@ // sure that all of these loans are honored. use borrowck::*; +use rustc::lint::builtin::UNUSED_MUT; use borrowck::move_data::MoveData; use rustc::middle::expr_use_visitor as euv; use rustc::middle::mem_categorization as mc; @@ -448,7 +449,10 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> { } LpUpvar(ty::UpvarId{ var_id, closure_expr_id: _ }) => { let local_id = self.tcx().hir.def_index_to_node_id(var_id); - self.tcx().used_mut_nodes.borrow_mut().insert(local_id); + self.bccx.tcx.lint_node(UNUSED_MUT, + local_id, + self.bccx.tcx.hir.span_if_local(local_id), + "unused mut variables"); None } LpExtend(ref base, mc::McInherited, LpDeref(pointer_kind)) | diff --git a/src/librustc_lint/lib.rs b/src/librustc_lint/lib.rs index 5ef277f02ace6..1c8691f7e0116 100644 --- a/src/librustc_lint/lib.rs +++ b/src/librustc_lint/lib.rs @@ -38,7 +38,6 @@ extern crate syntax; extern crate rustc; #[macro_use] extern crate log; -extern crate rustc_back; extern crate rustc_const_eval; extern crate syntax_pos; @@ -130,7 +129,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) { NonShorthandFieldPatterns, UnusedUnsafe, UnsafeCode, - UnusedMut, UnusedAllocation, MissingCopyImplementations, UnstableFeatures, diff --git a/src/librustc_lint/unused.rs b/src/librustc_lint/unused.rs index cbc4ebe90fd09..4641f61fd0d42 100644 --- a/src/librustc_lint/unused.rs +++ b/src/librustc_lint/unused.rs @@ -11,107 +11,16 @@ use rustc::hir::def_id::DefId; use rustc::ty; use rustc::ty::adjustment; -use util::nodemap::FxHashMap; use lint::{LateContext, EarlyContext, LintContext, LintArray}; use lint::{LintPass, EarlyLintPass, LateLintPass}; -use std::collections::hash_map::Entry::{Occupied, Vacant}; - use syntax::ast; use syntax::attr; use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType}; use syntax::symbol::keywords; -use syntax::ptr::P; use syntax_pos::Span; -use rustc_back::slice; use rustc::hir; -use rustc::hir::intravisit::FnKind; - -declare_lint! { - pub UNUSED_MUT, - Warn, - "detect mut variables which don't need to be mutable" -} - -#[derive(Copy, Clone)] -pub struct UnusedMut; - -impl UnusedMut { - fn check_unused_mut_pat(&self, cx: &LateContext, pats: &[P]) { - // collect all mutable pattern and group their NodeIDs by their Identifier to - // avoid false warnings in match arms with multiple patterns - - let mut mutables = FxHashMap(); - for p in pats { - p.each_binding(|_, id, span, path1| { - let hir_id = cx.tcx.hir.node_to_hir_id(id); - let bm = match cx.tables.pat_binding_modes().get(hir_id) { - Some(&bm) => bm, - None => span_bug!(span, "missing binding mode"), - }; - let name = path1.node; - if let ty::BindByValue(hir::MutMutable) = bm { - if !name.as_str().starts_with("_") { - match mutables.entry(name) { - Vacant(entry) => { - entry.insert(vec![id]); - } - Occupied(mut entry) => { - entry.get_mut().push(id); - } - } - } - } - }); - } - - let used_mutables = cx.tcx.used_mut_nodes.borrow(); - for (_, v) in &mutables { - if !v.iter().any(|e| used_mutables.contains(e)) { - cx.span_lint(UNUSED_MUT, - cx.tcx.hir.span(v[0]), - "variable does not need to be mutable"); - } - } - } -} - -impl LintPass for UnusedMut { - fn get_lints(&self) -> LintArray { - lint_array!(UNUSED_MUT) - } -} - -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedMut { - fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) { - if let hir::ExprMatch(_, ref arms, _) = e.node { - for a in arms { - self.check_unused_mut_pat(cx, &a.pats) - } - } - } - - fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) { - if let hir::StmtDecl(ref d, _) = s.node { - if let hir::DeclLocal(ref l) = d.node { - self.check_unused_mut_pat(cx, slice::ref_slice(&l.pat)); - } - } - } - - fn check_fn(&mut self, - cx: &LateContext, - _: FnKind, - _: &hir::FnDecl, - body: &hir::Body, - _: Span, - _: ast::NodeId) { - for a in &body.arguments { - self.check_unused_mut_pat(cx, slice::ref_slice(&a.pat)); - } - } -} declare_lint! { pub UNUSED_MUST_USE,