rust-unofficial / patterns

A catalogue of Rust design patterns, anti-patterns and idioms
https://rust-unofficial.github.io/patterns/
Mozilla Public License 2.0
7.85k stars 354 forks source link

chore: use `Self` instead of the struct name #284

Closed sassman closed 2 years ago

sassman commented 2 years ago

This PR just fixes a minor but IMO important detail: whenever returning with new or default using Self is preferable

wookietreiber commented 2 years ago

clippy has a check for this, although in nursery ATM, see https://rust-lang.github.io/rust-clippy/master/index.html#use_self

Personally, I'm all for using Self, so :+1:

pickfire commented 2 years ago

I don't think this is a good idea given that it reduces the clarity, I think that's the reason why it is still in nursery.

sassman commented 2 years ago

I don't think this is a good idea given that it reduces the clarity, I think that's the reason why it is still in nursery.

Could you please elaborate what exactly would reduce the clarity?

I'm having a hard time to imagine what clarity you might refer to.

pickfire commented 2 years ago

See https://github.com/rust-unofficial/patterns/issues/145, there's a reason it's still not closed up till now because different people have different opinion.

pickfire commented 2 years ago

Can we revert this pull request given that the lint is still in nursery and no clear benefits that using Self is better over the explicit type?

MarcoIeni commented 2 years ago

Hey pickfire, I think that using Self here is fine, since it's just an example code. That issue is more for the page dedicated to that idiom, I think. Also there is the benefit that when you copy-paste that piece of code you don't have to change the struct name, because Self will work for you. I hope you don't mind that much :)

pickfire commented 2 years ago

Also there is the benefit that when you copy-paste that piece of code you don't have to change the struct name, because Self will work for you.

If your file only have a single impl and is short then yeah, no issue. Imagine if your files is 1000 lines and tons of impl for a single enum or struct, then you can easily get lost on the context, having the return there can give more context. Built docs have the same issue.