From 050d7fd30805dcc995cd99575af2ff4db3ae0b90 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 23 Mar 2016 14:50:47 +0100 Subject: [PATCH] fallout and tests --- src/booleans.rs | 6 +++--- src/non_expressive_names.rs | 2 +- tests/compile-fail/block_in_if_condition.rs | 1 + tests/compile-fail/booleans.rs | 24 +++++++++++++++++++++ tests/compile-fail/eq_op.rs | 5 +++-- 5 files changed, 32 insertions(+), 6 deletions(-) create mode 100644 tests/compile-fail/booleans.rs diff --git a/src/booleans.rs b/src/booleans.rs index 4369a05d2d8f..e9197cf85c69 100644 --- a/src/booleans.rs +++ b/src/booleans.rs @@ -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" } diff --git a/src/non_expressive_names.rs b/src/non_expressive_names.rs index d7cb6fc5d281..87373c7a0ded 100644 --- a/src/non_expressive_names.rs +++ b/src/non_expressive_names.rs @@ -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 diff --git a/tests/compile-fail/block_in_if_condition.rs b/tests/compile-fail/block_in_if_condition.rs index 19fa949fb84f..3d47fc74a113 100644 --- a/tests/compile-fail/block_in_if_condition.rs +++ b/tests/compile-fail/block_in_if_condition.rs @@ -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 { diff --git a/tests/compile-fail/booleans.rs b/tests/compile-fail/booleans.rs new file mode 100644 index 000000000000..ed989c0e84b0 --- /dev/null +++ b/tests/compile-fail/booleans.rs @@ -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; +} diff --git a/tests/compile-fail/eq_op.rs b/tests/compile-fail/eq_op.rs index bd76695e6ad9..d49dcc6f84be 100644 --- a/tests/compile-fail/eq_op.rs +++ b/tests/compile-fail/eq_op.rs @@ -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