check that the receiver type satisfies bounds

This commit is contained in:
y21 2023-08-07 04:04:51 +02:00
parent e34e49f7ff
commit cf10690ad4
4 changed files with 148 additions and 63 deletions

View file

@ -206,11 +206,43 @@ mod issue11300 {
assert_eq!(i.len(), 3);
}
trait Helper<T: ?Sized> {}
impl Helper<i32> for [i32; 3] {}
impl Helper<i32> for std::array::IntoIter<i32, 3> {}
impl Helper<()> for std::array::IntoIter<i32, 3> {}
fn foo2<X: ?Sized, I>(_: I)
where
I: IntoIterator<Item = i32> + Helper<X>,
{
}
trait Helper2<T> {}
impl Helper2<std::array::IntoIter<i32, 3>> for i32 {}
impl Helper2<[i32; 3]> for i32 {}
fn foo3<I>(_: I)
where
I: IntoIterator<Item = i32>,
i32: Helper2<I>,
{
}
pub fn bar() {
// This should not trigger the lint:
// `[i32, 3]` does not satisfy the `ExactSizeIterator` bound, so the into_iter call cannot be
// removed and is not useless.
foo([1, 2, 3].into_iter());
// This should trigger the lint, receiver type [i32; 3] also implements `Helper`
foo2::<i32, _>([1, 2, 3]);
// This again should *not* lint, since X = () and I = std::array::IntoIter<i32, 3>,
// and `[i32; 3]: Helper<()>` is not true (only `std::array::IntoIter<i32, 3>: Helper<()>` is).
foo2::<(), _>([1, 2, 3].into_iter());
// This should lint. Removing the `.into_iter()` means that `I` gets substituted with `[i32; 3]`,
// and `i32: Helper2<[i32, 3]>` is true, so this call is indeed unncessary.
foo3([1, 2, 3]);
}
}

View file

@ -206,11 +206,43 @@ mod issue11300 {
assert_eq!(i.len(), 3);
}
trait Helper<T: ?Sized> {}
impl Helper<i32> for [i32; 3] {}
impl Helper<i32> for std::array::IntoIter<i32, 3> {}
impl Helper<()> for std::array::IntoIter<i32, 3> {}
fn foo2<X: ?Sized, I>(_: I)
where
I: IntoIterator<Item = i32> + Helper<X>,
{
}
trait Helper2<T> {}
impl Helper2<std::array::IntoIter<i32, 3>> for i32 {}
impl Helper2<[i32; 3]> for i32 {}
fn foo3<I>(_: I)
where
I: IntoIterator<Item = i32>,
i32: Helper2<I>,
{
}
pub fn bar() {
// This should not trigger the lint:
// `[i32, 3]` does not satisfy the `ExactSizeIterator` bound, so the into_iter call cannot be
// removed and is not useless.
foo([1, 2, 3].into_iter());
// This should trigger the lint, receiver type [i32; 3] also implements `Helper`
foo2::<i32, _>([1, 2, 3].into_iter());
// This again should *not* lint, since X = () and I = std::array::IntoIter<i32, 3>,
// and `[i32; 3]: Helper<()>` is not true (only `std::array::IntoIter<i32, 3>: Helper<()>` is).
foo2::<(), _>([1, 2, 3].into_iter());
// This should lint. Removing the `.into_iter()` means that `I` gets substituted with `[i32; 3]`,
// and `i32: Helper2<[i32, 3]>` is true, so this call is indeed unncessary.
foo3([1, 2, 3].into_iter());
}
}

View file

@ -178,5 +178,29 @@ note: this parameter accepts any `IntoIterator`, so you don't need to call `.int
LL | fn b<T: IntoIterator<Item = i32>>(_: T) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 24 previous errors
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
--> $DIR/useless_conversion.rs:237:24
|
LL | foo2::<i32, _>([1, 2, 3].into_iter());
| ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]`
|
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
--> $DIR/useless_conversion.rs:216:12
|
LL | I: IntoIterator<Item = i32> + Helper<X>,
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: explicit call to `.into_iter()` in function argument accepting `IntoIterator`
--> $DIR/useless_conversion.rs:245:14
|
LL | foo3([1, 2, 3].into_iter());
| ^^^^^^^^^^^^^^^^^^^^^ help: consider removing the `.into_iter()`: `[1, 2, 3]`
|
note: this parameter accepts any `IntoIterator`, so you don't need to call `.into_iter()`
--> $DIR/useless_conversion.rs:225:12
|
LL | I: IntoIterator<Item = i32>,
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 26 previous errors