new lint: double_neg

This commit is contained in:
Andre Bogus 2016-06-30 01:00:25 +02:00
parent d6e3fa8f49
commit a12e8394d7
5 changed files with 53 additions and 16 deletions

View file

@ -369,6 +369,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
misc::MODULO_ONE,
misc::REDUNDANT_PATTERN,
misc::TOPLEVEL_REF_ARG,
misc_early::DOUBLE_NEG,
misc_early::DUPLICATE_UNDERSCORE_ARGUMENT,
misc_early::REDUNDANT_CLOSURE_CALL,
misc_early::UNNEEDED_FIELD_PATTERN,

View file

@ -40,12 +40,25 @@ declare_lint! {
"Closures should not be called in the expression they are defined"
}
/// **What it does:** This lint detects expressions of the form `--x`
///
/// **Why is this bad?** It can mislead C/C++ programmers to think `x` was decremented.
///
/// **Known problems:** None.
///
/// **Example:** `--x;`
declare_lint! {
pub DOUBLE_NEG, Warn,
"--x is a double negation of x and not a pre-decrement as in C or C++"
}
#[derive(Copy, Clone)]
pub struct MiscEarly;
impl LintPass for MiscEarly {
fn get_lints(&self) -> LintArray {
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL)
lint_array!(UNNEEDED_FIELD_PATTERN, DUPLICATE_UNDERSCORE_ARGUMENT, REDUNDANT_CLOSURE_CALL, DOUBLE_NEG)
}
}
@ -126,21 +139,32 @@ impl EarlyLintPass for MiscEarly {
}
fn check_expr(&mut self, cx: &EarlyContext, expr: &Expr) {
if let ExprKind::Call(ref paren, _) = expr.node {
if let ExprKind::Paren(ref closure) = paren.node {
if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
span_lint_and_then(cx,
REDUNDANT_CLOSURE_CALL,
expr.span,
"Try not to call a closure in the expression where it is declared.",
|db| {
if decl.inputs.is_empty() {
let hint = format!("{}", snippet(cx, block.span, ".."));
db.span_suggestion(expr.span, "Try doing something like: ", hint);
}
});
match expr.node {
ExprKind::Call(ref paren, _) => {
if let ExprKind::Paren(ref closure) = paren.node {
if let ExprKind::Closure(_, ref decl, ref block, _) = closure.node {
span_lint_and_then(cx,
REDUNDANT_CLOSURE_CALL,
expr.span,
"Try not to call a closure in the expression where it is declared.",
|db| {
if decl.inputs.is_empty() {
let hint = format!("{}", snippet(cx, block.span, ".."));
db.span_suggestion(expr.span, "Try doing something like: ", hint);
}
});
}
}
}
ExprKind::Unary(UnOp::Neg, ref inner) => {
if let ExprKind::Unary(UnOp::Neg, _) = inner.node {
span_lint(cx,
DOUBLE_NEG,
expr.span,
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op");
}
}
_ => ()
}
}