}
}
- let hint = format!("{}", snippet(cx, right_side.span, ".."));
+ #[derive(Copy,Clone)]
+ pub struct BoolComparison;
+
+ impl LintPass for BoolComparison {
+ fn get_lints(&self) -> LintArray {
+ lint_array!(BOOL_COMPARISON)
+ }
+ }
+
+ impl LateLintPass for BoolComparison {
+ fn check_expr(&mut self, cx: &LateContext, e: &Expr) {
+ if let ExprBinary(Spanned{ node: BiEq, .. }, ref left_side, ref right_side) = e.node {
+ match (fetch_bool_expr(left_side), fetch_bool_expr(right_side)) {
+ (Some(true), None) => {
- let hint = format!("{}", snippet(cx, left_side.span, ".."));
++ let hint = snippet(cx, right_side.span, "..").into_owned();
+ span_lint_and_then(cx,
+ BOOL_COMPARISON,
+ e.span,
+ "equality checks against true are unnecesary",
+ |db| {
+ db.span_suggestion(e.span, "try simplifying it as shown:", hint);
+ });
+ }
+ (None, Some(true)) => {
++ let hint = snippet(cx, left_side.span, "..").into_owned();
+ span_lint_and_then(cx,
+ BOOL_COMPARISON,
+ e.span,
+ "equality checks against true are unnecesary",
+ |db| {
+ db.span_suggestion(e.span, "try simplifying it as shown:", hint);
+ });
+ }
+ (Some(false), None) => {
+ let hint = format!("!{}", snippet(cx, right_side.span, ".."));
+ span_lint_and_then(cx,
+ BOOL_COMPARISON,
+ e.span,
+ "equality checks against false can be replaced by a negation",
+ |db| {
+ db.span_suggestion(e.span, "try simplifying it as shown:", hint);
+ });
+ }
+ (None, Some(false)) => {
+ let hint = format!("!{}", snippet(cx, left_side.span, ".."));
+ span_lint_and_then(cx,
+ BOOL_COMPARISON,
+ e.span,
+ "equality checks against false can be replaced by a negation",
+ |db| {
+ db.span_suggestion(e.span, "try simplifying it as shown:", hint);
+ });
+ }
+ _ => (),
+ }
+ }
+ }
+ }
+
fn fetch_bool_block(block: &Block) -> Option<bool> {
if block.stmts.is_empty() {
block.expr.as_ref().and_then(|e| fetch_bool_expr(e))