@@ -10,16 +10,19 @@ use rustc_session::{declare_lint_pass, declare_tool_lint};
10
10
declare_clippy_lint ! {
11
11
/// **What it does:**
12
12
/// Detects people who use `if let Some(v) = ... { y } else { x }`
13
- /// when they could use `Option::map_or` instead.
13
+ /// when they could use `Option::map_or` or `Option::map_or_else`
14
+ /// instead.
14
15
///
15
16
/// **Why is this bad?**
16
17
/// Using the dedicated function in the Option class is clearer and
17
- /// more concise than an if let
18
+ /// more concise than an if let expression.
18
19
///
19
20
/// **Known problems:**
20
- /// Using `Option::map_or` requires it to compute the or value every
21
- /// function call, even if the value is unneeded. In some circumstances,
22
- /// it might be better to use Option::map_or_else. For example,
21
+ /// Using `Option::map_or` (as the lint presently suggests) requires
22
+ /// it to compute the else value every function call, even if the
23
+ /// value is unneeded. It might be better to use Option::map_or_else
24
+ /// if the code is either very expensive or if it changes some state
25
+ /// (i.e. mutates a value). For example,
23
26
/// ```rust
24
27
/// # let optional = Some(0);
25
28
/// let _ = if let Some(x) = optional {
@@ -67,9 +70,10 @@ declare_clippy_lint! {
67
70
68
71
declare_lint_pass ! ( OptionIfLetElse => [ OPTION_IF_LET_ELSE ] ) ;
69
72
70
- /// If this is the option if let/else thing we're detecting, then this
71
- /// function returns Some(Option<_> compared, expression if Option is
72
- /// Some, expression if Option is None). Otherwise, it returns None
73
+ /// If this expression is the option if let/else construct we're
74
+ /// detecting, then this function returns Some(Option<_> compared,
75
+ /// expression if Option is Some, expression if Option is None).
76
+ /// Otherwise, it returns None.
73
77
fn detect_option_if_let_else < ' a > ( cx : & LateContext < ' _ , ' _ > , expr : & ' a Expr < ' _ > ) -> Option < ( String , String , String ) > {
74
78
if_chain ! {
75
79
if let ExprKind :: Match ( let_body, arms, MatchSource :: IfLetDesugar { contains_else_clause: true } ) = & expr. kind;
@@ -112,9 +116,9 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OptionIfLetElse {
112
116
cx,
113
117
OPTION_IF_LET_ELSE ,
114
118
expr. span ,
115
- "use Option::map_or_else instead of an if let/else" ,
119
+ "use Option::map_or instead of an if let/else" ,
116
120
"try" ,
117
- format ! ( "{}.map_or_else ({}, {})" , option, else_func, map) ,
121
+ format ! ( "{}.map_or ({}, {})" , option, else_func, map) ,
118
122
Applicability :: MaybeIncorrect ,
119
123
) ;
120
124
}
0 commit comments