@@ -45,6 +45,7 @@ mod option_as_ref_deref;
45
45
mod option_map_or_none;
46
46
mod option_map_unwrap_or;
47
47
mod or_fun_call;
48
+ mod or_then_unwrap;
48
49
mod search_is_some;
49
50
mod single_char_add_str;
50
51
mod single_char_insert_string;
@@ -778,6 +779,42 @@ declare_clippy_lint! {
778
779
"using any `*or` method with a function call, which suggests `*or_else`"
779
780
}
780
781
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
+
781
818
declare_clippy_lint ! {
782
819
/// ### What it does
783
820
/// Checks for calls to `.expect(&format!(...))`, `.expect(foo(..))`,
@@ -2039,6 +2076,7 @@ impl_lint_pass!(Methods => [
2039
2076
OPTION_MAP_OR_NONE ,
2040
2077
BIND_INSTEAD_OF_MAP ,
2041
2078
OR_FUN_CALL ,
2079
+ OR_THEN_UNWRAP ,
2042
2080
EXPECT_FUN_CALL ,
2043
2081
CHARS_NEXT_CMP ,
2044
2082
CHARS_LAST_CMP ,
@@ -2474,6 +2512,9 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
2474
2512
Some ( ( "get_mut" , [ recv, get_arg] , _) ) => {
2475
2513
get_unwrap:: check ( cx, expr, recv, get_arg, true ) ;
2476
2514
} ,
2515
+ Some ( ( "or" , [ recv, or_arg] , or_span) ) => {
2516
+ or_then_unwrap:: check ( cx, expr, recv, or_arg, or_span) ;
2517
+ } ,
2477
2518
_ => { } ,
2478
2519
}
2479
2520
unwrap_used:: check ( cx, expr, recv) ;
0 commit comments