-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Labels
A-lintArea: New lintsArea: New lintsC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesCategory: Enhancement of lints, like adding more cases or adding help messages
Description
What it does
This lint disallows usages of the methods using Default
trait when there is an alternative, explicit type for a select number of types which are deemed to have an "obvious" default implementation as well as being easy to type
Methods such as:
Option::unwrap_or_default
->Option::unwrap_or
Result::unwrap_or_default
->Result::unwrap_or
Entry::or_default
->Entry::or_insert
Types affected:
- numbers:
0
or0.0
&str
:""
bool
:false
char
:'\0'
Option<T>
:None
I would avoid implementing this for even slightly more involved types like the NonZero family as:
- more typing involved
- has to bring the respective type into scope
category: complexity
lint name: or_default_for_simple_type
Advantage
- Reduces cognitive complexity. You have to think about the type less
- You don't have to know the type. It is useful when reviewing code on github
- Is more concise to type
- Everyone knows what the defaults for these types are
Drawbacks
- Some people may prefer using
Default
trait
Example
let a: Option<u8> = None;
let b = a.unwrap_or_default();
Could be written as:
let a: Option<u8> = None;
let b = a.unwrap_or(0);
paolobarbolinilukaslueg
Metadata
Metadata
Assignees
Labels
A-lintArea: New lintsArea: New lintsC-enhancementCategory: Enhancement of lints, like adding more cases or adding help messagesCategory: Enhancement of lints, like adding more cases or adding help messages