rust-lang / rust-lang/rust-clippy

Inconsistency of `format_args!` between clippy and rustc

Open
#16,736 9 comments 0 reactions 1 assignee View on GitHub

@diksipav is already working on this.

Since Apr 3, 2026.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

I'm refactoring my code to leverage store-able format_args (https://github.com/rust-lang/rust/pull/140748) which is stable since 1.89, but found that cargo clippy sometimes reports lifetime errors where cargo check or cargo build compiles fine without any error.

The following code compiles since Rust 1.89, but still emits lifetime errors on clippy since 1.89. The latest stable and nightly clippy still fail to compile it.

Reproducer

Code (playground link):

use std::fmt;

// This function compiles fine on both rustc and clippy.
fn do_fmt1(w: &mut dyn fmt::Write, mode: u8) -> fmt::Result {
    let f = match mode {
        0 => format_args!("foo"),
        1 => format_args!("bar {}", 42),
        _ => unreachable!(),
    };
    w.write_fmt(f)
}

fn do_fmt2(w: &mut dyn fmt::Write, mode: u8) -> fmt::Result {
    w.write_fmt(match mode {
        0 => format_args!("foo"),
        1 => format_args!("bar {}", 42), // <- clippy reports lifetime error, but rustc compiles fine.
        _ => unreachable!(),
    })
}

fn main() {}

Current output:

  Checking playground v0.0.1 (/playground)
error[E0716]: temporary value dropped while borrowed
  --> src/main.rs:15:14
   |
13 |     w.write_fmt(match mode {
   |       --------- borrow later used by call
14 |         0 => format_args!("foo"),
15 |         1 => format_args!("bar {}", 42),
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^-
   |              |                        |
   |              |                        temporary value is freed at the end of this statement
   |              creates a temporary value which is freed while still in use
   |
   = note: consider using a `let` binding to create a longer lived value

For more information about this error, try `rustc --explain E0716`.
error: could not compile `playground` (bin "playground") due to 1 previous error

Desired output:

No error.

Version
rustc 1.94.0 (4a4ef493e 2026-03-02)
binary: rustc
commit-hash: 4a4ef493e3a1488c6e321570238084b38948f6db
commit-date: 2026-03-02
host: x86_64-unknown-linux-gnu
release: 1.94.0
LLVM version: 21.1.8
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.