fix unnecessary_semicolon: don't lint on stmts with attrs

This commit is contained in:
Ada Alakbarova 2025-08-12 19:06:28 +02:00
parent 874f1c8dc0
commit af2dd2d3bc
No known key found for this signature in database
4 changed files with 29 additions and 0 deletions

View file

@ -87,6 +87,8 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessarySemicolon {
ExprKind::If(..) | ExprKind::Match(_, _, MatchSource::Normal | MatchSource::Postfix)
)
&& cx.typeck_results().expr_ty(expr) == cx.tcx.types.unit
// if a stmt has attrs, then turning it into an expr will break the code, since attrs aren't allowed on exprs
&& cx.tcx.hir_attrs(stmt.hir_id).is_empty()
{
if let Some(block_is_unit) = self.is_last_in_block(stmt) {
if cx.tcx.sess.edition() <= Edition2021 && leaks_droppable_temporary_with_limited_lifetime(cx, expr) {

View file

@ -63,3 +63,12 @@ fn issue14100() -> bool {
// cast into the `bool` function return type.
if return true {};
}
fn issue15426(x: u32) {
// removing the `;` would turn the stmt into an expr, but attrs aren't allowed on exprs
#[rustfmt::skip]
match x {
0b00 => {} 0b01 => {}
0b11 => {} _ => {}
};
}

View file

@ -63,3 +63,12 @@ fn issue14100() -> bool {
// cast into the `bool` function return type.
if return true {};
}
fn issue15426(x: u32) {
// removing the `;` would turn the stmt into an expr, but attrs aren't allowed on exprs
#[rustfmt::skip]
match x {
0b00 => {} 0b01 => {}
0b11 => {} _ => {}
};
}

View file

@ -63,3 +63,12 @@ fn issue14100() -> bool {
// cast into the `bool` function return type.
if return true {};
}
fn issue15426(x: u32) {
// removing the `;` would turn the stmt into an expr, but attrs aren't allowed on exprs
#[rustfmt::skip]
match x {
0b00 => {} 0b01 => {}
0b11 => {} _ => {}
};
}