New lint for statement with no effect
This commit is contained in:
parent
aee42f70f3
commit
0fe5981870
6 changed files with 107 additions and 1 deletions
|
|
@ -52,6 +52,7 @@ pub mod zero_div_zero;
|
|||
pub mod open_options;
|
||||
pub mod needless_features;
|
||||
pub mod needless_update;
|
||||
pub mod no_effect;
|
||||
|
||||
mod reexport {
|
||||
pub use syntax::ast::{Name, Ident, NodeId};
|
||||
|
|
@ -98,6 +99,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
reg.register_late_lint_pass(box mutex_atomic::MutexAtomic);
|
||||
reg.register_late_lint_pass(box needless_features::NeedlessFeaturesPass);
|
||||
reg.register_late_lint_pass(box needless_update::NeedlessUpdatePass);
|
||||
reg.register_late_lint_pass(box no_effect::NoEffectPass);
|
||||
|
||||
reg.register_lint_group("clippy_pedantic", vec![
|
||||
methods::OPTION_UNWRAP_USED,
|
||||
|
|
@ -159,6 +161,7 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
needless_features::UNSTABLE_AS_MUT_SLICE,
|
||||
needless_features::UNSTABLE_AS_SLICE,
|
||||
needless_update::NEEDLESS_UPDATE,
|
||||
no_effect::NO_EFFECT,
|
||||
open_options::NONSENSICAL_OPEN_OPTIONS,
|
||||
precedence::PRECEDENCE,
|
||||
ptr_arg::PTR_ARG,
|
||||
|
|
|
|||
61
src/no_effect.rs
Normal file
61
src/no_effect.rs
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
||||
use rustc::middle::def::{DefStruct, DefVariant};
|
||||
use rustc_front::hir::{Expr, ExprCall, ExprLit, ExprPath, ExprStruct};
|
||||
use rustc_front::hir::{Stmt, StmtSemi};
|
||||
|
||||
use utils::in_macro;
|
||||
use utils::span_lint;
|
||||
|
||||
declare_lint! {
|
||||
pub NO_EFFECT,
|
||||
Warn,
|
||||
"statements with no effect"
|
||||
}
|
||||
|
||||
fn has_no_effect(cx: &LateContext, expr: &Expr) -> bool {
|
||||
if in_macro(cx, expr.span) {
|
||||
return false;
|
||||
}
|
||||
match expr.node {
|
||||
ExprLit(..) |
|
||||
ExprPath(..) => true,
|
||||
ExprStruct(_, ref fields, ref base) => {
|
||||
fields.iter().all(|field| has_no_effect(cx, &field.expr)) &&
|
||||
match *base {
|
||||
Some(ref base) => has_no_effect(cx, base),
|
||||
None => true,
|
||||
}
|
||||
}
|
||||
ExprCall(ref callee, ref args) => {
|
||||
let def = cx.tcx.def_map.borrow().get(&callee.id).map(|d| d.full_def());
|
||||
match def {
|
||||
Some(DefStruct(..)) |
|
||||
Some(DefVariant(..)) => {
|
||||
args.iter().all(|arg| has_no_effect(cx, arg))
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct NoEffectPass;
|
||||
|
||||
impl LintPass for NoEffectPass {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(NO_EFFECT)
|
||||
}
|
||||
}
|
||||
|
||||
impl LateLintPass for NoEffectPass {
|
||||
fn check_stmt(&mut self, cx: &LateContext, stmt: &Stmt) {
|
||||
if let StmtSemi(ref expr, _) = stmt.node {
|
||||
if has_no_effect(cx, expr) {
|
||||
span_lint(cx, NO_EFFECT, stmt.span,
|
||||
"statement with no effect");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue