From 8a98736f51249befe9d4a4412619490f6cecfa54 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Thu, 13 Aug 2015 09:44:03 +0200 Subject: [PATCH] spelling fix, rework needless_bool with snippet (fixes #150) --- src/needless_bool.rs | 40 ++++++++++++++++++----------- tests/compile-fail/eq_op.rs | 2 +- tests/compile-fail/needless_bool.rs | 8 +++--- 3 files changed, 30 insertions(+), 20 deletions(-) diff --git a/src/needless_bool.rs b/src/needless_bool.rs index 6a4a55aeda5..2a4ed50b93d 100644 --- a/src/needless_bool.rs +++ b/src/needless_bool.rs @@ -10,7 +10,7 @@ use syntax::ast::*; use syntax::ast_util::{is_comparison_binop, binop_to_string}; use syntax::ptr::P; use syntax::codemap::Span; -use utils::{de_p, span_lint}; +use utils::{de_p, span_lint, snippet}; declare_lint! { pub NEEDLESS_BOOL, @@ -28,20 +28,30 @@ impl LintPass for NeedlessBool { } fn check_expr(&mut self, cx: &Context, e: &Expr) { - if let ExprIf(_, ref then_block, Option::Some(ref else_expr)) = e.node { + if let ExprIf(ref pred, ref then_block, Some(ref else_expr)) = e.node { match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) { - (Option::Some(true), Option::Some(true)) => { + (Some(true), Some(true)) => { span_lint(cx, NEEDLESS_BOOL, e.span, - "your if-then-else expression will always return true"); }, - (Option::Some(true), Option::Some(false)) => { + "this if-then-else expression will always return true"); }, + (Some(false), Some(false)) => { span_lint(cx, NEEDLESS_BOOL, e.span, - "you can reduce your if statement to its predicate"); }, - (Option::Some(false), Option::Some(true)) => { - span_lint(cx, NEEDLESS_BOOL, e.span, - "you can reduce your if statement to `!` + its predicate"); }, - (Option::Some(false), Option::Some(false)) => { - span_lint(cx, NEEDLESS_BOOL, e.span, - "your if-then-else expression will always return false"); }, + "this if-then-else expression will always return false"); }, + (Some(true), Some(false)) => { + let pred_snip = snippet(cx, pred.span, ".."); + let hint = if pred_snip == ".." { "its predicate".into() } else { + format!("`{}`", pred_snip) + }; + span_lint(cx, NEEDLESS_BOOL, e.span, &format!( + "you can reduce this if-then-else expression to just {}", hint)); + }, + (Some(false), Some(true)) => { + let pred_snip = snippet(cx, pred.span, ".."); + let hint = if pred_snip == ".." { "`!` and its predicate".into() } else { + format!("`!{}`", pred_snip) + }; + span_lint(cx, NEEDLESS_BOOL, e.span, &format!( + "you can reduce this if-then-else expression to just {}", hint)); + }, _ => () } } @@ -51,14 +61,14 @@ impl LintPass for NeedlessBool { fn fetch_bool_block(block: &Block) -> Option { if block.stmts.is_empty() { block.expr.as_ref().map(de_p).and_then(fetch_bool_expr) - } else { Option::None } + } else { None } } fn fetch_bool_expr(expr: &Expr) -> Option { match &expr.node { &ExprBlock(ref block) => fetch_bool_block(block), &ExprLit(ref lit_ptr) => if let &LitBool(value) = &lit_ptr.node { - Option::Some(value) } else { Option::None }, - _ => Option::None + Some(value) } else { None }, + _ => None } } diff --git a/tests/compile-fail/eq_op.rs b/tests/compile-fail/eq_op.rs index 298132013a9..8f61a11aa08 100755 --- a/tests/compile-fail/eq_op.rs +++ b/tests/compile-fail/eq_op.rs @@ -16,7 +16,7 @@ fn main() { 1.5 < 1.5; //~ERROR equal expressions 1u64 >= 1u64; //~ERROR equal expressions - // casts, methods, parenthesis + // casts, methods, parentheses (1 as u64) & (1 as u64); //~ERROR equal expressions 1 ^ ((((((1)))))); //~ERROR equal expressions id((1)) | id(1); //~ERROR equal expressions diff --git a/tests/compile-fail/needless_bool.rs b/tests/compile-fail/needless_bool.rs index 6016f79ab03..39fdf6353fd 100755 --- a/tests/compile-fail/needless_bool.rs +++ b/tests/compile-fail/needless_bool.rs @@ -4,9 +4,9 @@ #[deny(needless_bool)] fn main() { let x = true; - if x { true } else { true }; //~ERROR your if-then-else expression will always return true - if x { false } else { false }; //~ERROR your if-then-else expression will always return false - if x { true } else { false }; //~ERROR you can reduce your if statement to its predicate - if x { false } else { true }; //~ERROR you can reduce your if statement to `!` + its predicate + if x { true } else { true }; //~ERROR this if-then-else expression will always return true + if x { false } else { false }; //~ERROR this if-then-else expression will always return false + if x { true } else { false }; //~ERROR you can reduce this if-then-else expression to just `x` + if x { false } else { true }; //~ERROR you can reduce this if-then-else expression to just `!x` if x { x } else { false }; // would also be questionable, but we don't catch this yet }