Skip to content

False positive: methods called new usually return Self #1724

@hagsteel

Description

@hagsteel

Consider this (rather silly) code:


struct FooRead<T: Read> {
    inner: T
}

impl<T: Read> Read for FooRead<T> {
    fn read(&mut self, _: &mut [u8]) -> Result<usize, std::io::Error> {
        Ok(1)
    }
}

struct Foo<T: Read> {
    foo: T
}

impl<T: Read> Foo<T> {
    fn new(t: T) -> Foo<FooRead<T>> {
        Foo {
            foo: FooRead { inner: t }
        }
    }
}

If the new function is changed to this:

impl<T: Read> Foo<T> {
    fn new(t: T) -> Self {
        Self {
            foo: FooRead { inner: t }
        }
    }
}

It errors with error[E0308]: mismatched types

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