//! This lint is **warn** by default
use crate::utils::sugg::Sugg;
-use crate::utils::{in_macro, span_lint, span_lint_and_sugg};
+use crate::utils::{higher, span_lint, span_lint_and_sugg};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_errors::Applicability;
use syntax::ast::LitKind;
use syntax::source_map::Spanned;
/// **Why is this bad?** Redundant code.
///
/// **Known problems:** Maybe false positives: Sometimes, the two branches are
- /// painstakingly documented (which we of course do not detect), so they *may*
+ /// painstakingly documented (which we, of course, do not detect), so they *may*
/// have some value. Even then, the documentation can be rewritten to match the
/// shorter code.
///
/// **Example:**
- /// ```rust
+ /// ```rust,ignore
/// if x {
/// false
/// } else {
/// true
/// }
/// ```
+ /// Could be written as
+ /// ```rust,ignore
+ /// !x
+ /// ```
pub NEEDLESS_BOOL,
complexity,
- "if-statements with plain booleans in the then- and else-clause, e.g. `if p { true } else { false }`"
+ "if-statements with plain booleans in the then- and else-clause, e.g., `if p { true } else { false }`"
}
declare_clippy_lint! {
/// **Known problems:** None.
///
/// **Example:**
- /// ```rust
+ /// ```rust,ignore
/// if x == true {} // could be `if x { }`
/// ```
pub BOOL_COMPARISON,
complexity,
- "comparing a variable to a boolean, e.g. `if x == true` or `if x != true`"
+ "comparing a variable to a boolean, e.g., `if x == true` or `if x != true`"
}
-#[derive(Copy, Clone)]
-pub struct NeedlessBool;
-
-impl LintPass for NeedlessBool {
- fn get_lints(&self) -> LintArray {
- lint_array!(NEEDLESS_BOOL)
- }
-
- fn name(&self) -> &'static str {
- "NeedlessBool"
- }
-}
+declare_lint_pass!(NeedlessBool => [NEEDLESS_BOOL]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NeedlessBool {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
use self::Expression::*;
- if let ExprKind::If(ref pred, ref then_block, Some(ref else_expr)) = e.node {
+ if let Some((ref pred, ref then_block, Some(ref else_expr))) = higher::if_block(&e) {
let reduce = |ret, not| {
let mut applicability = Applicability::MachineApplicable;
let snip = Sugg::hir_with_applicability(cx, pred, "<predicate>", &mut applicability);
}
fn parent_node_is_if_expr<'a, 'b>(expr: &Expr, cx: &LateContext<'a, 'b>) -> bool {
- let parent_id = cx.tcx.hir().get_parent_node_by_hir_id(expr.hir_id);
- let parent_node = cx.tcx.hir().get_by_hir_id(parent_id);
+ let parent_id = cx.tcx.hir().get_parent_node(expr.hir_id);
+ let parent_node = cx.tcx.hir().get(parent_id);
- if let rustc::hir::Node::Expr(e) = parent_node {
- if let ExprKind::If(_, _, _) = e.node {
- return true;
- }
+ match parent_node {
+ rustc::hir::Node::Expr(e) => higher::if_block(&e).is_some(),
+ rustc::hir::Node::Arm(e) => higher::if_block(&e.body).is_some(),
+ _ => false,
}
-
- false
}
-#[derive(Copy, Clone)]
-pub struct BoolComparison;
-
-impl LintPass for BoolComparison {
- fn get_lints(&self) -> LintArray {
- lint_array!(BOOL_COMPARISON)
- }
-
- fn name(&self) -> &'static str {
- "BoolComparison"
- }
-}
+declare_lint_pass!(BoolComparison => [BOOL_COMPARISON]);
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BoolComparison {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
- if in_macro(e.span) {
+ if e.span.from_expansion() {
return;
}