spelling fix, rework needless_bool with snippet (fixes #150)
This commit is contained in:
parent
1d64553bc6
commit
8a98736f51
@ -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<bool> {
|
||||
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<bool> {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user