Skip to content
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

False positive by redundant_closure to remove a closure that is required for lifetime reasons #7497

Closed
weiznich opened this issue Jul 27, 2021 · 1 comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@weiznich
Copy link

weiznich commented Jul 27, 2021

Lint name: redundant_closure

I tried this code:

pub trait Insertable<T> {
    type Values;

    fn values(self) -> Self::Values;
}


pub trait AsValueIterator<Tab> {
    type Item;

    fn as_value_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = <&'a Self::Item as Insertable<Tab>>::Values> + 'a>
    where
        Self::Item: 'a,
        &'a Self::Item: Insertable<Tab>;
}

impl<T, Tab, const N: usize> AsValueIterator<Tab> for [T; N] {
    type Item = T;

    fn as_value_iter<'a>(&'a self) -> Box<dyn Iterator<Item = <&'a T as Insertable<Tab>>::Values> + 'a>
    where
        Self::Item: 'a,
        &'a T: Insertable<Tab>,
    {
        Box::new(IntoIterator::into_iter(self).map(|v| Insertable::values(v)))
    }
}

impl<'b, T, Tab, const N: usize> AsValueIterator<Tab> for &'b [T; N] {
    type Item = T;

    fn as_value_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = <&'a Self::Item as Insertable<Tab>>::Values> + 'a>
    where
        Self::Item: 'a,
        &'a T: Insertable<Tab>,
    {
        Box::new(IntoIterator::into_iter(*self).map(|v| Insertable::values(v)))
    }
}

impl<T, Tab, const N: usize> AsValueIterator<Tab> for Box<[T; N]> {
    type Item = T;

    fn as_value_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = <&'a Self::Item as Insertable<Tab>>::Values> + 'a>
    where
        Self::Item: 'a,
        &'a T: Insertable<Tab>,
    {
        Box::new(IntoIterator::into_iter(&**self).map(|v| Insertable::values(v)))
    }
}

impl<T, Tab> AsValueIterator<Tab> for Vec<T> {
    type Item = T;

    fn as_value_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = <&'a Self::Item as Insertable<Tab>>::Values> + 'a>
    where
        Self::Item: 'a,
        &'a T: Insertable<Tab>,
    {
        Box::new(IntoIterator::into_iter(self).map(|v| Insertable::values(v)))
    }
}

impl<'b, T, Tab> AsValueIterator<Tab> for &'b [T] {
    type Item = T;

    fn as_value_iter<'a>(
        &'a self,
    ) -> Box<dyn Iterator<Item = <&'a Self::Item as Insertable<Tab>>::Values> + 'a>
    where
        Self::Item: 'a,
        &'a T: Insertable<Tab>,
    {
        Box::new(IntoIterator::into_iter(*self).map(|v| Insertable::values(v)))
    }
}

This is a minimized version from real code inside of diesel. See this playground for an interactive version.

This results in the following output:

warning: redundant closure
  --> src/main.rs:30:52
   |
30 |         Box::new(IntoIterator::into_iter(self).map(|v| Insertable::values(v)))
   |                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Insertable::values`
   |
   = note: `#[warn(clippy::redundant_closure)]` on by default
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

warning: redundant closure
  --> src/main.rs:44:53
   |
44 |         Box::new(IntoIterator::into_iter(*self).map(|v| Insertable::values(v)))
   |                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Insertable::values`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

warning: redundant closure
  --> src/main.rs:58:55
   |
58 |         Box::new(IntoIterator::into_iter(&**self).map(|v| Insertable::values(v)))
   |                                                       ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Insertable::values`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

warning: redundant closure
  --> src/main.rs:72:52
   |
72 |         Box::new(IntoIterator::into_iter(self).map(|v| Insertable::values(v)))
   |                                                    ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Insertable::values`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

warning: redundant closure
  --> src/main.rs:86:53
   |
86 |         Box::new(IntoIterator::into_iter(*self).map(|v| Insertable::values(v)))
   |                                                     ^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Insertable::values`
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure

I expected to see this happen: No warning or a suggestion that won't cause a compiler error after applying.
Instead, this happened: Clippy suggested to remove the map closure in every trait implementation. This results in a compiler error (See this playground for a changed version)

Meta

  • cargo clippy -V: 0.1.55 (2021-07-26 08095fc)
  • rustc -Vv:
1.56.0-nightly (2021-07-26 08095fc1f875c89e507f)
(Playground version)
@weiznich weiznich added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jul 27, 2021
@Jarcho
Copy link
Contributor

Jarcho commented Apr 11, 2022

This was fixed by #7661.

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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

3 participants