]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/needless_bool.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / needless_bool.rs
index a9b91705483002047b62e8078ec1e507cdc135bf..b761457f64ce23e035ea042c9dab34085e11e5bb 100644 (file)
@@ -3,7 +3,7 @@
 //! This lint is **warn** by default
 
 use crate::utils::sugg::Sugg;
-use crate::utils::{higher, in_macro_or_desugar, 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_lint_pass, declare_tool_lint};
     /// 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 }`"
@@ -46,7 +50,7 @@
     /// **Known problems:** None.
     ///
     /// **Example:**
-    /// ```rust
+    /// ```rust,ignore
     /// if x == true {} // could be `if x { }`
     /// ```
     pub BOOL_COMPARISON,
@@ -115,23 +119,21 @@ fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
 }
 
 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 higher::if_block(&e).is_some() {
-            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
 }
 
 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_or_desugar(e.span) {
+        if e.span.from_expansion() {
             return;
         }