Skip to content

Commit 371a195

Browse files
committed
Refactored used_mut_nodes
1 parent cc1fc88 commit 371a195

File tree

5 files changed

+18
-96
lines changed

5 files changed

+18
-96
lines changed

src/librustc/lint/builtin.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ declare_lint! {
216216
"detects use of deprecated items"
217217
}
218218

219+
declare_lint! {
220+
pub UNUSED_MUT,
221+
Warn,
222+
"detect mut variables which don't need to be mutable"
223+
}
224+
219225
/// Does nothing as a lint pass, but registers some `Lint`s
220226
/// which are used by other parts of the compiler.
221227
#[derive(Copy, Clone)]
@@ -256,7 +262,8 @@ impl LintPass for HardwiredLints {
256262
MISSING_FRAGMENT_SPECIFIER,
257263
PARENTHESIZED_PARAMS_IN_TYPES_AND_MODULES,
258264
LATE_BOUND_LIFETIME_ARGUMENTS,
259-
DEPRECATED
265+
DEPRECATED,
266+
UNUSED_MUT
260267
)
261268
}
262269
}

src/librustc_borrowck/borrowck/check_loans.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use self::UseError::*;
2020

2121
use borrowck::*;
2222
use borrowck::InteriorKind::{InteriorElement, InteriorField};
23+
use rustc::lint::builtin::UNUSED_MUT;
2324
use rustc::middle::expr_use_visitor as euv;
2425
use rustc::middle::expr_use_visitor::MutateMode;
2526
use rustc::middle::mem_categorization as mc;
@@ -847,7 +848,10 @@ impl<'a, 'tcx> CheckLoanCtxt<'a, 'tcx> {
847848
let lp = opt_loan_path(&assignee_cmt).unwrap();
848849
self.move_data.each_assignment_of(assignment_id, &lp, |assign| {
849850
if assignee_cmt.mutbl.is_mutable() {
850-
self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
851+
self.bccx.tcx.lint_node(UNUSED_MUT,
852+
local_id,
853+
self.bccx.tcx.hir.span_if_local(local_id),
854+
"unused mut variables");
851855
} else {
852856
self.bccx.report_reassigned_immutable_variable(
853857
assignment_span,

src/librustc_borrowck/borrowck/gather_loans/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// sure that all of these loans are honored.
1818

1919
use borrowck::*;
20+
use rustc::lint::builtin::UNUSED_MUT;
2021
use borrowck::move_data::MoveData;
2122
use rustc::middle::expr_use_visitor as euv;
2223
use rustc::middle::mem_categorization as mc;
@@ -448,7 +449,10 @@ impl<'a, 'tcx> GatherLoanCtxt<'a, 'tcx> {
448449
}
449450
LpUpvar(ty::UpvarId{ var_id, closure_expr_id: _ }) => {
450451
let local_id = self.tcx().hir.def_index_to_node_id(var_id);
451-
self.tcx().used_mut_nodes.borrow_mut().insert(local_id);
452+
self.bccx.tcx.lint_node(UNUSED_MUT,
453+
local_id,
454+
self.bccx.tcx.hir.span_if_local(local_id),
455+
"unused mut variables");
452456
None
453457
}
454458
LpExtend(ref base, mc::McInherited, LpDeref(pointer_kind)) |

src/librustc_lint/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ extern crate syntax;
3838
extern crate rustc;
3939
#[macro_use]
4040
extern crate log;
41-
extern crate rustc_back;
4241
extern crate rustc_const_eval;
4342
extern crate syntax_pos;
4443

@@ -130,7 +129,6 @@ pub fn register_builtins(store: &mut lint::LintStore, sess: Option<&Session>) {
130129
NonShorthandFieldPatterns,
131130
UnusedUnsafe,
132131
UnsafeCode,
133-
UnusedMut,
134132
UnusedAllocation,
135133
MissingCopyImplementations,
136134
UnstableFeatures,

src/librustc_lint/unused.rs

-91
Original file line numberDiff line numberDiff line change
@@ -11,107 +11,16 @@
1111
use rustc::hir::def_id::DefId;
1212
use rustc::ty;
1313
use rustc::ty::adjustment;
14-
use util::nodemap::FxHashMap;
1514
use lint::{LateContext, EarlyContext, LintContext, LintArray};
1615
use lint::{LintPass, EarlyLintPass, LateLintPass};
1716

18-
use std::collections::hash_map::Entry::{Occupied, Vacant};
19-
2017
use syntax::ast;
2118
use syntax::attr;
2219
use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType};
2320
use syntax::symbol::keywords;
24-
use syntax::ptr::P;
2521
use syntax_pos::Span;
2622

27-
use rustc_back::slice;
2823
use rustc::hir;
29-
use rustc::hir::intravisit::FnKind;
30-
31-
declare_lint! {
32-
pub UNUSED_MUT,
33-
Warn,
34-
"detect mut variables which don't need to be mutable"
35-
}
36-
37-
#[derive(Copy, Clone)]
38-
pub struct UnusedMut;
39-
40-
impl UnusedMut {
41-
fn check_unused_mut_pat(&self, cx: &LateContext, pats: &[P<hir::Pat>]) {
42-
// collect all mutable pattern and group their NodeIDs by their Identifier to
43-
// avoid false warnings in match arms with multiple patterns
44-
45-
let mut mutables = FxHashMap();
46-
for p in pats {
47-
p.each_binding(|_, id, span, path1| {
48-
let hir_id = cx.tcx.hir.node_to_hir_id(id);
49-
let bm = match cx.tables.pat_binding_modes().get(hir_id) {
50-
Some(&bm) => bm,
51-
None => span_bug!(span, "missing binding mode"),
52-
};
53-
let name = path1.node;
54-
if let ty::BindByValue(hir::MutMutable) = bm {
55-
if !name.as_str().starts_with("_") {
56-
match mutables.entry(name) {
57-
Vacant(entry) => {
58-
entry.insert(vec![id]);
59-
}
60-
Occupied(mut entry) => {
61-
entry.get_mut().push(id);
62-
}
63-
}
64-
}
65-
}
66-
});
67-
}
68-
69-
let used_mutables = cx.tcx.used_mut_nodes.borrow();
70-
for (_, v) in &mutables {
71-
if !v.iter().any(|e| used_mutables.contains(e)) {
72-
cx.span_lint(UNUSED_MUT,
73-
cx.tcx.hir.span(v[0]),
74-
"variable does not need to be mutable");
75-
}
76-
}
77-
}
78-
}
79-
80-
impl LintPass for UnusedMut {
81-
fn get_lints(&self) -> LintArray {
82-
lint_array!(UNUSED_MUT)
83-
}
84-
}
85-
86-
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedMut {
87-
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
88-
if let hir::ExprMatch(_, ref arms, _) = e.node {
89-
for a in arms {
90-
self.check_unused_mut_pat(cx, &a.pats)
91-
}
92-
}
93-
}
94-
95-
fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
96-
if let hir::StmtDecl(ref d, _) = s.node {
97-
if let hir::DeclLocal(ref l) = d.node {
98-
self.check_unused_mut_pat(cx, slice::ref_slice(&l.pat));
99-
}
100-
}
101-
}
102-
103-
fn check_fn(&mut self,
104-
cx: &LateContext,
105-
_: FnKind,
106-
_: &hir::FnDecl,
107-
body: &hir::Body,
108-
_: Span,
109-
_: ast::NodeId) {
110-
for a in &body.arguments {
111-
self.check_unused_mut_pat(cx, slice::ref_slice(&a.pat));
112-
}
113-
}
114-
}
11524

11625
declare_lint! {
11726
pub UNUSED_MUST_USE,

0 commit comments

Comments
 (0)