1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use rustc::hir::def_id::DefId;
13 use rustc::ty::adjustment;
14 use util::nodemap::FxHashMap;
15 use lint::{LateContext, EarlyContext, LintContext, LintArray};
16 use lint::{LintPass, EarlyLintPass, LateLintPass};
18 use std::collections::hash_map::Entry::{Occupied, Vacant};
22 use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType};
23 use syntax::symbol::keywords;
25 use syntax::util::parser;
28 use rustc_back::slice;
30 use rustc::hir::intravisit::FnKind;
35 "detect mut variables which don't need to be mutable"
38 #[derive(Copy, Clone)]
42 fn check_unused_mut_pat(&self, cx: &LateContext, pats: &[P<hir::Pat>]) {
43 // collect all mutable pattern and group their NodeIDs by their Identifier to
44 // avoid false warnings in match arms with multiple patterns
46 let mut mutables = FxHashMap();
48 p.each_binding(|_, id, span, path1| {
49 let hir_id = cx.tcx.hir.node_to_hir_id(id);
50 let bm = match cx.tables.pat_binding_modes().get(hir_id) {
52 None => span_bug!(span, "missing binding mode"),
54 let name = path1.node;
55 if let ty::BindByValue(hir::MutMutable) = bm {
56 if !name.as_str().starts_with("_") {
57 match mutables.entry(name) {
59 entry.insert(vec![id]);
61 Occupied(mut entry) => {
62 entry.get_mut().push(id);
70 let used_mutables = cx.tcx.used_mut_nodes.borrow();
71 for (_, v) in &mutables {
72 if !v.iter().any(|e| used_mutables.contains(e)) {
73 cx.span_lint(UNUSED_MUT,
74 cx.tcx.hir.span(v[0]),
75 "variable does not need to be mutable");
81 impl LintPass for UnusedMut {
82 fn get_lints(&self) -> LintArray {
83 lint_array!(UNUSED_MUT)
87 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedMut {
88 fn check_arm(&mut self, cx: &LateContext, a: &hir::Arm) {
89 self.check_unused_mut_pat(cx, &a.pats)
92 fn check_local(&mut self, cx: &LateContext, l: &hir::Local) {
93 self.check_unused_mut_pat(cx, slice::ref_slice(&l.pat));
96 fn check_fn(&mut self,
103 for a in &body.arguments {
104 self.check_unused_mut_pat(cx, slice::ref_slice(&a.pat));
112 "unused result of a type flagged as #[must_use]"
118 "unused result of an expression in a statement"
121 #[derive(Copy, Clone)]
122 pub struct UnusedResults;
124 impl LintPass for UnusedResults {
125 fn get_lints(&self) -> LintArray {
126 lint_array!(UNUSED_MUST_USE, UNUSED_RESULTS)
130 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
131 fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
132 let expr = match s.node {
133 hir::StmtSemi(ref expr, _) => &**expr,
137 if let hir::ExprRet(..) = expr.node {
141 let t = cx.tables.expr_ty(&expr);
142 let ty_warned = match t.sty {
143 ty::TyTuple(ref tys, _) if tys.is_empty() => return,
144 ty::TyNever => return,
145 ty::TyAdt(def, _) => {
146 if def.variants.is_empty() {
149 check_must_use(cx, def.did, s.span, "")
155 let mut fn_warned = false;
156 if cx.tcx.sess.features.borrow().fn_must_use {
157 let maybe_def = match expr.node {
158 hir::ExprCall(ref callee, _) => {
160 hir::ExprPath(ref qpath) => {
161 Some(cx.tables.qpath_def(qpath, callee.hir_id))
166 hir::ExprMethodCall(..) => {
167 cx.tables.type_dependent_defs().get(expr.hir_id).cloned()
171 if let Some(def) = maybe_def {
172 let def_id = def.def_id();
173 fn_warned = check_must_use(cx, def_id, s.span, "return value of ");
177 if !(ty_warned || fn_warned) {
178 cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
181 fn check_must_use(cx: &LateContext, def_id: DefId, sp: Span, describe_path: &str) -> bool {
182 for attr in cx.tcx.get_attrs(def_id).iter() {
183 if attr.check_name("must_use") {
184 let mut msg = format!("unused {}`{}` which must be used",
185 describe_path, cx.tcx.item_path_str(def_id));
186 // check for #[must_use="..."]
187 if let Some(s) = attr.value_str() {
189 msg.push_str(&s.as_str());
191 cx.span_lint(UNUSED_MUST_USE, sp, &msg);
203 "path statements with no effect"
206 #[derive(Copy, Clone)]
207 pub struct PathStatements;
209 impl LintPass for PathStatements {
210 fn get_lints(&self) -> LintArray {
211 lint_array!(PATH_STATEMENTS)
215 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathStatements {
216 fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
217 if let hir::StmtSemi(ref expr, _) = s.node {
218 if let hir::ExprPath(_) = expr.node {
219 cx.span_lint(PATH_STATEMENTS, s.span, "path statement with no effect");
226 pub UNUSED_ATTRIBUTES,
228 "detects attributes that were not used by the compiler"
231 #[derive(Copy, Clone)]
232 pub struct UnusedAttributes;
234 impl LintPass for UnusedAttributes {
235 fn get_lints(&self) -> LintArray {
236 lint_array!(UNUSED_ATTRIBUTES)
240 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
241 fn check_attribute(&mut self, cx: &LateContext, attr: &ast::Attribute) {
242 debug!("checking attribute: {:?}", attr);
243 let name = unwrap_or!(attr.name(), return);
245 // Note that check_name() marks the attribute as used if it matches.
246 for &(ref name, ty, _) in BUILTIN_ATTRIBUTES {
248 AttributeType::Whitelisted if attr.check_name(name) => {
249 debug!("{:?} is Whitelisted", name);
256 let plugin_attributes = cx.sess().plugin_attributes.borrow_mut();
257 for &(ref name, ty) in plugin_attributes.iter() {
258 if ty == AttributeType::Whitelisted && attr.check_name(&name) {
259 debug!("{:?} (plugin attr) is whitelisted with ty {:?}", name, ty);
264 if !attr::is_used(attr) {
265 debug!("Emitting warning for: {:?}", attr);
266 cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
267 // Is it a builtin attribute that must be used at the crate level?
268 let known_crate = BUILTIN_ATTRIBUTES.iter()
269 .find(|&&(builtin, ty, _)| name == builtin && ty == AttributeType::CrateLevel)
272 // Has a plugin registered this attribute as one which must be used at
274 let plugin_crate = plugin_attributes.iter()
275 .find(|&&(ref x, t)| name == &**x && AttributeType::CrateLevel == t)
277 if known_crate || plugin_crate {
278 let msg = match attr.style {
279 ast::AttrStyle::Outer => {
280 "crate-level attribute should be an inner attribute: add an exclamation \
283 ast::AttrStyle::Inner => "crate-level attribute should be in the root module",
285 cx.span_lint(UNUSED_ATTRIBUTES, attr.span, msg);
288 debug!("Attr was used: {:?}", attr);
296 "`if`, `match`, `while` and `return` do not need parentheses"
299 #[derive(Copy, Clone)]
300 pub struct UnusedParens;
303 fn check_unused_parens_core(&self,
307 struct_lit_needs_parens: bool) {
308 if let ast::ExprKind::Paren(ref inner) = value.node {
309 let necessary = struct_lit_needs_parens &&
310 parser::contains_exterior_struct_lit(&inner);
312 cx.span_lint(UNUSED_PARENS,
314 &format!("unnecessary parentheses around {}", msg))
320 impl LintPass for UnusedParens {
321 fn get_lints(&self) -> LintArray {
322 lint_array!(UNUSED_PARENS)
326 impl EarlyLintPass for UnusedParens {
327 fn check_expr(&mut self, cx: &EarlyContext, e: &ast::Expr) {
328 use syntax::ast::ExprKind::*;
329 let (value, msg, struct_lit_needs_parens) = match e.node {
330 If(ref cond, ..) => (cond, "`if` condition", true),
331 While(ref cond, ..) => (cond, "`while` condition", true),
332 IfLet(_, ref cond, ..) => (cond, "`if let` head expression", true),
333 WhileLet(_, ref cond, ..) => (cond, "`while let` head expression", true),
334 ForLoop(_, ref cond, ..) => (cond, "`for` head expression", true),
335 Match(ref head, _) => (head, "`match` head expression", true),
336 Ret(Some(ref value)) => (value, "`return` value", false),
337 Assign(_, ref value) => (value, "assigned value", false),
338 AssignOp(.., ref value) => (value, "assigned value", false),
339 InPlace(_, ref value) => (value, "emplacement value", false),
342 self.check_unused_parens_core(cx, &value, msg, struct_lit_needs_parens);
345 fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {
346 let (value, msg) = match s.node {
347 ast::StmtKind::Local(ref local) => {
349 Some(ref value) => (value, "assigned value"),
355 self.check_unused_parens_core(cx, &value, msg, false);
360 UNUSED_IMPORT_BRACES,
362 "unnecessary braces around an imported item"
365 #[derive(Copy, Clone)]
366 pub struct UnusedImportBraces;
368 impl LintPass for UnusedImportBraces {
369 fn get_lints(&self) -> LintArray {
370 lint_array!(UNUSED_IMPORT_BRACES)
374 impl EarlyLintPass for UnusedImportBraces {
375 fn check_item(&mut self, cx: &EarlyContext, item: &ast::Item) {
376 if let ast::ItemKind::Use(ref view_path) = item.node {
377 if let ast::ViewPathList(_, ref items) = view_path.node {
378 if items.len() == 1 && items[0].node.name.name != keywords::SelfValue.name() {
379 let msg = format!("braces around {} is unnecessary", items[0].node.name);
380 cx.span_lint(UNUSED_IMPORT_BRACES, item.span, &msg);
390 "detects unnecessary allocations that can be eliminated"
393 #[derive(Copy, Clone)]
394 pub struct UnusedAllocation;
396 impl LintPass for UnusedAllocation {
397 fn get_lints(&self) -> LintArray {
398 lint_array!(UNUSED_ALLOCATION)
402 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAllocation {
403 fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
405 hir::ExprBox(_) => {}
409 for adj in cx.tables.expr_adjustments(e) {
410 if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(_, m)) = adj.kind {
412 hir::MutImmutable => "unnecessary allocation, use & instead",
413 hir::MutMutable => "unnecessary allocation, use &mut instead"
415 cx.span_lint(UNUSED_ALLOCATION, e.span, msg);