Skip to content

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

@GuiAmPm

Description

@GuiAmPm

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions