Skip to content

missing-const-for-fn false positive when a derefMut is used #14290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
GuiAmPm opened this issue Feb 24, 2025 · 0 comments · Fixed by #14294
Closed

missing-const-for-fn false positive when a derefMut is used #14290

GuiAmPm opened this issue Feb 24, 2025 · 0 comments · Fixed by #14294
Labels
C-bug Category: Clippy is not doing the correct thing

Comments

@GuiAmPm
Copy link

GuiAmPm commented Feb 24, 2025

Summary

A false positive happens when using a DerefMut and changing the inner deref'ed value.

Might be a duplicate of #14020, but the setup is different.

Reproducer

I tried this code:

struct Wrapper<T> {
    t: T,
}

impl<T> Deref for Wrapper<T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.t
    }
}
impl<T> DerefMut for Wrapper<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.t
    }
}

struct Example(bool);

fn do_something(mut a: Wrapper<Example>) {
    a.0 = !a.0;
}

while Cargo.toml has the following configuration:

[lints.clippy]
nursery = { level = "deny", priority = -1}

this happened:

this could be a `const fn`
for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
`-D clippy::missing-const-for-fn` implied by `-D clippy::nursery`
to override `-D clippy::nursery` add `#[allow(clippy::missing_const_for_fn)]`clippy[Click for full compiler diagnostic](rust-analyzer-diagnostics-view:/diagnostic%20message%20[6]?6#
main.rs(23, 4): make the function `const`: ` const`

Adding const to the fn do_something makes the code not to compile.

Version

rustc 1.87.0-nightly (827a0d638 2025-02-18)
binary: rustc
commit-hash: 827a0d638dabc9a22c56f9c37a557568f86ac76c
commit-date: 2025-02-18
host: x86_64-unknown-linux-gnu
release: 1.87.0-nightly
LLVM version: 20.1.0

Additional Labels

No response

@GuiAmPm GuiAmPm added the C-bug Category: Clippy is not doing the correct thing label Feb 24, 2025
github-merge-queue bot pushed a commit that referenced this issue Mar 20, 2025
Closes #14020
Closes #14290
Closes #14091

Add checks for unstable const traits.

changelog: [`missing_const_for_fn`] fix FP on unstable const traits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant