For code that calls .or_insert_with(Default::default) on a std::collections::btree_map::Entry or std::collections::hash_map::Entry, clippy should suggest std::collections::btree_map::Entry::or_default and std::collections::hash_map::Entry::or_default
Hey, I'm a new contributor here - is anyone working on this? If not, may I gave it a shot?
Go ahead! Let us know if you need help!
@splashofcrimson I just wanted to let you know that if you're wondering how to get started, I think that the lint that i just wrote is really similar. You can find the code here. Hopefully you find it helpful :-)
If you don't want to do it anymore, I could probably take a shot at it.
Without knowing clippy internals, is it possible to issue a warning for any or_insert_with(MyStruct::default), i.e. not just for Default::default?
I think the right way is to resolve the path and see if it is a Default implementation. Otherwise MyStruct could have its own default function without implementing Default (or worse, while implementing Default another way).
Seems to me no one is working on this, so I'll give it a go today!
I understand we don't have to suggest unwrap_or_default for {Option | Result}.unwrap_or(Default::default()) because that is already covered by or_fun_call, correct?
I understand we don't have to suggest unwrap_or_default for {Option | Result}.unwrap_or(Default::default()) because that is already covered by or_fun_call, correct?
Yes. FYI you can test this quickly in the playground, Clippy can be executed there.
This would not be linted though:
Some(42).unwrap_or_else(Default::default);
This could be improved to suggest unwrap_or_default, but may be the subject of another issue.
And about the maps
map.entry("42").or_insert(Default::default());
Is linted, but recommends or_insert_with(Default::default), which is the subject of this issue. I think it's OK if the suggestion of one lint triggers another lint (as long as it's not circular xD).
This lint should probably be implemented in the methods module.