Skip to content

rustfmt panic: Option::unwrap() on None value in items.rs:562 #6571

@avrabe

Description

@avrabe

rustfmt nightly crashes with a panic when formatting a Rust project, while stable rustfmt works correctly on the same codebase.

Environment

  • rustfmt version: rustfmt 1.8.0-nightly (2805e1dc4c 2025-05-26)
  • rustc version: 1.88.0-nightly (d5b4c2e4f 2025-04-02)
  • Platform: aarch64-apple-darwin (macOS)
  • Stable rustfmt version (works): rustfmt 1.8.0-stable (17067e9ac6 2025-05-09)

Steps to Reproduce

  1. Clone the repository (large WebAssembly runtime project with multiple crates)
  2. Run cargo +nightly fmt or cargo +nightly fmt --check
  3. rustfmt crashes immediately

Expected Behavior

rustfmt should format the code successfully, as the stable version does.

Actual Behavior

rustfmt panics with the following error:

thread 'main' panicked at src/tools/rustfmt/src/items.rs:562:10:
called `Option::unwrap()` on a `None` value

Stack Trace

Click to expand full stack trace
stack backtrace:
   0:        0x112758b4c - std::backtrace::Backtrace::create::hc57bb19ebdef0510
   1:        0x1109139d0 - std[816cb4d147ae502e]::panicking::update_hook::<alloc[7a1f954cc2d99109]::boxed::Box<rustc_driver_impl[552622669175e5de]::install_ice_hook::{closure#1}>>::{closure#0}
   2:        0x112775d58 - std::panicking::rust_panic_with_hook::h375e05f6c3166197
   3:        0x112775924 - std::panicking::begin_panic_handler::{{closure}}::h91a9bdd01e1edf44
   4:        0x112771100 - std::sys::backtrace::__rust_end_short_backtrace::hc80203d592b7cf8a
   5:        0x112775614 - __rustc[d03900b93b55ae64]::rust_begin_unwind
   6:        0x1153cf430 - core::panicking::panic_fmt::hc0c2e31b3e5a1fcd
   7:        0x1153cf4a0 - core::panicking::panic::h3290d96a04360fb0
   8:        0x1153cf3c8 - core::option::unwrap_failed::he4762d0ad524140c
   9:        0x1046bdd18 - <rustfmt_nightly[f94b0dc06409199d]::visitor::FmtVisitor>::visit_item
  10:        0x10469dc38 - <rustfmt_nightly[f94b0dc06409199d]::visitor::FmtVisitor>::visit_items_with_reordering
  11:        0x1046c31cc - <rustfmt_nightly[f94b0dc06409199d]::visitor::FmtVisitor>::format_separate_mod
  12:        0x10456f3d4 - <scoped_tls[74c1bb14a531eeca]::ScopedKey<rustc_span[4c7be617748eb397]::SessionGlobals>>::with::<<rustfmt_nightly[f94b0dc06409199d]::Session<std[816cb4d147ae502e]::io::stdio::Stdout>>::format_input_inner::{closure#0}, core[5769011f6fae3bc7]::result::Result<rustfmt_nightly[f94b0dc06409199d]::FormatReport, rustfmt_nightly[f94b0dc06409199d]::ErrorKind>>
  13:        0x104584e18 - rustfmt[3a0166c872dddd99]::format_and_emit_report::<std[816cb4d147ae502e]::io::stdio::Stdout>
  14:        0x104582cec - rustfmt[3a0166c872dddd99]::execute
  15:        0x10457fd40 - rustfmt[3a0166c872dddd99]::main
  16:        0x104577b18 - std[816cb4d147ae502e]::sys::backtrace::__rust_begin_short_backtrace::<fn(), ()>
  17:        0x104577ef0 - std[816cb4d147ae502e]::rt::lang_start::<()>::{closure#0}
  18:        0x112756468 - std::rt::lang_start_internal::h08d727d9648d1aa4
  19:        0x104585d7c - _main

Minimal Reproduction Example

While I couldn't isolate a minimal example due to the crash occurring early in the formatting process, the project that triggers this issue contains:

  • Multiple workspace crates
  • Heavy use of conditional compilation (#[cfg] attributes)
  • No-std support with feature flags
  • Generic type parameters with bounds
  • Macro usage (both declarative and procedural)

Additional Context

  • The crash occurs at line 562 in src/tools/rustfmt/src/items.rs (based on the ICE file, though the panic message shows line 567)
  • The panic happens in the visit_item method of FmtVisitor
  • Stable rustfmt (1.8.0-stable) formats the same codebase without any issues
  • The project uses a rustfmt.toml with several nightly-only options

Workaround

Using stable rustfmt (cargo fmt) works correctly, though it cannot use the nightly-only formatting options specified in rustfmt.toml.

rustfmt.toml Configuration

Click to expand rustfmt.toml
edition = "2021"
max_width = 100
use_small_heuristics = "Default"
reorder_imports = true
reorder_modules = true
remove_nested_parens = true
merge_derives = true
use_try_shorthand = true
use_field_init_shorthand = true
force_explicit_abi = true
newline_style = "Auto"
normalize_doc_attributes = true
overflow_delimited_expr = true
wrap_comments = true
comment_width = 80
normalize_comments = true
format_strings = true
format_macro_matchers = true
format_macro_bodies = true
hex_literal_case = "Lower"
imports_granularity = "Crate"
group_imports = "StdExternalCrate"
imports_layout = "HorizontalVertical"
trailing_comma = "Vertical"

Related Issues

Could be related to recent changes in item visitor or handling of specific Rust syntax patterns in the nightly version.

ICE Report File

The full ICE report was saved to: `r

rustc-ice-2025-05-27T15_32_11-98522.txt

ustc-ice-2025-05-27T15_32_11-98522.txt`

Metadata

Metadata

Labels

needs-mcveneeds a Minimal Complete and Verifiable Example

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions