Skip to content

Commit af2dd2d

Browse files
committed
fix unnecessary_semicolon: don't lint on stmts with attrs
1 parent 874f1c8 commit af2dd2d

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

clippy_lints/src/unnecessary_semicolon.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ impl<'tcx> LateLintPass<'tcx> for UnnecessarySemicolon {
8787
ExprKind::If(..) | ExprKind::Match(_, _, MatchSource::Normal | MatchSource::Postfix)
8888
)
8989
&& cx.typeck_results().expr_ty(expr) == cx.tcx.types.unit
90+
// if a stmt has attrs, then turning it into an expr will break the code, since attrs aren't allowed on exprs
91+
&& cx.tcx.hir_attrs(stmt.hir_id).is_empty()
9092
{
9193
if let Some(block_is_unit) = self.is_last_in_block(stmt) {
9294
if cx.tcx.sess.edition() <= Edition2021 && leaks_droppable_temporary_with_limited_lifetime(cx, expr) {

tests/ui/unnecessary_semicolon.edition2021.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ fn issue14100() -> bool {
6363
// cast into the `bool` function return type.
6464
if return true {};
6565
}
66+
67+
fn issue15426(x: u32) {
68+
// removing the `;` would turn the stmt into an expr, but attrs aren't allowed on exprs
69+
#[rustfmt::skip]
70+
match x {
71+
0b00 => {} 0b01 => {}
72+
0b11 => {} _ => {}
73+
};
74+
}

tests/ui/unnecessary_semicolon.edition2024.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ fn issue14100() -> bool {
6363
// cast into the `bool` function return type.
6464
if return true {};
6565
}
66+
67+
fn issue15426(x: u32) {
68+
// removing the `;` would turn the stmt into an expr, but attrs aren't allowed on exprs
69+
#[rustfmt::skip]
70+
match x {
71+
0b00 => {} 0b01 => {}
72+
0b11 => {} _ => {}
73+
};
74+
}

tests/ui/unnecessary_semicolon.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,12 @@ fn issue14100() -> bool {
6363
// cast into the `bool` function return type.
6464
if return true {};
6565
}
66+
67+
fn issue15426(x: u32) {
68+
// removing the `;` would turn the stmt into an expr, but attrs aren't allowed on exprs
69+
#[rustfmt::skip]
70+
match x {
71+
0b00 => {} 0b01 => {}
72+
0b11 => {} _ => {}
73+
};
74+
}

0 commit comments

Comments
 (0)