@@ -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;
@@ -2263,6 +2264,35 @@ declare_clippy_lint! {
22632264 "replace with no effect"
22642265}
22652266
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+
22662296pub struct Methods {
22672297 avoid_breaking_exported_api : bool ,
22682298 msrv : Option < RustcVersion > ,
@@ -2364,6 +2394,7 @@ impl_lint_pass!(Methods => [
23642394 IS_DIGIT_ASCII_RADIX ,
23652395 NEEDLESS_OPTION_TAKE ,
23662396 NO_EFFECT_REPLACE ,
2397+ OBFUSCATED_IF_ELSE ,
23672398] ) ;
23682399
23692400/// Extracts a method call name, args, and `Span` of the method name.
@@ -2772,6 +2803,9 @@ impl Methods {
27722803 Some ( ( "map" , [ m_recv, m_arg] , span) ) => {
27732804 option_map_unwrap_or:: check ( cx, expr, m_recv, m_arg, recv, u_arg, span) ;
27742805 } ,
2806+ Some ( ( "then_some" , [ t_recv, t_arg] , _) ) => {
2807+ obfuscated_if_else:: check ( cx, expr, t_recv, t_arg, u_arg) ;
2808+ } ,
27752809 _ => { } ,
27762810 } ,
27772811 ( "unwrap_or_else" , [ u_arg] ) => match method_call ( recv) {
0 commit comments