Skip to content

implied_bounds_in_impls false positive #11435

@feds01

Description

@feds01

Summary

The lint suggests removing a specified bound, however, removing the bound breaks code that then uses the function.

Lint Name

implied_bounds_in_impls

Reproducer

I tried this code:

use std::{
    cell::RefCell,
    fmt,
    ops::{Deref, DerefMut},
};

pub struct HeavyState<T: fmt::Debug> {
    current: RefCell<T>,
}

impl<T: fmt::Debug> HeavyState<T> {
    pub fn get_mut(&self) -> impl Deref<Target = T> + DerefMut + '_ {
        self.current.borrow_mut()
    }
}

pub fn foo(bindings: HeavyState<Vec<u32>>) {
   let mut b = bindings.get_mut();
   b.push(0);
}

fn main() {}

Clippy reports the following:

warning: this bound is already specified as the supertrait of `DerefMut`
  --> src/main.rs:14:35
   |
14 |     pub fn get_mut(&self) -> impl Deref<Target = T> + DerefMut + '_ {
   |                                   ^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#implied_bounds_in_impls
   = note: `#[warn(clippy::implied_bounds_in_impls)]` on by default
help: try removing this bound
   |
14 -     pub fn get_mut(&self) -> impl Deref<Target = T> + DerefMut + '_ {
14 +     pub fn get_mut(&self) -> impl DerefMut + '_ {
   |

However, applying the suggestion results in the following:

error[E0599]: no method named `push` found for opaque type `impl std::ops::DerefMut + '_` in the current scope
  --> src/main.rs:23:7
   |
23 |     b.push(0);
   |       ^^^^ method not found in `impl DerefMut`

The suggestion should be to transfer the associated type bound Target = T onto DerefMut rather than entirely removing the bound. This would be the correct code:

impl<T: fmt::Debug> HeavyState<T> {
    pub fn get_mut(&self) -> impl DerefMut<Target = T> + '_ {
        self.current.borrow_mut()
    }
}

Version

rustc 1.74.0-nightly (84a9f4c6e 2023-08-29)
binary: rustc
commit-hash: 84a9f4c6e6f0f02bff9acc9a5e0305a1554a87fc
commit-date: 2023-08-29
host: x86_64-apple-darwin
release: 1.74.0-nightly
LLVM version: 17.0.0

Additional Labels

@rustbot label +I-suggestion-causes-error

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't haveI-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions