@@ -46,6 +46,7 @@ mod map_unwrap_or;
4646mod needless_option_as_deref;
4747mod needless_option_take;
4848mod no_effect_replace;
49+ mod obfuscated_if_else;
4950mod ok_expect;
5051mod option_as_ref_deref;
5152mod option_map_or_none;
@@ -2259,6 +2260,35 @@ declare_clippy_lint! {
22592260 "replace with no effect"
22602261}
22612262
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+
22622292pub struct Methods {
22632293 avoid_breaking_exported_api : bool ,
22642294 msrv : Option < RustcVersion > ,
@@ -2360,6 +2390,7 @@ impl_lint_pass!(Methods => [
23602390 IS_DIGIT_ASCII_RADIX ,
23612391 NEEDLESS_OPTION_TAKE ,
23622392 NO_EFFECT_REPLACE ,
2393+ OBFUSCATED_IF_ELSE ,
23632394] ) ;
23642395
23652396/// Extracts a method call name, args, and `Span` of the method name.
@@ -2768,6 +2799,9 @@ impl Methods {
27682799 Some ( ( "map" , [ m_recv, m_arg] , span) ) => {
27692800 option_map_unwrap_or:: check ( cx, expr, m_recv, m_arg, recv, u_arg, span) ;
27702801 } ,
2802+ Some ( ( "then_some" , [ t_recv, t_arg] , _) ) => {
2803+ obfuscated_if_else:: check ( cx, expr, t_recv, t_arg, u_arg) ;
2804+ } ,
27712805 _ => { } ,
27722806 } ,
27732807 ( "unwrap_or_else" , [ u_arg] ) => match method_call ( recv) {
0 commit comments