about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlejandra González <blyxyas@gmail.com>2025-08-06 17:59:23 +0000
committerGitHub <noreply@github.com>2025-08-06 17:59:23 +0000
commit4cb43f4c4128ed1f58d10ade425656caa2a05fa7 (patch)
tree5226a520c71395e2b0b51e6ee3d409e9a9222047
parenta4d43215b89819941f92c3ef5156de64c43d4a7b (diff)
parent8602faa6ffa6aca71470b734459c2805f106c8f2 (diff)
downloadrust-4cb43f4c4128ed1f58d10ade425656caa2a05fa7.tar.gz
rust-4cb43f4c4128ed1f58d10ade425656caa2a05fa7.zip
Optimize `needless_bool` lint (#15423)
Two optimizations have been done when checking for the context in which
to apply the lint:

- Checking for the mere presence of comments does not require building a
`String` with the comment to then check if it is empty and discard it.
- Checking for the presence of comment can be done after we have checked
that we do have a `if` construct that we intend to lint instead of for
every expression.

changelog: none

r? blyxyas
-rw-r--r--clippy_lints/src/needless_bool.rs21
1 files changed, 10 insertions, 11 deletions
diff --git a/clippy_lints/src/needless_bool.rs b/clippy_lints/src/needless_bool.rs
index b3aa1a7286a..fa5afcc0087 100644
--- a/clippy_lints/src/needless_bool.rs
+++ b/clippy_lints/src/needless_bool.rs
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet_with_applicability;
 use clippy_utils::sugg::Sugg;
 use clippy_utils::{
     SpanlessEq, get_parent_expr, higher, is_block_like, is_else_clause, is_expn_of, is_parent_stmt,
-    is_receiver_of_method_call, peel_blocks, peel_blocks_with_stmt, span_extract_comment, sym,
+    is_receiver_of_method_call, peel_blocks, peel_blocks_with_stmt, span_contains_comment, sym,
 };
 use rustc_ast::ast::LitKind;
 use rustc_errors::Applicability;
@@ -128,14 +128,13 @@ fn condition_needs_parentheses(e: &Expr<'_>) -> bool {
 impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
     fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
         use self::Expression::{Bool, RetBool};
-        if e.span.from_expansion() || !span_extract_comment(cx.tcx.sess.source_map(), e.span).is_empty() {
-            return;
-        }
-        if let Some(higher::If {
-            cond,
-            then,
-            r#else: Some(r#else),
-        }) = higher::If::hir(e)
+        if !e.span.from_expansion()
+            && let Some(higher::If {
+                cond,
+                then,
+                r#else: Some(else_expr),
+            }) = higher::If::hir(e)
+            && !span_contains_comment(cx.tcx.sess.source_map(), e.span)
         {
             let reduce = |ret, not| {
                 let mut applicability = Applicability::MachineApplicable;
@@ -167,7 +166,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
                     applicability,
                 );
             };
-            if let Some((a, b)) = fetch_bool_block(then).and_then(|a| Some((a, fetch_bool_block(r#else)?))) {
+            if let Some((a, b)) = fetch_bool_block(then).and_then(|a| Some((a, fetch_bool_block(else_expr)?))) {
                 match (a, b) {
                     (RetBool(true), RetBool(true)) | (Bool(true), Bool(true)) => {
                         span_lint(
@@ -193,7 +192,7 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessBool {
                 }
             }
             if let Some((lhs_a, a)) = fetch_assign(then)
-                && let Some((lhs_b, b)) = fetch_assign(r#else)
+                && let Some((lhs_b, b)) = fetch_assign(else_expr)
                 && SpanlessEq::new(cx).eq_expr(lhs_a, lhs_b)
             {
                 let mut applicability = Applicability::MachineApplicable;