2015-09-15 18:58:19 -04:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2017-06-20 04:36:56 +09:00
|
|
|
use rustc::hir::def_id::DefId;
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty;
|
|
|
|
use rustc::ty::adjustment;
|
2015-09-15 18:58:19 -04:00
|
|
|
use lint::{LateContext, EarlyContext, LintContext, LintArray};
|
|
|
|
use lint::{LintPass, EarlyLintPass, LateLintPass};
|
|
|
|
|
|
|
|
use syntax::ast;
|
2016-08-23 03:54:53 +00:00
|
|
|
use syntax::attr;
|
2016-11-08 08:30:26 +10:30
|
|
|
use syntax::feature_gate::{BUILTIN_ATTRIBUTES, AttributeType};
|
2017-09-29 23:50:34 -07:00
|
|
|
use syntax::print::pprust;
|
2017-09-15 12:49:10 -07:00
|
|
|
use syntax::symbol::keywords;
|
2017-07-20 16:53:56 -04:00
|
|
|
use syntax::util::parser;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2015-09-15 18:58:19 -04:00
|
|
|
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir;
|
2015-09-15 18:58:19 -04:00
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_MUST_USE,
|
|
|
|
Warn,
|
|
|
|
"unused result of a type flagged as #[must_use]"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub UNUSED_RESULTS,
|
|
|
|
Allow,
|
|
|
|
"unused result of an expression in a statement"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct UnusedResults;
|
|
|
|
|
|
|
|
impl LintPass for UnusedResults {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_MUST_USE, UNUSED_RESULTS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedResults {
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
|
2015-09-15 18:58:19 -04:00
|
|
|
let expr = match s.node {
|
|
|
|
hir::StmtSemi(ref expr, _) => &**expr,
|
2016-10-09 09:38:07 +05:30
|
|
|
_ => return,
|
2015-09-15 18:58:19 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
if let hir::ExprRet(..) = expr.node {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-06 21:54:24 +02:00
|
|
|
let t = cx.tables.expr_ty(&expr);
|
2017-08-07 17:20:19 -07:00
|
|
|
let ty_warned = match t.sty {
|
2017-08-11 13:43:33 -07:00
|
|
|
ty::TyTuple(ref tys, _) if tys.is_empty() => return,
|
|
|
|
ty::TyNever => return,
|
2017-08-11 21:52:16 -07:00
|
|
|
ty::TyAdt(def, _) => {
|
|
|
|
if def.variants.is_empty() {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
check_must_use(cx, def.did, s.span, "")
|
|
|
|
}
|
|
|
|
},
|
2015-09-15 18:58:19 -04:00
|
|
|
_ => false,
|
|
|
|
};
|
2017-08-07 17:20:19 -07:00
|
|
|
|
|
|
|
let mut fn_warned = false;
|
2017-09-22 15:45:47 -07:00
|
|
|
let mut op_warned = false;
|
2017-08-15 16:21:28 -07:00
|
|
|
if cx.tcx.sess.features.borrow().fn_must_use {
|
|
|
|
let maybe_def = match expr.node {
|
|
|
|
hir::ExprCall(ref callee, _) => {
|
|
|
|
match callee.node {
|
|
|
|
hir::ExprPath(ref qpath) => {
|
|
|
|
Some(cx.tables.qpath_def(qpath, callee.hir_id))
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
hir::ExprMethodCall(..) => {
|
|
|
|
cx.tables.type_dependent_defs().get(expr.hir_id).cloned()
|
|
|
|
},
|
|
|
|
_ => None
|
|
|
|
};
|
|
|
|
if let Some(def) = maybe_def {
|
|
|
|
let def_id = def.def_id();
|
|
|
|
fn_warned = check_must_use(cx, def_id, s.span, "return value of ");
|
|
|
|
}
|
2017-09-22 15:45:47 -07:00
|
|
|
|
|
|
|
if let hir::ExprBinary(bin_op, ..) = expr.node {
|
|
|
|
match bin_op.node {
|
|
|
|
// Hardcoding the comparison operators here seemed more
|
|
|
|
// expedient than the refactoring that would be needed to
|
|
|
|
// look up the `#[must_use]` attribute which does exist on
|
|
|
|
// the comparison trait methods
|
|
|
|
hir::BiEq | hir::BiLt | hir::BiLe | hir::BiNe | hir::BiGe | hir::BiGt => {
|
|
|
|
let msg = "unused comparison which must be used";
|
|
|
|
cx.span_lint(UNUSED_MUST_USE, expr.span, msg);
|
|
|
|
op_warned = true;
|
|
|
|
},
|
|
|
|
_ => {},
|
|
|
|
}
|
|
|
|
}
|
2017-08-07 17:20:19 -07:00
|
|
|
}
|
|
|
|
|
2017-09-22 15:45:47 -07:00
|
|
|
if !(ty_warned || fn_warned || op_warned) {
|
2015-09-15 18:58:19 -04:00
|
|
|
cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
|
|
|
|
}
|
|
|
|
|
2017-08-07 17:20:19 -07:00
|
|
|
fn check_must_use(cx: &LateContext, def_id: DefId, sp: Span, describe_path: &str) -> bool {
|
2017-06-20 04:36:56 +09:00
|
|
|
for attr in cx.tcx.get_attrs(def_id).iter() {
|
2015-09-15 18:58:19 -04:00
|
|
|
if attr.check_name("must_use") {
|
2017-08-07 17:20:19 -07:00
|
|
|
let mut msg = format!("unused {}`{}` which must be used",
|
|
|
|
describe_path, cx.tcx.item_path_str(def_id));
|
2015-09-15 18:58:19 -04:00
|
|
|
// check for #[must_use="..."]
|
2016-07-03 14:38:37 -07:00
|
|
|
if let Some(s) = attr.value_str() {
|
|
|
|
msg.push_str(": ");
|
2016-11-16 10:52:37 +00:00
|
|
|
msg.push_str(&s.as_str());
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
cx.span_lint(UNUSED_MUST_USE, sp, &msg);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
pub PATH_STATEMENTS,
|
|
|
|
Warn,
|
|
|
|
"path statements with no effect"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct PathStatements;
|
|
|
|
|
|
|
|
impl LintPass for PathStatements {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(PATH_STATEMENTS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PathStatements {
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
|
2015-10-28 01:08:46 +09:00
|
|
|
if let hir::StmtSemi(ref expr, _) = s.node {
|
2016-10-27 05:17:42 +03:00
|
|
|
if let hir::ExprPath(_) = expr.node {
|
2016-10-09 09:38:07 +05:30
|
|
|
cx.span_lint(PATH_STATEMENTS, s.span, "path statement with no effect");
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
2015-09-29 16:44:26 +01:00
|
|
|
pub UNUSED_ATTRIBUTES,
|
2015-09-15 18:58:19 -04:00
|
|
|
Warn,
|
|
|
|
"detects attributes that were not used by the compiler"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct UnusedAttributes;
|
|
|
|
|
|
|
|
impl LintPass for UnusedAttributes {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_ATTRIBUTES)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAttributes {
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_attribute(&mut self, cx: &LateContext, attr: &ast::Attribute) {
|
2016-08-19 18:58:14 -07:00
|
|
|
debug!("checking attribute: {:?}", attr);
|
2017-03-03 09:23:59 +00:00
|
|
|
let name = unwrap_or!(attr.name(), return);
|
2016-08-19 18:58:14 -07:00
|
|
|
|
2015-09-15 18:58:19 -04:00
|
|
|
// Note that check_name() marks the attribute as used if it matches.
|
2016-11-08 08:30:26 +10:30
|
|
|
for &(ref name, ty, _) in BUILTIN_ATTRIBUTES {
|
2015-09-15 18:58:19 -04:00
|
|
|
match ty {
|
|
|
|
AttributeType::Whitelisted if attr.check_name(name) => {
|
2016-08-19 18:58:14 -07:00
|
|
|
debug!("{:?} is Whitelisted", name);
|
2015-09-15 18:58:19 -04:00
|
|
|
break;
|
2016-10-09 09:38:07 +05:30
|
|
|
}
|
|
|
|
_ => (),
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let plugin_attributes = cx.sess().plugin_attributes.borrow_mut();
|
|
|
|
for &(ref name, ty) in plugin_attributes.iter() {
|
2016-02-09 21:39:09 +01:00
|
|
|
if ty == AttributeType::Whitelisted && attr.check_name(&name) {
|
2016-08-19 18:58:14 -07:00
|
|
|
debug!("{:?} (plugin attr) is whitelisted with ty {:?}", name, ty);
|
2015-09-15 18:58:19 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !attr::is_used(attr) {
|
2016-08-19 18:58:14 -07:00
|
|
|
debug!("Emitting warning for: {:?}", attr);
|
2015-09-15 18:58:19 -04:00
|
|
|
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, "unused attribute");
|
|
|
|
// Is it a builtin attribute that must be used at the crate level?
|
2016-11-08 08:30:26 +10:30
|
|
|
let known_crate = BUILTIN_ATTRIBUTES.iter()
|
2017-03-03 09:23:59 +00:00
|
|
|
.find(|&&(builtin, ty, _)| name == builtin && ty == AttributeType::CrateLevel)
|
2016-10-09 09:38:07 +05:30
|
|
|
.is_some();
|
2015-09-15 18:58:19 -04:00
|
|
|
|
|
|
|
// Has a plugin registered this attribute as one which must be used at
|
|
|
|
// the crate level?
|
|
|
|
let plugin_crate = plugin_attributes.iter()
|
2017-03-03 09:23:59 +00:00
|
|
|
.find(|&&(ref x, t)| name == &**x && AttributeType::CrateLevel == t)
|
2016-10-09 09:38:07 +05:30
|
|
|
.is_some();
|
|
|
|
if known_crate || plugin_crate {
|
2016-11-14 12:00:25 +00:00
|
|
|
let msg = match attr.style {
|
2016-10-09 09:38:07 +05:30
|
|
|
ast::AttrStyle::Outer => {
|
|
|
|
"crate-level attribute should be an inner attribute: add an exclamation \
|
|
|
|
mark: #![foo]"
|
|
|
|
}
|
|
|
|
ast::AttrStyle::Inner => "crate-level attribute should be in the root module",
|
2015-09-15 18:58:19 -04:00
|
|
|
};
|
|
|
|
cx.span_lint(UNUSED_ATTRIBUTES, attr.span, msg);
|
|
|
|
}
|
2016-08-19 18:58:14 -07:00
|
|
|
} else {
|
|
|
|
debug!("Attr was used: {:?}", attr);
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
2017-10-21 00:00:57 +03:00
|
|
|
pub(super) UNUSED_PARENS,
|
2015-09-15 18:58:19 -04:00
|
|
|
Warn,
|
|
|
|
"`if`, `match`, `while` and `return` do not need parentheses"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct UnusedParens;
|
|
|
|
|
|
|
|
impl UnusedParens {
|
2016-10-09 09:38:07 +05:30
|
|
|
fn check_unused_parens_core(&self,
|
|
|
|
cx: &EarlyContext,
|
|
|
|
value: &ast::Expr,
|
|
|
|
msg: &str,
|
2015-09-15 18:58:19 -04:00
|
|
|
struct_lit_needs_parens: bool) {
|
2016-02-08 16:05:05 +01:00
|
|
|
if let ast::ExprKind::Paren(ref inner) = value.node {
|
2017-07-20 16:53:56 -04:00
|
|
|
let necessary = struct_lit_needs_parens &&
|
|
|
|
parser::contains_exterior_struct_lit(&inner);
|
2015-09-15 18:58:19 -04:00
|
|
|
if !necessary {
|
2017-09-29 23:50:34 -07:00
|
|
|
let span_msg = format!("unnecessary parentheses around {}", msg);
|
|
|
|
let mut err = cx.struct_span_lint(UNUSED_PARENS,
|
|
|
|
value.span,
|
|
|
|
&span_msg);
|
2017-09-30 00:53:26 -07:00
|
|
|
// Remove exactly one pair of parentheses (rather than naïvely
|
|
|
|
// stripping all paren characters)
|
|
|
|
let mut ate_left_paren = false;
|
|
|
|
let mut ate_right_paren = false;
|
2017-09-29 23:50:34 -07:00
|
|
|
let parens_removed = pprust::expr_to_string(value)
|
2017-09-30 00:53:26 -07:00
|
|
|
.trim_matches(|c| {
|
|
|
|
match c {
|
|
|
|
'(' => {
|
|
|
|
if ate_left_paren {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
ate_left_paren = true;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
')' => {
|
|
|
|
if ate_right_paren {
|
|
|
|
false
|
|
|
|
} else {
|
|
|
|
ate_right_paren = true;
|
|
|
|
true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}).to_owned();
|
2017-09-29 23:50:34 -07:00
|
|
|
err.span_suggestion_short(value.span,
|
|
|
|
"remove these parentheses",
|
|
|
|
parens_removed);
|
|
|
|
err.emit();
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for UnusedParens {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_PARENS)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl EarlyLintPass for UnusedParens {
|
|
|
|
fn check_expr(&mut self, cx: &EarlyContext, e: &ast::Expr) {
|
2016-02-08 16:05:05 +01:00
|
|
|
use syntax::ast::ExprKind::*;
|
2015-09-15 18:58:19 -04:00
|
|
|
let (value, msg, struct_lit_needs_parens) = match e.node {
|
2016-08-26 19:23:42 +03:00
|
|
|
If(ref cond, ..) => (cond, "`if` condition", true),
|
|
|
|
While(ref cond, ..) => (cond, "`while` condition", true),
|
|
|
|
IfLet(_, ref cond, ..) => (cond, "`if let` head expression", true),
|
|
|
|
WhileLet(_, ref cond, ..) => (cond, "`while let` head expression", true),
|
|
|
|
ForLoop(_, ref cond, ..) => (cond, "`for` head expression", true),
|
2016-02-08 16:05:05 +01:00
|
|
|
Match(ref head, _) => (head, "`match` head expression", true),
|
|
|
|
Ret(Some(ref value)) => (value, "`return` value", false),
|
|
|
|
Assign(_, ref value) => (value, "assigned value", false),
|
2016-08-26 19:23:42 +03:00
|
|
|
AssignOp(.., ref value) => (value, "assigned value", false),
|
2016-02-08 16:05:05 +01:00
|
|
|
InPlace(_, ref value) => (value, "emplacement value", false),
|
2016-10-09 09:38:07 +05:30
|
|
|
_ => return,
|
2015-09-15 18:58:19 -04:00
|
|
|
};
|
2016-02-09 21:39:09 +01:00
|
|
|
self.check_unused_parens_core(cx, &value, msg, struct_lit_needs_parens);
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn check_stmt(&mut self, cx: &EarlyContext, s: &ast::Stmt) {
|
|
|
|
let (value, msg) = match s.node {
|
2016-10-09 09:38:07 +05:30
|
|
|
ast::StmtKind::Local(ref local) => {
|
|
|
|
match local.init {
|
|
|
|
Some(ref value) => (value, "assigned value"),
|
|
|
|
None => return,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => return,
|
2015-09-15 18:58:19 -04:00
|
|
|
};
|
2016-02-09 21:39:09 +01:00
|
|
|
self.check_unused_parens_core(cx, &value, msg, false);
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
|
|
|
UNUSED_IMPORT_BRACES,
|
|
|
|
Allow,
|
|
|
|
"unnecessary braces around an imported item"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct UnusedImportBraces;
|
|
|
|
|
|
|
|
impl LintPass for UnusedImportBraces {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_IMPORT_BRACES)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-24 06:11:31 +02:00
|
|
|
impl EarlyLintPass for UnusedImportBraces {
|
|
|
|
fn check_item(&mut self, cx: &EarlyContext, item: &ast::Item) {
|
|
|
|
if let ast::ItemKind::Use(ref view_path) = item.node {
|
|
|
|
if let ast::ViewPathList(_, ref items) = view_path.node {
|
|
|
|
if items.len() == 1 && items[0].node.name.name != keywords::SelfValue.name() {
|
2016-08-12 09:15:02 +00:00
|
|
|
let msg = format!("braces around {} is unnecessary", items[0].node.name);
|
|
|
|
cx.span_lint(UNUSED_IMPORT_BRACES, item.span, &msg);
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_lint! {
|
2017-10-21 00:00:57 +03:00
|
|
|
pub(super) UNUSED_ALLOCATION,
|
2015-09-15 18:58:19 -04:00
|
|
|
Warn,
|
|
|
|
"detects unnecessary allocations that can be eliminated"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct UnusedAllocation;
|
|
|
|
|
|
|
|
impl LintPass for UnusedAllocation {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_ALLOCATION)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedAllocation {
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
|
2015-09-15 18:58:19 -04:00
|
|
|
match e.node {
|
2015-09-24 18:00:08 +03:00
|
|
|
hir::ExprBox(_) => {}
|
2016-10-09 09:38:07 +05:30
|
|
|
_ => return,
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
|
2017-05-27 10:29:24 +03:00
|
|
|
for adj in cx.tables.expr_adjustments(e) {
|
|
|
|
if let adjustment::Adjust::Borrow(adjustment::AutoBorrow::Ref(_, m)) = adj.kind {
|
|
|
|
let msg = match m {
|
|
|
|
hir::MutImmutable => "unnecessary allocation, use & instead",
|
|
|
|
hir::MutMutable => "unnecessary allocation, use &mut instead"
|
|
|
|
};
|
|
|
|
cx.span_lint(UNUSED_ALLOCATION, e.span, msg);
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|