rust-lang / rust-lang/rust-clippy

missing-const-for-fn: println is not const-(st)able yet

Open
#13,015 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary
fn a() {
    let x = 1 else { return }; //~ WARN irrefutable `let...else` pattern

    // Multiline else blocks should not get printed
    let x = 1 else { //~ WARN irrefutable `let...else` pattern
        eprintln!("problem case encountered");
        return
    };
}

this cannot be a const fn yet although clippy suggets it:

error[E0015]: cannot call non-const fn `std::io::_eprint` in constant functions
 --> a.rs:6:9
  |
6 |         eprintln!("problem case encountered");
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
  = note: this error originates in the macro `eprintln` (in Nightly builds, run with -Z macro-backtrace for more info)

error: `std::fmt::Arguments::<'a>::new_const` is not yet stable as a const fn
 --> a.rs:6:9
  |
6 |         eprintln!("problem case encountered");
  |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = help: add `#![feature(const_fmt_arguments_new)]` to the crate attributes to enable
  = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `eprintln` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors; 4 warnings emitted
Lint Name

missing-const-for-fn:

Reproducer

I tried this code:

<code>

I saw this happen:

<output>

I expected to see this happen:

Version
rustc 1.81.0-nightly (9c3bc805d 2024-06-27)
binary: rustc
commit-hash: 9c3bc805dd9cb84019c124b9a50fdff1e62a7ec9
commit-date: 2024-06-27
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7
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.

Research direction

Read the missing-const-for-fn lint implementation and its existing tests, using the shown let-else example with eprintln! as the starting case. Confirm that the lint does not suggest const fn when the body calls non-const printing machinery, and add or update a regression test for the intended diagnostic.

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
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.