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.
|
|
|
|
|
2016-03-22 17:30:57 +02:00
|
|
|
use rustc::ty;
|
|
|
|
use rustc::ty::adjustment;
|
2016-11-08 14:02:55 +11:00
|
|
|
use util::nodemap::FxHashMap;
|
2015-09-15 18:58:19 -04:00
|
|
|
use lint::{LateContext, EarlyContext, LintContext, LintArray};
|
|
|
|
use lint::{LintPass, EarlyLintPass, LateLintPass};
|
|
|
|
|
|
|
|
use std::collections::hash_map::Entry::{Occupied, Vacant};
|
|
|
|
|
|
|
|
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};
|
2016-11-16 08:21:52 +00:00
|
|
|
use syntax::symbol::keywords;
|
2015-09-15 18:58:19 -04:00
|
|
|
use syntax::ptr::P;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2015-09-15 18:58:19 -04:00
|
|
|
|
std: Stabilize library APIs for 1.5
This commit stabilizes and deprecates library APIs whose FCP has closed in the
last cycle, specifically:
Stabilized APIs:
* `fs::canonicalize`
* `Path::{metadata, symlink_metadata, canonicalize, read_link, read_dir, exists,
is_file, is_dir}` - all moved to inherent methods from the `PathExt` trait.
* `Formatter::fill`
* `Formatter::width`
* `Formatter::precision`
* `Formatter::sign_plus`
* `Formatter::sign_minus`
* `Formatter::alternate`
* `Formatter::sign_aware_zero_pad`
* `string::ParseError`
* `Utf8Error::valid_up_to`
* `Iterator::{cmp, partial_cmp, eq, ne, lt, le, gt, ge}`
* `<[T]>::split_{first,last}{,_mut}`
* `Condvar::wait_timeout` - note that `wait_timeout_ms` is not yet deprecated
but will be once 1.5 is released.
* `str::{R,}MatchIndices`
* `str::{r,}match_indices`
* `char::from_u32_unchecked`
* `VecDeque::insert`
* `VecDeque::shrink_to_fit`
* `VecDeque::as_slices`
* `VecDeque::as_mut_slices`
* `VecDeque::swap_remove_front` - (renamed from `swap_front_remove`)
* `VecDeque::swap_remove_back` - (renamed from `swap_back_remove`)
* `Vec::resize`
* `str::slice_mut_unchecked`
* `FileTypeExt`
* `FileTypeExt::{is_block_device, is_char_device, is_fifo, is_socket}`
* `BinaryHeap::from` - `from_vec` deprecated in favor of this
* `BinaryHeap::into_vec` - plus a `Into` impl
* `BinaryHeap::into_sorted_vec`
Deprecated APIs
* `slice::ref_slice`
* `slice::mut_ref_slice`
* `iter::{range_inclusive, RangeInclusive}`
* `std::dynamic_lib`
Closes #27706
Closes #27725
cc #27726 (align not stabilized yet)
Closes #27734
Closes #27737
Closes #27742
Closes #27743
Closes #27772
Closes #27774
Closes #27777
Closes #27781
cc #27788 (a few remaining methods though)
Closes #27790
Closes #27793
Closes #27796
Closes #27810
cc #28147 (not all parts stabilized)
2015-10-22 16:28:45 -07:00
|
|
|
use rustc_back::slice;
|
2016-03-29 08:50:44 +03:00
|
|
|
use rustc::hir;
|
|
|
|
use rustc::hir::intravisit::FnKind;
|
2015-09-15 18:58:19 -04:00
|
|
|
|
|
|
|
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<hir::Pat>]) {
|
|
|
|
// collect all mutable pattern and group their NodeIDs by their Identifier to
|
|
|
|
// avoid false warnings in match arms with multiple patterns
|
|
|
|
|
2016-11-08 14:02:55 +11:00
|
|
|
let mut mutables = FxHashMap();
|
2015-09-15 18:58:19 -04:00
|
|
|
for p in pats {
|
2016-11-25 13:21:19 +02:00
|
|
|
p.each_binding(|mode, id, _, path1| {
|
2015-09-23 20:04:49 +03:00
|
|
|
let name = path1.node;
|
2015-09-15 18:58:19 -04:00
|
|
|
if let hir::BindByValue(hir::MutMutable) = mode {
|
2015-09-23 20:04:49 +03:00
|
|
|
if !name.as_str().starts_with("_") {
|
2016-11-16 08:21:52 +00:00
|
|
|
match mutables.entry(name) {
|
2016-10-09 09:38:07 +05:30
|
|
|
Vacant(entry) => {
|
|
|
|
entry.insert(vec![id]);
|
|
|
|
}
|
|
|
|
Occupied(mut entry) => {
|
|
|
|
entry.get_mut().push(id);
|
|
|
|
}
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let used_mutables = cx.tcx.used_mut_nodes.borrow();
|
|
|
|
for (_, v) in &mutables {
|
|
|
|
if !v.iter().any(|e| used_mutables.contains(e)) {
|
2016-10-09 09:38:07 +05:30
|
|
|
cx.span_lint(UNUSED_MUT,
|
2017-01-26 02:41:06 +02:00
|
|
|
cx.tcx.hir.span(v[0]),
|
2015-09-15 18:58:19 -04:00
|
|
|
"variable does not need to be mutable");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LintPass for UnusedMut {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_MUT)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedMut {
|
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
|
|
|
if let hir::ExprMatch(_, ref arms, _) = e.node {
|
|
|
|
for a in arms {
|
|
|
|
self.check_unused_mut_pat(cx, &a.pats)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
fn check_fn(&mut self,
|
2016-12-07 13:56:36 +01:00
|
|
|
cx: &LateContext,
|
|
|
|
_: FnKind,
|
2016-12-20 22:46:11 +02:00
|
|
|
_: &hir::FnDecl,
|
|
|
|
body: &hir::Body,
|
2016-12-07 13:56:36 +01:00
|
|
|
_: Span,
|
|
|
|
_: ast::NodeId) {
|
2016-12-20 22:46:11 +02:00
|
|
|
for a in &body.arguments {
|
2015-09-15 18:58:19 -04:00
|
|
|
self.check_unused_mut_pat(cx, slice::ref_slice(&a.pat));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2015-09-15 18:58:19 -04:00
|
|
|
let warned = match t.sty {
|
2017-01-11 15:58:37 +08:00
|
|
|
ty::TyTuple(ref tys, _) if tys.is_empty() => return,
|
2016-08-02 15:56:20 +08:00
|
|
|
ty::TyNever => return,
|
2015-09-15 18:58:19 -04:00
|
|
|
ty::TyBool => return,
|
2016-09-06 01:26:02 +03:00
|
|
|
ty::TyAdt(def, _) => {
|
2015-11-20 14:51:18 +02:00
|
|
|
let attrs = cx.tcx.get_attrs(def.did);
|
2017-03-24 09:31:26 +01:00
|
|
|
check_must_use(cx, &attrs, s.span)
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if !warned {
|
|
|
|
cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_must_use(cx: &LateContext, attrs: &[ast::Attribute], sp: Span) -> bool {
|
|
|
|
for attr in attrs {
|
|
|
|
if attr.check_name("must_use") {
|
|
|
|
let mut msg = "unused result which must be used".to_string();
|
|
|
|
// 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 UNUSED_UNSAFE,
|
|
|
|
Warn,
|
|
|
|
"unnecessary use of an `unsafe` block"
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub struct UnusedUnsafe;
|
|
|
|
|
|
|
|
impl LintPass for UnusedUnsafe {
|
|
|
|
fn get_lints(&self) -> LintArray {
|
|
|
|
lint_array!(UNUSED_UNSAFE)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-07 13:14:47 +01:00
|
|
|
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UnusedUnsafe {
|
2016-12-07 13:56:36 +01:00
|
|
|
fn check_expr(&mut self, cx: &LateContext, e: &hir::Expr) {
|
2017-01-19 23:17:48 -08:00
|
|
|
/// Return the NodeId for an enclosing scope that is also `unsafe`
|
|
|
|
fn is_enclosed(cx: &LateContext, id: ast::NodeId) -> Option<(String, ast::NodeId)> {
|
|
|
|
let parent_id = cx.tcx.hir.get_parent_node(id);
|
|
|
|
if parent_id != id {
|
|
|
|
if cx.tcx.used_unsafe.borrow().contains(&parent_id) {
|
|
|
|
Some(("block".to_string(), parent_id))
|
|
|
|
} else if let Some(hir::map::NodeItem(&hir::Item {
|
|
|
|
node: hir::ItemFn(_, hir::Unsafety::Unsafe, _, _, _, _),
|
|
|
|
..
|
|
|
|
})) = cx.tcx.hir.find(parent_id) {
|
|
|
|
Some(("fn".to_string(), parent_id))
|
|
|
|
} else {
|
|
|
|
is_enclosed(cx, parent_id)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2015-09-15 18:58:19 -04:00
|
|
|
if let hir::ExprBlock(ref blk) = e.node {
|
|
|
|
// Don't warn about generated blocks, that'll just pollute the output.
|
|
|
|
if blk.rules == hir::UnsafeBlock(hir::UserProvided) &&
|
2016-10-09 09:38:07 +05:30
|
|
|
!cx.tcx.used_unsafe.borrow().contains(&blk.id) {
|
2017-01-19 23:17:48 -08:00
|
|
|
|
|
|
|
let mut db = cx.struct_span_lint(UNUSED_UNSAFE, blk.span,
|
|
|
|
"unnecessary `unsafe` block");
|
|
|
|
|
2017-05-04 14:17:23 +02:00
|
|
|
db.span_label(blk.span, "unnecessary `unsafe` block");
|
2017-01-19 23:17:48 -08:00
|
|
|
if let Some((kind, id)) = is_enclosed(cx, blk.id) {
|
|
|
|
db.span_note(cx.tcx.hir.span(id),
|
|
|
|
&format!("because it's nested under this `unsafe` {}", kind));
|
|
|
|
}
|
|
|
|
db.emit();
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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! {
|
|
|
|
UNUSED_PARENS,
|
|
|
|
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 {
|
2016-02-09 21:39:09 +01:00
|
|
|
let necessary = struct_lit_needs_parens && contains_exterior_struct_lit(&inner);
|
2015-09-15 18:58:19 -04:00
|
|
|
if !necessary {
|
2016-10-09 09:38:07 +05:30
|
|
|
cx.span_lint(UNUSED_PARENS,
|
|
|
|
value.span,
|
2015-09-15 18:58:19 -04:00
|
|
|
&format!("unnecessary parentheses around {}", msg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Expressions that syntactically contain an "exterior" struct
|
|
|
|
/// literal i.e. not surrounded by any parens or other
|
|
|
|
/// delimiters, e.g. `X { y: 1 }`, `X { y: 1 }.method()`, `foo
|
|
|
|
/// == X { y: 1 }` and `X { y: 1 } == foo` all do, but `(X {
|
|
|
|
/// y: 1 }) == foo` does not.
|
|
|
|
fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
|
|
|
|
match value.node {
|
2016-02-08 16:05:05 +01:00
|
|
|
ast::ExprKind::Struct(..) => true,
|
2015-09-15 18:58:19 -04:00
|
|
|
|
2016-02-08 16:05:05 +01:00
|
|
|
ast::ExprKind::Assign(ref lhs, ref rhs) |
|
|
|
|
ast::ExprKind::AssignOp(_, ref lhs, ref rhs) |
|
|
|
|
ast::ExprKind::Binary(_, ref lhs, ref rhs) => {
|
2015-09-15 18:58:19 -04:00
|
|
|
// X { y: 1 } + X { y: 2 }
|
2016-10-09 09:38:07 +05:30
|
|
|
contains_exterior_struct_lit(&lhs) || contains_exterior_struct_lit(&rhs)
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
2016-02-08 16:05:05 +01:00
|
|
|
ast::ExprKind::Unary(_, ref x) |
|
|
|
|
ast::ExprKind::Cast(ref x, _) |
|
|
|
|
ast::ExprKind::Type(ref x, _) |
|
|
|
|
ast::ExprKind::Field(ref x, _) |
|
|
|
|
ast::ExprKind::TupField(ref x, _) |
|
|
|
|
ast::ExprKind::Index(ref x, _) => {
|
2015-09-15 18:58:19 -04:00
|
|
|
// &X { y: 1 }, X { y: 1 }.y
|
2016-02-09 21:39:09 +01:00
|
|
|
contains_exterior_struct_lit(&x)
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
|
2016-08-26 19:23:42 +03:00
|
|
|
ast::ExprKind::MethodCall(.., ref exprs) => {
|
2015-09-15 18:58:19 -04:00
|
|
|
// X { y: 1 }.bar(...)
|
2016-02-09 21:39:09 +01:00
|
|
|
contains_exterior_struct_lit(&exprs[0])
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
|
2016-10-09 09:38:07 +05:30
|
|
|
_ => false,
|
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! {
|
|
|
|
UNUSED_ALLOCATION,
|
|
|
|
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-01-06 21:54:24 +02:00
|
|
|
if let Some(adjustment) = cx.tables.adjustments.get(&e.id) {
|
2016-10-20 06:33:20 +03:00
|
|
|
if let adjustment::Adjust::DerefRef { autoref, .. } = adjustment.kind {
|
2015-09-15 18:58:19 -04:00
|
|
|
match autoref {
|
2016-10-20 06:33:20 +03:00
|
|
|
Some(adjustment::AutoBorrow::Ref(_, hir::MutImmutable)) => {
|
2016-10-09 09:38:07 +05:30
|
|
|
cx.span_lint(UNUSED_ALLOCATION,
|
|
|
|
e.span,
|
2015-09-15 18:58:19 -04:00
|
|
|
"unnecessary allocation, use & instead");
|
|
|
|
}
|
2016-10-20 06:33:20 +03:00
|
|
|
Some(adjustment::AutoBorrow::Ref(_, hir::MutMutable)) => {
|
2016-10-09 09:38:07 +05:30
|
|
|
cx.span_lint(UNUSED_ALLOCATION,
|
|
|
|
e.span,
|
2015-09-15 18:58:19 -04:00
|
|
|
"unnecessary allocation, use &mut instead");
|
|
|
|
}
|
2016-10-09 09:38:07 +05:30
|
|
|
_ => (),
|
2015-09-15 18:58:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|