Skip to content

Commit 714c64c

Browse files
authored
fix: unnecessary_safety_comment FP on desugared assign (rust-lang#14371)
Closes rust-lang#13039 changelog: [`unnecessary_safety_comment`]: fix FP on desugared assign
2 parents 8f280ff + 97275d5 commit 714c64c

File tree

4 files changed

+65
-2
lines changed

4 files changed

+65
-2
lines changed

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,25 @@ fn expr_has_unnecessary_safety_comment<'tcx>(
312312
},
313313
_,
314314
) => ControlFlow::Break(()),
315+
// `_ = foo()` is desugared to `{ let _ = foo(); }`
316+
hir::ExprKind::Block(
317+
Block {
318+
rules: BlockCheckMode::DefaultBlock,
319+
stmts:
320+
[
321+
hir::Stmt {
322+
kind:
323+
hir::StmtKind::Let(hir::LetStmt {
324+
source: hir::LocalSource::AssignDesugar(_),
325+
..
326+
}),
327+
..
328+
},
329+
],
330+
..
331+
},
332+
_,
333+
) => ControlFlow::Continue(Descend::Yes),
315334
// statements will be handled by check_stmt itself again
316335
hir::ExprKind::Block(..) => ControlFlow::Continue(Descend::No),
317336
_ => ControlFlow::Continue(Descend::Yes),

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.default.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,5 +342,17 @@ LL | const NO_SAFETY_IN_IMPL: i32 = unsafe { 1 };
342342
|
343343
= help: consider adding a safety comment on the preceding line
344344

345-
error: aborting due to 39 previous errors
345+
error: statement has unnecessary safety comment
346+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:719:5
347+
|
348+
LL | _ = bar();
349+
| ^^^^^^^^^^
350+
|
351+
help: consider removing the safety comment
352+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:718:5
353+
|
354+
LL | // SAFETY: unnecessary_safety_comment triggers here
355+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
356+
357+
error: aborting due to 40 previous errors
346358

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.disabled.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,5 +438,17 @@ LL | unsafe { Date::__from_ordinal_date_unchecked(1970, 1) }.into_julian
438438
|
439439
= help: consider adding a safety comment on the preceding line
440440

441-
error: aborting due to 51 previous errors
441+
error: statement has unnecessary safety comment
442+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:719:5
443+
|
444+
LL | _ = bar();
445+
| ^^^^^^^^^^
446+
|
447+
help: consider removing the safety comment
448+
--> tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs:718:5
449+
|
450+
LL | // SAFETY: unnecessary_safety_comment triggers here
451+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
452+
453+
error: aborting due to 52 previous errors
442454

tests/ui-toml/undocumented_unsafe_blocks/undocumented_unsafe_blocks.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,4 +703,24 @@ mod issue_11709_regression {
703703
//~[disabled]^ undocumented_unsafe_blocks
704704
}
705705

706+
fn issue_13039() {
707+
unsafe fn foo() -> usize {
708+
1234
709+
}
710+
711+
fn bar() -> usize {
712+
1234
713+
}
714+
715+
// SAFETY: unnecessary_safety_comment should not trigger here
716+
_ = unsafe { foo() };
717+
718+
// SAFETY: unnecessary_safety_comment triggers here
719+
_ = bar();
720+
//~^ unnecessary_safety_comment
721+
722+
// SAFETY: unnecessary_safety_comment should not trigger here
723+
_ = unsafe { foo() }
724+
}
725+
706726
fn main() {}

0 commit comments

Comments
 (0)