about summary refs log tree commit diff
path: root/clippy_utils/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2022-06-20 15:08:32 +0000
committerbors <bors@rust-lang.org>2022-06-20 15:08:32 +0000
commit93c6f9ebed65eb4d77a5cf1ccf670cef3b1fca9e (patch)
tree42c135810bfe8dd6b87d1fd2ef1b65a8c1f309ec /clippy_utils/src
parent97d451397de3f97fa586297a7bc2d16908d3d842 (diff)
parent39ffda014debd9d1d4126a8996d39af934fe8d94 (diff)
downloadrust-93c6f9ebed65eb4d77a5cf1ccf670cef3b1fca9e.tar.gz
rust-93c6f9ebed65eb4d77a5cf1ccf670cef3b1fca9e.zip
Auto merge of #9006 - kyoto7250:issue-8836-v2, r=Jarcho
feat(fix): ignore `todo!` and `unimplemented!` in `if_same_then_else`

close: #8836
take over:  #8853

This PR adds  check `todo!` and `unimplemented!` in if_same_then_else.
( I thought `unimplemented` should not be checked as well as todo!.)

Thank you in advance.

changelog: ignore todo! and unimplemented! in if_same_then_else

r? `@Jarcho`
Diffstat (limited to 'clippy_utils/src')
-rw-r--r--clippy_utils/src/hir_utils.rs38
1 files changed, 37 insertions, 1 deletions
diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs
index af62c4afd5a..74ef2c1bebb 100644
--- a/clippy_utils/src/hir_utils.rs
+++ b/clippy_utils/src/hir_utils.rs
@@ -1,4 +1,5 @@
 use crate::consts::constant_simple;
+use crate::macros::macro_backtrace;
 use crate::source::snippet_opt;
 use rustc_ast::ast::InlineAsmTemplatePiece;
 use rustc_data_structures::fx::FxHasher;
@@ -12,7 +13,7 @@ use rustc_hir::{
 use rustc_lexer::{tokenize, TokenKind};
 use rustc_lint::LateContext;
 use rustc_middle::ty::TypeckResults;
-use rustc_span::Symbol;
+use rustc_span::{sym, Symbol};
 use std::hash::{Hash, Hasher};
 
 /// Type used to check whether two ast are the same. This is different from the
@@ -121,6 +122,9 @@ impl HirEqInterExpr<'_, '_, '_> {
 
     /// Checks whether two blocks are the same.
     fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
+        if self.cannot_be_compared_block(left) || self.cannot_be_compared_block(right) {
+            return false;
+        }
         match (left.stmts, left.expr, right.stmts, right.expr) {
             ([], None, [], None) => {
                 // For empty blocks, check to see if the tokens are equal. This will catch the case where a macro
@@ -171,6 +175,38 @@ impl HirEqInterExpr<'_, '_, '_> {
         }
     }
 
+    fn cannot_be_compared_block(&mut self, block: &Block<'_>) -> bool {
+        if block.stmts.last().map_or(false, |stmt| {
+            matches!(
+                stmt.kind,
+                StmtKind::Semi(semi_expr) if self.should_ignore(semi_expr)
+            )
+        }) {
+            return true;
+        }
+
+        if let Some(block_expr) = block.expr
+            && self.should_ignore(block_expr)
+        {
+            return true
+        }
+
+        false
+    }
+
+    fn should_ignore(&mut self, expr: &Expr<'_>) -> bool {
+        if macro_backtrace(expr.span).last().map_or(false, |macro_call| {
+            matches!(
+                &self.inner.cx.tcx.get_diagnostic_name(macro_call.def_id),
+                Some(sym::todo_macro | sym::unimplemented_macro)
+            )
+        }) {
+            return true;
+        }
+
+        false
+    }
+
     pub fn eq_array_length(&mut self, left: ArrayLen, right: ArrayLen) -> bool {
         match (left, right) {
             (ArrayLen::Infer(..), ArrayLen::Infer(..)) => true,