From 6a566a1009fefdbfe30e8475836f6b06fed81b3c Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 14 Mar 2016 17:13:10 +0100 Subject: [PATCH] use snippet_opt and span_suggestion --- src/needless_bool.rs | 20 ++++++++++---------- tests/compile-fail/needless_bool.rs | 20 ++++++++++++++++---- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/src/needless_bool.rs b/src/needless_bool.rs index 58afc86b82c..43e7cfddadd 100644 --- a/src/needless_bool.rs +++ b/src/needless_bool.rs @@ -6,7 +6,7 @@ use rustc::lint::*; use rustc_front::hir::*; use syntax::ast::LitKind; use syntax::codemap::Spanned; -use utils::{span_lint, span_lint_and_then, snippet}; +use utils::{span_lint, span_lint_and_then, snippet, snippet_opt}; /// **What it does:** This lint checks for expressions of the form `if c { true } else { false }` (or vice versa) and suggest using the condition directly. /// @@ -50,16 +50,16 @@ impl LateLintPass for NeedlessBool { use self::Expression::*; if let ExprIf(ref pred, ref then_block, Some(ref else_expr)) = e.node { let reduce = |hint: &str, not| { - let pred_snip = snippet(cx, pred.span, ".."); - let hint = if pred_snip == ".." { - hint.into() - } else { - format!("`{}{}`", not, pred_snip) + let hint = match snippet_opt(cx, pred.span) { + Some(pred_snip) => format!("`{}{}`", not, pred_snip), + None => hint.into(), }; - span_lint(cx, - NEEDLESS_BOOL, - e.span, - &format!("you can reduce this if-then-else expression to just {}", hint)); + span_lint_and_then(cx, + NEEDLESS_BOOL, + e.span, + "this if-then-else expression returns a bool literal", |db| { + db.span_suggestion(e.span, "you can reduce it to", hint); + }); }; match (fetch_bool_block(then_block), fetch_bool_expr(else_expr)) { (RetBool(true), RetBool(true)) | diff --git a/tests/compile-fail/needless_bool.rs b/tests/compile-fail/needless_bool.rs index eff2bdc9b28..7f2d7754bda 100644 --- a/tests/compile-fail/needless_bool.rs +++ b/tests/compile-fail/needless_bool.rs @@ -7,8 +7,14 @@ fn main() { let x = true; 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 { true } else { false }; + //~^ ERROR this if-then-else expression returns a bool literal + //~| HELP you can reduce it to + //~| SUGGESTION `x` + if x { false } else { true }; + //~^ ERROR this if-then-else expression returns a bool literal + //~| HELP you can reduce it to + //~| SUGGESTION `!x` if x { x } else { false }; // would also be questionable, but we don't catch this yet bool_ret(x); bool_ret2(x); @@ -30,10 +36,16 @@ fn bool_ret2(x: bool) -> bool { #[deny(needless_bool)] fn bool_ret3(x: bool) -> bool { - if x { return true } else { return false }; //~ERROR you can reduce this if-then-else expression to just `return x` + if x { return true } else { return false }; + //~^ ERROR this if-then-else expression returns a bool literal + //~| HELP you can reduce it to + //~| SUGGESTION `return x` } #[deny(needless_bool)] fn bool_ret4(x: bool) -> bool { - if x { return false } else { return true }; //~ERROR you can reduce this if-then-else expression to just `return !x` + if x { return false } else { return true }; + //~^ ERROR this if-then-else expression returns a bool literal + //~| HELP you can reduce it to + //~| SUGGESTION `return !x` }