rust-lang / rust-lang/rust-clippy

missing-const-for-fn : const_refs_to_cell is not stable yet

Open
#13,016 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary
use std::mem::forget;

#[allow(dead_code)]
 fn test_generic_fn_forget<T>(val: T) {
    forget(&val);
    forget(val);
}

=>

warning: this could be a `const fn`
 --> a.rs:5:2
  |
5 | /  fn test_generic_fn_forget<T>(val: T) {
6 | |     forget(&val);
7 | |     forget(val);
8 | | }
  | |_^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
  = note: requested on the command line with `-W clippy::missing-const-for-fn`
help: make the function `const`
  |
5 |  const fn test_generic_fn_forget<T>(val: T) {

=>

error[E0658]: cannot borrow here, since the borrowed element may contain interior mutability
 --> a.rs:5:12
  |
5 |     forget(&val);
  |            ^^^^
  |
  = note: see issue #80384 <https://github.com/rust-lang/rust/issues/80384> for more information
  = help: add `#![feature(const_refs_to_cell)]` to the crate attributes to enable
  = note: this compiler was built on 2024-06-27; consider upgrading it if it is out of date

error: aborting due to 1 previous error
Lint Name

No response

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

Start with the missing_const_for_fn lint and reproduce the report using the supplied Rust snippet and nightly compiler version. Inspect how the lint handles the unstable const_refs_to_cell feature; done means this example no longer receives an unusable const-fn suggestion while valid cases retain their diagnostic.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.