mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-17 22:46:50 +00:00
use snippet_opt and span_suggestion
This commit is contained in:
parent
9dc282e31d
commit
6a566a1009
@ -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)) |
|
||||
|
@ -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`
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user