@@ -46,6 +46,7 @@ mod map_unwrap_or;
46
46
mod needless_option_as_deref;
47
47
mod needless_option_take;
48
48
mod no_effect_replace;
49
+ mod obfuscated_if_else;
49
50
mod ok_expect;
50
51
mod option_as_ref_deref;
51
52
mod option_map_or_none;
@@ -2263,6 +2264,35 @@ declare_clippy_lint! {
2263
2264
"replace with no effect"
2264
2265
}
2265
2266
2267
+ declare_clippy_lint ! {
2268
+ /// ### What it does
2269
+ /// Checks for usages of `.then_some(..).unwrap_or(..)`
2270
+ ///
2271
+ /// ### Why is this bad?
2272
+ /// This can be written more clearly with `if .. else ..`
2273
+ ///
2274
+ /// ### Limitations
2275
+ /// This lint currently only looks for usages of
2276
+ /// `.then_some(..).unwrap_or(..)`, but will be expanded
2277
+ /// to account for similar patterns.
2278
+ ///
2279
+ /// ### Example
2280
+ /// ```rust
2281
+ /// let x = true;
2282
+ /// x.then_some("a").unwrap_or("b");
2283
+ /// ```
2284
+ /// Use instead:
2285
+ /// ```rust
2286
+ /// let x = true;
2287
+ /// if x { "a" } else { "b" };
2288
+ /// ```
2289
+ #[ clippy:: version = "1.64.0" ]
2290
+ pub OBFUSCATED_IF_ELSE ,
2291
+ style,
2292
+ "use of `.then_some(..).unwrap_or(..)` can be written \
2293
+ more clearly with `if .. else ..`"
2294
+ }
2295
+
2266
2296
pub struct Methods {
2267
2297
avoid_breaking_exported_api : bool ,
2268
2298
msrv : Option < RustcVersion > ,
@@ -2364,6 +2394,7 @@ impl_lint_pass!(Methods => [
2364
2394
IS_DIGIT_ASCII_RADIX ,
2365
2395
NEEDLESS_OPTION_TAKE ,
2366
2396
NO_EFFECT_REPLACE ,
2397
+ OBFUSCATED_IF_ELSE ,
2367
2398
] ) ;
2368
2399
2369
2400
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2772,6 +2803,9 @@ impl Methods {
2772
2803
Some ( ( "map" , [ m_recv, m_arg] , span) ) => {
2773
2804
option_map_unwrap_or:: check ( cx, expr, m_recv, m_arg, recv, u_arg, span) ;
2774
2805
} ,
2806
+ Some ( ( "then_some" , [ t_recv, t_arg] , _) ) => {
2807
+ obfuscated_if_else:: check ( cx, expr, t_recv, t_arg, u_arg) ;
2808
+ } ,
2775
2809
_ => { } ,
2776
2810
} ,
2777
2811
( "unwrap_or_else" , [ u_arg] ) => match method_call ( recv) {
0 commit comments