@@ -73,6 +73,7 @@ mod map_collect_result_unit;
7373mod map_err_ignore;
7474mod map_flatten;
7575mod map_identity;
76+ mod map_or_identity;
7677mod map_unwrap_or;
7778mod map_with_unused_argument_over_ranges;
7879mod mut_mutex_lock;
@@ -4714,6 +4715,29 @@ declare_clippy_lint! {
47144715 "filtering `std::io::Lines` with `filter_map()`, `flat_map()`, or `flatten()` might cause an infinite loop"
47154716}
47164717
4718+ declare_clippy_lint ! {
4719+ /// ### What it does
4720+ /// Checks the usage of `.map_or(...)` with an identity function for `Option` and `Result` types.
4721+ ///
4722+ /// ### Why is this bad?
4723+ /// This can be written more concisely by using `unwrap_or()`.
4724+ ///
4725+ /// ### Example
4726+ /// ```no_run
4727+ /// let opt = Some(1);
4728+ /// opt.map_or(42, |v| v);
4729+ /// ```
4730+ /// Use instead:
4731+ /// ```no_run
4732+ /// let opt = Some(1);
4733+ /// opt.unwrap_or(42);
4734+ /// ```
4735+ #[ clippy:: version = "1.93.0" ]
4736+ pub MAP_OR_IDENTITY ,
4737+ suspicious,
4738+ "using an identity function when mapping with `.map_or(|err| ..., |x| x)`"
4739+ }
4740+
47174741#[ expect( clippy:: struct_excessive_bools) ]
47184742pub struct Methods {
47194743 avoid_breaking_exported_api : bool ,
@@ -4897,6 +4921,7 @@ impl_lint_pass!(Methods => [
48974921 REDUNDANT_ITER_CLONED ,
48984922 UNNECESSARY_OPTION_MAP_OR_ELSE ,
48994923 LINES_FILTER_MAP_OK ,
4924+ MAP_OR_IDENTITY ,
49004925] ) ;
49014926
49024927/// Extracts a method call name, args, and `Span` of the method name.
@@ -5368,6 +5393,7 @@ impl Methods {
53685393 option_map_or_none:: check ( cx, expr, recv, def, map) ;
53695394 manual_ok_or:: check ( cx, expr, recv, def, map) ;
53705395 unnecessary_map_or:: check ( cx, expr, recv, def, map, span, self . msrv ) ;
5396+ map_or_identity:: check ( cx, expr, recv, def, map) ;
53715397 } ,
53725398 ( sym:: map_or_else, [ def, map] ) => {
53735399 result_map_or_else_none:: check ( cx, expr, recv, def, map) ;
0 commit comments