@@ -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;
@@ -2259,6 +2260,35 @@ declare_clippy_lint! {
2259
2260
"replace with no effect"
2260
2261
}
2261
2262
2263
+ declare_clippy_lint ! {
2264
+ /// ### What it does
2265
+ /// Checks for usages of `.then_some(..).unwrap_or(..)`
2266
+ ///
2267
+ /// ### Why is this bad?
2268
+ /// This can be written more clearly with `if .. else ..`
2269
+ ///
2270
+ /// ### Limitations
2271
+ /// This lint currently only looks for usages of
2272
+ /// `.then_some(..).unwrap_or(..)`, but will be expanded
2273
+ /// to account for similar patterns.
2274
+ ///
2275
+ /// ### Example
2276
+ /// ```rust
2277
+ /// let x = true;
2278
+ /// x.then_some("a").unwrap_or("b");
2279
+ /// ```
2280
+ /// Use instead:
2281
+ /// ```rust
2282
+ /// let x = true;
2283
+ /// if x { "a" } else { "b" };
2284
+ /// ```
2285
+ #[ clippy:: version = "1.64.0" ]
2286
+ pub OBFUSCATED_IF_ELSE ,
2287
+ style,
2288
+ "use of `.then_some(..).unwrap_or(..)` can be written \
2289
+ more clearly with `if .. else ..`"
2290
+ }
2291
+
2262
2292
pub struct Methods {
2263
2293
avoid_breaking_exported_api : bool ,
2264
2294
msrv : Option < RustcVersion > ,
@@ -2360,6 +2390,7 @@ impl_lint_pass!(Methods => [
2360
2390
IS_DIGIT_ASCII_RADIX ,
2361
2391
NEEDLESS_OPTION_TAKE ,
2362
2392
NO_EFFECT_REPLACE ,
2393
+ OBFUSCATED_IF_ELSE ,
2363
2394
] ) ;
2364
2395
2365
2396
/// Extracts a method call name, args, and `Span` of the method name.
@@ -2768,6 +2799,9 @@ impl Methods {
2768
2799
Some ( ( "map" , [ m_recv, m_arg] , span) ) => {
2769
2800
option_map_unwrap_or:: check ( cx, expr, m_recv, m_arg, recv, u_arg, span) ;
2770
2801
} ,
2802
+ Some ( ( "then_some" , [ t_recv, t_arg] , _) ) => {
2803
+ obfuscated_if_else:: check ( cx, expr, t_recv, t_arg, u_arg) ;
2804
+ } ,
2771
2805
_ => { } ,
2772
2806
} ,
2773
2807
( "unwrap_or_else" , [ u_arg] ) => match method_call ( recv) {
0 commit comments