From 040d45e41253aeb6f8efbf3f07f737ea1fb961df Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Sat, 18 Jun 2022 18:24:39 +0900 Subject: check macro in eq_block --- clippy_utils/src/hir_utils.rs | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'clippy_utils/src') diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index 39f970dc420..f21098f95f7 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -1,5 +1,6 @@ use crate::consts::constant_simple; use crate::source::snippet_opt; +use crate::macros::macro_backtrace; use rustc_ast::ast::InlineAsmTemplatePiece; use rustc_data_structures::fx::FxHasher; use rustc_hir::def::Res; @@ -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 @@ -65,7 +66,9 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> { } pub fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool { - self.inter_expr().eq_block(left, right) + !self.cannot_be_compared_block(left) + && !self.cannot_be_compared_block(right) + && self.inter_expr().eq_block(left, right) } pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool { @@ -83,6 +86,38 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> { pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool { self.inter_expr().eq_path_segments(left, right) } + + fn cannot_be_compared_block(&mut self, block: &Block<'_>) -> bool { + if block.stmts.first().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.cx.tcx.get_diagnostic_name(macro_call.def_id), + Some(sym::todo_macro | sym::unimplemented_macro) + ) + ) { + return true; + } + + false + } } pub struct HirEqInterExpr<'a, 'b, 'tcx> { -- cgit 1.4.1-3-g733a5 From 4a02ae9636d57dd4b5b93e5d2688f9eec5de39e9 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Sat, 18 Jun 2022 18:29:39 +0900 Subject: cargo dev fmt --- clippy_lints/src/copies.rs | 9 +-------- clippy_utils/src/hir_utils.rs | 10 +++++----- 2 files changed, 6 insertions(+), 13 deletions(-) (limited to 'clippy_utils/src') diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index f6e4d9a4f5c..1deff9684a1 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -195,10 +195,7 @@ fn lint_if_same_then_else(cx: &LateContext<'_>, conds: &[&Expr<'_>], blocks: &[& .array_windows::<2>() .enumerate() .fold(true, |all_eq, (i, &[lhs, rhs])| { - if eq.eq_block(lhs, rhs) - && !contains_let(conds[i]) - && conds.get(i + 1).map_or(true, |e| !contains_let(e)) - { + if eq.eq_block(lhs, rhs) && !contains_let(conds[i]) && conds.get(i + 1).map_or(true, |e| !contains_let(e)) { span_lint_and_note( cx, IF_SAME_THEN_ELSE, @@ -368,10 +365,6 @@ fn eq_stmts( .all(|b| get_stmt(b).map_or(false, |s| eq.eq_stmt(s, stmt))) } - - - - fn scan_block_for_eq(cx: &LateContext<'_>, _conds: &[&Expr<'_>], block: &Block<'_>, blocks: &[&Block<'_>]) -> BlockEq { let mut eq = SpanlessEq::new(cx); let mut eq = eq.inter_expr(); diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index f21098f95f7..97a15108d0c 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -1,6 +1,6 @@ use crate::consts::constant_simple; -use crate::source::snippet_opt; use crate::macros::macro_backtrace; +use crate::source::snippet_opt; use rustc_ast::ast::InlineAsmTemplatePiece; use rustc_data_structures::fx::FxHasher; use rustc_hir::def::Res; @@ -88,12 +88,12 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> { } fn cannot_be_compared_block(&mut self, block: &Block<'_>) -> bool { - if block.stmts.first().map_or(false, |stmt| + if block.stmts.first().map_or(false, |stmt| { matches!( stmt.kind, StmtKind::Semi(semi_expr) if self.should_ignore(semi_expr) ) - ) { + }) { return true; } @@ -107,12 +107,12 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> { } fn should_ignore(&mut self, expr: &Expr<'_>) -> bool { - if macro_backtrace(expr.span).last().map_or(false, |macro_call| + if macro_backtrace(expr.span).last().map_or(false, |macro_call| { matches!( &self.cx.tcx.get_diagnostic_name(macro_call.def_id), Some(sym::todo_macro | sym::unimplemented_macro) ) - ) { + }) { return true; } -- cgit 1.4.1-3-g733a5 From 46d056e2eb7c5cab767c91383cf7ef1828809fd9 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Mon, 20 Jun 2022 11:05:40 +0900 Subject: check last statement --- clippy_utils/src/hir_utils.rs | 2 +- tests/ui/if_same_then_else.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) (limited to 'clippy_utils/src') diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index 97a15108d0c..3eb01bf75a6 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -88,7 +88,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> { } fn cannot_be_compared_block(&mut self, block: &Block<'_>) -> bool { - if block.stmts.first().map_or(false, |stmt| { + if block.stmts.last().map_or(false, |stmt| { matches!( stmt.kind, StmtKind::Semi(semi_expr) if self.should_ignore(semi_expr) diff --git a/tests/ui/if_same_then_else.rs b/tests/ui/if_same_then_else.rs index 4110d1a9c01..2598c2ab426 100644 --- a/tests/ui/if_same_then_else.rs +++ b/tests/ui/if_same_then_else.rs @@ -179,6 +179,38 @@ mod issue_8836 { } else { unimplemented!(); } + + if true { + println!("FOO"); + todo!(); + } else { + println!("FOO"); + todo!(); + } + + if true { + println!("FOO"); + unimplemented!(); + } else { + println!("FOO"); + unimplemented!(); + } + + if true { + println!("FOO"); + todo!() + } else { + println!("FOO"); + todo!() + } + + if true { + println!("FOO"); + unimplemented!() + } else { + println!("FOO"); + unimplemented!() + } } } -- cgit 1.4.1-3-g733a5 From 39ffda014debd9d1d4126a8996d39af934fe8d94 Mon Sep 17 00:00:00 2001 From: kyoto7250 <50972773+kyoto7250@users.noreply.github.com> Date: Mon, 20 Jun 2022 11:14:52 +0900 Subject: check macro in HitEqInterExpr --- clippy_utils/src/hir_utils.rs | 71 ++++++++++++++++++++++--------------------- 1 file changed, 36 insertions(+), 35 deletions(-) (limited to 'clippy_utils/src') diff --git a/clippy_utils/src/hir_utils.rs b/clippy_utils/src/hir_utils.rs index 3eb01bf75a6..46b615c04f3 100644 --- a/clippy_utils/src/hir_utils.rs +++ b/clippy_utils/src/hir_utils.rs @@ -66,9 +66,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> { } pub fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool { - !self.cannot_be_compared_block(left) - && !self.cannot_be_compared_block(right) - && self.inter_expr().eq_block(left, right) + self.inter_expr().eq_block(left, right) } pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool { @@ -86,38 +84,6 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> { pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool { self.inter_expr().eq_path_segments(left, right) } - - 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.cx.tcx.get_diagnostic_name(macro_call.def_id), - Some(sym::todo_macro | sym::unimplemented_macro) - ) - }) { - return true; - } - - false - } } pub struct HirEqInterExpr<'a, 'b, 'tcx> { @@ -156,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 @@ -206,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, -- cgit 1.4.1-3-g733a5