rust-lang / rust-lang/rust-clippy

IIFE false positive when temporary lifetimes are involved

Open
#8,653 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

The format_args! macro creates temporaries dropped at the end of the expression, so

let x = format_args!("");

is almost always unusable. An IIFE can be used to extend the temporary lifetime to the entirety of the IIFE, allowing format_args! to be used for factoring out large format_args!.

This typically isn't necessary, as you an just nest format_args! directly, but is useful for cases where a fmt;:Arguments may be used in multiple places, e.g.

(|message| {
    #[cfg(debug_assertions)]
    panic!("{}", message);
    #[cfg(not(debug_assertions)]
    eprintln!("{}; attempting to continue...", message);
})(format_args!("big message that formats some locals and would be annoying to duplicate multiple times"));
Lint Name

clippy::redundant_closure_call

Reproducer

I tried this code:

pub fn main() {
    let a = 2;
    let b = 3;
    let c = a + b;

    (|equasion| {
        println!("With Rust, we can know that {equasion}!");
    })(format_args!("{a} + {b} = {c}"));
}

I saw this happen:

warning: try not to call a closure in the expression where it is declared
 --> src/main.rs:6:5
  |
6 | /     (|equasion| {
7 | |         println!("With Rust, we can know that {equasion}!");
8 | |     })(format_args!("{a} + {b} = {c}"));
  | |_______________________________________^
  |
  = note: `#[warn(clippy::redundant_closure_call)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call

I expected to see this happen:

No warnings (as with IIFE-for-try) because replacing the IIFE with a let binding changes temporary lifetimes to make the code no longer compile.

Version
rustc 1.61.0-nightly (76d770ac2 2022-04-02)
binary: rustc
commit-hash: 76d770ac21d9521db6a92a48c7b3d5b2cc535941
commit-date: 2022-04-02
host: x86_64-pc-windows-msvc
release: 1.61.0-nightly
LLVM version: 14.0.0

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.

Research direction

Start by locating the implementation and tests for the clippy::redundant_closure_call lint, then run the Rust reproducer from the issue to confirm the warning. Compare the format_args! IIFE case with an ordinary redundant closure call; done means the lifetime-preserving case is accepted without weakening the lint for cases that remain redundant.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.