fallout and tests

This commit is contained in:
Oliver Schneider 2016-03-23 14:50:47 +01:00
parent 5911ccaba8
commit 050d7fd308
5 changed files with 32 additions and 6 deletions

View File

@ -2,17 +2,17 @@ use rustc::lint::*;
use rustc_front::hir::*;
use rustc_front::intravisit::*;
use syntax::ast::LitKind;
use utils::{span_lint_and_then, in_macro, snippet_opt};
use utils::{span_lint_and_then, in_macro, snippet_opt, SpanlessEq};
/// **What it does:** This lint checks for boolean expressions that can be written more concisely
///
/// **Why is this bad?** Readability of boolean expressions suffers from unnecesessary duplication
///
/// **Known problems:** None
/// **Known problems:** Ignores short circuting behavior, bitwise and/or and xor. Ends up suggesting things like !(a == b)
///
/// **Example:** `if a && b || a` should be `if a`
declare_lint! {
pub NONMINIMAL_BOOL, Warn,
pub NONMINIMAL_BOOL, Allow,
"checks for boolean expressions that can be written more concisely"
}

View File

@ -262,7 +262,7 @@ fn levenstein_not_1(a_name: &str, b_name: &str) -> bool {
}
if let Some(b2) = b_chars.next() {
// check if there's just one character inserted
return !(a == b2 && a_chars.eq(b_chars));
return a != b2 || a_chars.ne(b_chars);
} else {
// tuple
// ntuple

View File

@ -4,6 +4,7 @@
#![deny(block_in_if_condition_expr)]
#![deny(block_in_if_condition_stmt)]
#![allow(unused, let_and_return)]
#![warn(nonminimal_bool)]
macro_rules! blocky {

View File

@ -0,0 +1,24 @@
#![feature(plugin)]
#![plugin(clippy)]
#![deny(nonminimal_bool)]
#[allow(unused)]
fn main() {
let a: bool = unimplemented!();
let b: bool = unimplemented!();
let _ = a && b || a; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = a;
let _ = !(a && b); //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = !b || !a;
let _ = !true; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = false;
let _ = !false; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = true;
let _ = !!a; //~ ERROR this boolean expression can be simplified
//|~ HELP for further information visit
//|~ SUGGESTION let _ = a;
}

View File

@ -4,6 +4,7 @@
#[deny(eq_op)]
#[allow(identity_op)]
#[allow(no_effect)]
#[deny(nonminimal_bool)]
fn main() {
// simple values and comparisons
1 == 1; //~ERROR equal expressions
@ -38,9 +39,9 @@ fn main() {
1 - 1; //~ERROR equal expressions
1 / 1; //~ERROR equal expressions
true && true; //~ERROR equal expressions
//~|WARN this boolean expression can be simplified
//~|ERROR this boolean expression can be simplified
true || true; //~ERROR equal expressions
//~|WARN this boolean expression can be simplified
//~|ERROR this boolean expression can be simplified
let mut a = vec![1];
a == a; //~ERROR equal expressions