Skip to content

Commit 040d45e

Browse files
committed
check macro in eq_block
1 parent 7a83809 commit 040d45e

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

clippy_lints/src/copies.rs

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then};
2-
use clippy_utils::macros::macro_backtrace;
32
use clippy_utils::source::{first_line_of_span, indent_of, reindent_multiline, snippet, snippet_opt};
43
use clippy_utils::{
54
eq_expr_value, get_enclosing_block, hash_expr, hash_stmt, if_sequence, is_else_clause, is_lint_allowed,
@@ -13,7 +12,7 @@ use rustc_lint::{LateContext, LateLintPass};
1312
use rustc_session::{declare_lint_pass, declare_tool_lint};
1413
use rustc_span::hygiene::walk_chain;
1514
use rustc_span::source_map::SourceMap;
16-
use rustc_span::{sym, BytePos, Span, Symbol};
15+
use rustc_span::{BytePos, Span, Symbol};
1716
use std::borrow::Cow;
1817

1918
declare_clippy_lint! {
@@ -197,8 +196,6 @@ fn lint_if_same_then_else(cx: &LateContext<'_>, conds: &[&Expr<'_>], blocks: &[&
197196
.enumerate()
198197
.fold(true, |all_eq, (i, &[lhs, rhs])| {
199198
if eq.eq_block(lhs, rhs)
200-
&& !contains_acceptable_macro(cx, lhs)
201-
&& !contains_acceptable_macro(cx, rhs)
202199
&& !contains_let(conds[i])
203200
&& conds.get(i + 1).map_or(true, |e| !contains_let(e))
204201
{
@@ -371,37 +368,9 @@ fn eq_stmts(
371368
.all(|b| get_stmt(b).map_or(false, |s| eq.eq_stmt(s, stmt)))
372369
}
373370

374-
fn contains_acceptable_macro(cx: &LateContext<'_>, block: &Block<'_>) -> bool {
375-
if block.stmts.first().map_or(false, |stmt|
376-
matches!(
377-
stmt.kind,
378-
StmtKind::Semi(semi_expr) if acceptable_macro(cx, semi_expr)
379-
)
380-
) {
381-
return true;
382-
}
383-
384-
if let Some(block_expr) = block.expr
385-
&& acceptable_macro(cx, block_expr)
386-
{
387-
return true
388-
}
389371

390-
false
391-
}
392372

393-
fn acceptable_macro(cx: &LateContext<'_>, expr: &Expr<'_>) -> bool {
394-
if macro_backtrace(expr.span).last().map_or(false, |macro_call|
395-
matches!(
396-
&cx.tcx.get_diagnostic_name(macro_call.def_id),
397-
Some(sym::todo_macro | sym::unimplemented_macro)
398-
)
399-
) {
400-
return true;
401-
}
402373

403-
false
404-
}
405374

406375
fn scan_block_for_eq(cx: &LateContext<'_>, _conds: &[&Expr<'_>], block: &Block<'_>, blocks: &[&Block<'_>]) -> BlockEq {
407376
let mut eq = SpanlessEq::new(cx);

clippy_utils/src/hir_utils.rs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::consts::constant_simple;
22
use crate::source::snippet_opt;
3+
use crate::macros::macro_backtrace;
34
use rustc_ast::ast::InlineAsmTemplatePiece;
45
use rustc_data_structures::fx::FxHasher;
56
use rustc_hir::def::Res;
@@ -12,7 +13,7 @@ use rustc_hir::{
1213
use rustc_lexer::{tokenize, TokenKind};
1314
use rustc_lint::LateContext;
1415
use rustc_middle::ty::TypeckResults;
15-
use rustc_span::Symbol;
16+
use rustc_span::{sym, Symbol};
1617
use std::hash::{Hash, Hasher};
1718

1819
/// 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> {
6566
}
6667

6768
pub fn eq_block(&mut self, left: &Block<'_>, right: &Block<'_>) -> bool {
68-
self.inter_expr().eq_block(left, right)
69+
!self.cannot_be_compared_block(left)
70+
&& !self.cannot_be_compared_block(right)
71+
&& self.inter_expr().eq_block(left, right)
6972
}
7073

7174
pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
@@ -83,6 +86,38 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
8386
pub fn eq_path_segments(&mut self, left: &[PathSegment<'_>], right: &[PathSegment<'_>]) -> bool {
8487
self.inter_expr().eq_path_segments(left, right)
8588
}
89+
90+
fn cannot_be_compared_block(&mut self, block: &Block<'_>) -> bool {
91+
if block.stmts.first().map_or(false, |stmt|
92+
matches!(
93+
stmt.kind,
94+
StmtKind::Semi(semi_expr) if self.should_ignore(semi_expr)
95+
)
96+
) {
97+
return true;
98+
}
99+
100+
if let Some(block_expr) = block.expr
101+
&& self.should_ignore(block_expr)
102+
{
103+
return true
104+
}
105+
106+
false
107+
}
108+
109+
fn should_ignore(&mut self, expr: &Expr<'_>) -> bool {
110+
if macro_backtrace(expr.span).last().map_or(false, |macro_call|
111+
matches!(
112+
&self.cx.tcx.get_diagnostic_name(macro_call.def_id),
113+
Some(sym::todo_macro | sym::unimplemented_macro)
114+
)
115+
) {
116+
return true;
117+
}
118+
119+
false
120+
}
86121
}
87122

88123
pub struct HirEqInterExpr<'a, 'b, 'tcx> {

0 commit comments

Comments
 (0)