rust/tests/ui/implied_bounds_in_impls.fixed

69 lines
1.9 KiB
Rust
Raw Normal View History

2023-08-23 08:48:26 -05:00
#![warn(clippy::implied_bounds_in_impls)]
#![allow(dead_code)]
#![feature(return_position_impl_trait_in_trait)]
use std::ops::{Deref, DerefMut};
// Only one bound, nothing to lint.
fn normal_deref<T>(x: T) -> impl Deref<Target = T> {
Box::new(x)
}
// Deref implied by DerefMut
fn deref_derefmut<T>(x: T) -> impl DerefMut<Target = T> {
Box::new(x)
}
trait GenericTrait<T> {}
trait GenericTrait2<V> {}
// U is intentionally at a different "index" in GenericSubtrait than `T` is in GenericTrait,
// so this can be a good test to make sure that the calculations are right (no off-by-one errors,
// ...)
trait GenericSubtrait<T, U, V>: GenericTrait<U> + GenericTrait2<V> {}
impl GenericTrait<i32> for () {}
impl GenericTrait<i64> for () {}
impl<V> GenericTrait2<V> for () {}
impl<V> GenericSubtrait<(), i32, V> for () {}
impl<V> GenericSubtrait<(), i64, V> for () {}
fn generics_implied<U, W>() -> impl GenericSubtrait<U, W, U>
where
(): GenericSubtrait<U, W, U>,
{
}
fn generics_implied_multi<V>() -> impl GenericSubtrait<(), i32, V> {}
fn generics_implied_multi2<T, V>() -> impl GenericSubtrait<(), T, V>
where
(): GenericSubtrait<(), T, V> + GenericTrait<T>,
{
}
// i32 != i64, GenericSubtrait<_, i64, _> does not imply GenericTrait<i32>, don't lint
fn generics_different() -> impl GenericTrait<i32> + GenericSubtrait<(), i64, ()> {}
// i32 == i32, GenericSubtrait<_, i32, _> does imply GenericTrait<i32>, lint
fn generics_same() -> impl GenericSubtrait<(), i32, ()> {}
trait SomeTrait {
// Check that it works in trait declarations.
2023-08-23 09:48:12 -05:00
fn f() -> impl DerefMut<Target = u8>;
2023-08-23 08:48:26 -05:00
}
struct SomeStruct;
impl SomeStruct {
// Check that it works in inherent impl blocks.
fn f() -> impl DerefMut<Target = u8> {
Box::new(123)
}
}
impl SomeTrait for SomeStruct {
// Check that it works in trait impls.
fn f() -> impl DerefMut<Target = u8> {
Box::new(42)
}
}
fn main() {}