@@ -45,6 +45,7 @@ mod option_as_ref_deref;
4545mod option_map_or_none;
4646mod option_map_unwrap_or;
4747mod or_fun_call;
48+ mod or_then_unwrap;
4849mod search_is_some;
4950mod single_char_add_str;
5051mod single_char_insert_string;
@@ -778,6 +779,42 @@ declare_clippy_lint! {
778779 "using any `*or` method with a function call, which suggests `*or_else`"
779780}
780781
782+ declare_clippy_lint ! {
783+ /// ### What it does
784+ /// Checks for `.or(…).unwrap()` calls to Options and Results.
785+ ///
786+ /// ### Why is this bad?
787+ /// You should use `.unwrap_or(…)` instead for clarity.
788+ ///
789+ /// ### Example
790+ /// ```rust
791+ /// # let fallback = "fallback";
792+ /// // Result
793+ /// # type Error = &'static str;
794+ /// # let result: Result<&str, Error> = Err("error");
795+ /// let value = result.or::<Error>(Ok(fallback)).unwrap();
796+ ///
797+ /// // Option
798+ /// # let option: Option<&str> = None;
799+ /// let value = option.or(Some(fallback)).unwrap();
800+ /// ```
801+ /// Use instead:
802+ /// ```rust
803+ /// # let fallback = "fallback";
804+ /// // Result
805+ /// # let result: Result<&str, &str> = Err("error");
806+ /// let value = result.unwrap_or(fallback);
807+ ///
808+ /// // Option
809+ /// # let option: Option<&str> = None;
810+ /// let value = option.unwrap_or(fallback);
811+ /// ```
812+ #[ clippy:: version = "1.61.0" ]
813+ pub OR_THEN_UNWRAP ,
814+ complexity,
815+ "checks for `.or(…).unwrap()` calls to Options and Results."
816+ }
817+
781818declare_clippy_lint ! {
782819 /// ### What it does
783820 /// Checks for calls to `.expect(&format!(...))`, `.expect(foo(..))`,
@@ -2039,6 +2076,7 @@ impl_lint_pass!(Methods => [
20392076 OPTION_MAP_OR_NONE ,
20402077 BIND_INSTEAD_OF_MAP ,
20412078 OR_FUN_CALL ,
2079+ OR_THEN_UNWRAP ,
20422080 EXPECT_FUN_CALL ,
20432081 CHARS_NEXT_CMP ,
20442082 CHARS_LAST_CMP ,
@@ -2474,6 +2512,9 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
24742512 Some ( ( "get_mut" , [ recv, get_arg] , _) ) => {
24752513 get_unwrap:: check ( cx, expr, recv, get_arg, true ) ;
24762514 } ,
2515+ Some ( ( "or" , [ recv, or_arg] , or_span) ) => {
2516+ or_then_unwrap:: check ( cx, expr, recv, or_arg, or_span) ;
2517+ } ,
24772518 _ => { } ,
24782519 }
24792520 unwrap_used:: check ( cx, expr, recv) ;
0 commit comments