Skip to content

Commit

Permalink
Auto merge of rust-lang#9207 - Jarcho:todo_arm, r=giraffate
Browse files Browse the repository at this point in the history
Check for `todo!` on every expression in `SpanlessEq`

fixes rust-lang#9204
changelog: [`match_same_arms`](https://rust-lang.github.io/rust-clippy/master/index.html#match_same_arms): Don't lint on arms with `todo!`
  • Loading branch information
bors committed Jul 20, 2022
2 parents e98b3ca + 95c7591 commit 7c8e1bf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 30 deletions.
33 changes: 4 additions & 29 deletions clippy_utils/src/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,6 @@ 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
Expand Down Expand Up @@ -180,36 +177,13 @@ 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| {
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 {
Expand Down Expand Up @@ -327,7 +301,8 @@ impl HirEqInterExpr<'_, '_, '_> {
(&ExprKind::DropTemps(le), &ExprKind::DropTemps(re)) => self.eq_expr(le, re),
_ => false,
};
is_eq || self.inner.expr_fallback.as_mut().map_or(false, |f| f(left, right))
(is_eq && (!self.should_ignore(left) || !self.should_ignore(right)))
|| self.inner.expr_fallback.as_mut().map_or(false, |f| f(left, right))
}

fn eq_exprs(&mut self, left: &[Expr<'_>], right: &[Expr<'_>]) -> bool {
Expand Down
10 changes: 9 additions & 1 deletion tests/ui/match_same_arms2.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![warn(clippy::match_same_arms)]
#![allow(clippy::blacklisted_name)]
#![allow(clippy::blacklisted_name, clippy::diverging_sub_expression)]

fn bar<T>(_: T) {}
fn foo() -> bool {
Expand Down Expand Up @@ -227,4 +227,12 @@ fn main() {
Some(Bar { y: 0, x: 5, .. }) => 1,
_ => 200,
};

let _ = match 0 {
0 => todo!(),
1 => todo!(),
2 => core::convert::identity::<u32>(todo!()),
3 => core::convert::identity::<u32>(todo!()),
_ => 5,
};
}

0 comments on commit 7c8e1bf

Please sign in to comment.