Skip to content

unnecessary_safety_comment causes compiler to panic #15684

@teofr

Description

@teofr

Summary

When unnecessary_safety_comment tries to check for comments over attributes recent changes make clippy to ICE when getting the span of the attribute.

To reproduce just enable unnecessary_safety_comment and add some statement (I think blocks as well) with an attribute on top.

#![warn(clippy::unnecessary_safety_comment)]

fn foo() -> i32 {
    // SAFETY: hello
    #[must_use]
    return 33;
}

fn main() {
    println!("Hello, world!");
}

This only seems to happen in the nightly version, so to reproduce just RUST_BACKTRACE=1 cargo dev lint tmp/test.rs over a clone of master. (I'm currently at 2a6197e375398afbb501ac71f25c943a05031403)

Version

rustc 1.91.0-nightly (a1208bf76 2025-09-03)
binary: rustc
commit-hash: a1208bf765ba783ee4ebdc4c29ab0a0c215806ef
commit-date: 2025-09-03
host: aarch64-apple-darwin
release: 1.91.0-nightly
LLVM version: 21.1.0

Error output

Backtrace

  Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
   Running `target/debug/clippy_dev lint tmp/test.rs`
  Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.13s
   Running `target/debug/clippy-driver -L ./target/debug -Z no-codegen --edition 2024 tmp/test.rs`
warning: `#[must_use]` attribute cannot be used on expressions
--> tmp/test.rs:5:5
|
5 |     #[must_use]
|     ^^^^^^^^^^^
|
= warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in a future release!
= help: `#[must_use]` can be applied to functions, data types, unions, and traits
= note: `#[warn(unused_attributes)]` (part of `#[warn(unused)]`) on by default

warning: function `foo` is never used
--> tmp/test.rs:3:4
|
3 | fn foo() -> i32 {
|    ^^^
|
= note: `#[warn(dead_code)]` (part of `#[warn(unused)]`) on by default


thread 'rustc' (1222232) panicked at /Users/teofr/.rustup/toolchains/nightly-2025-09-04-aarch64-apple-darwin/lib/rustlib/rustc-src/rust/compiler/rustc_hir/src/hir.rs:1323:18:
can't get the span of an arbitrary parsed attribute: Parsed(MustUse { span: tmp/test.rs:5:5: 5:16 (#0), reason: None })
stack backtrace:
 0: __rustc::rust_begin_unwind
 1: core::panicking::panic_fmt
 2: <rustc_hir::hir::Attribute as rustc_ast::attr::AttributeExt>::span
           at /Users/teofr/.rustup/toolchains/nightly-2025-09-04-aarch64-apple-darwin/lib/rustlib/rustc-src/rust/compiler/rustc_hir/src/hir.rs:1323:18
 3: rustc_hir::hir::Attribute::span
           at /Users/teofr/.rustup/toolchains/nightly-2025-09-04-aarch64-apple-darwin/lib/rustlib/rustc-src/rust/compiler/rustc_hir/src/hir.rs:1447:9
 4: clippy_lints::undocumented_unsafe_blocks::include_attrs_in_span::{{closure}}
           at ./clippy_lints/src/undocumented_unsafe_blocks.rs:481:21
 5: <core::slice::iter::Iter<T> as core::iter::traits::iterator::Iterator>::fold
           at /Users/teofr/.rustup/toolchains/nightly-2025-09-04-aarch64-apple-darwin/lib/rustlib/src/rust/library/core/src/slice/iter/macros.rs:255:27
 6: clippy_lints::undocumented_unsafe_blocks::include_attrs_in_span
           at ./clippy_lints/src/undocumented_unsafe_blocks.rs:477:45
 7: clippy_lints::undocumented_unsafe_blocks::stmt_has_safety_comment
           at ./clippy_lints/src/undocumented_unsafe_blocks.rs:579:16
 8: <clippy_lints::undocumented_unsafe_blocks::UndocumentedUnsafeBlocks as rustc_lint::passes::LateLintPass>::check_stmt
           at ./clippy_lints/src/undocumented_unsafe_blocks.rs:172:17
 9: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass>>::with_lint_attrs::<<rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_stmt::{closure#0}>
10: rustc_hir::intravisit::walk_block::<rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass>>
11: rustc_hir::intravisit::walk_expr::<rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass>>
12: rustc_hir::intravisit::walk_body::<rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass>>
13: rustc_hir::intravisit::walk_fn::<rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass>>
14: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass>>::with_param_env::<<rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_item::{closure#0}::{closure#0}>
15: <rustc_lint::late::LateContextAndPass<rustc_lint::late::RuntimeCombinedLateLintPass> as rustc_hir::intravisit::Visitor>::visit_item
16: rustc_lint::late::check_crate::{closure#0}
17: rustc_lint::late::check_crate
18: rustc_interface::passes::analysis::{closure#0}
19: rustc_interface::passes::analysis
    [... omitted 1 frame ...]
20: rustc_interface::passes::create_and_enter_global_ctxt::<core::option::Option<rustc_interface::queries::Linker>, rustc_driver_impl::run_compiler::{closure#0}::{closure#2}>
21: rustc_interface::interface::run_compiler::<(), rustc_driver_impl::run_compiler::{closure#0}>::{closure#1}
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

error: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new?template=ice.yml

note: please make sure that you have updated to the latest nightly

note: rustc 1.91.0-nightly (a1208bf76 2025-09-03) running on aarch64-apple-darwin

note: compiler flags: -Z no-codegen

query stack during panic:
#0 [analysis] running analysis passes on this crate
end of query stack
note: Clippy version: clippy 0.1.91 (2a6197e375 2025-09-16)

warning: 2 warnings emitted  ```

</p>
</details>


<!-- TRIAGEBOT_START -->

<!-- TRIAGEBOT_ASSIGN_START -->

<!-- TRIAGEBOT_ASSIGN_END -->
<!-- TRIAGEBOT_END -->

Metadata

Metadata

Assignees

Labels

C-bugCategory: Clippy is not doing the correct thingI-ICEIssue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions