Skip to content

Commit 44c62c9

Browse files
committed
feat: add tests and fix existing ones
1 parent 17ff850 commit 44c62c9

File tree

3 files changed

+22
-6
lines changed

3 files changed

+22
-6
lines changed

clippy_lints/src/use_unwrap_or.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
4747
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
4848
// look for x.or().unwrap()
4949
if_chain! {
50-
if let ExprKind::MethodCall(path, [unwrap_self], unwrap_span) = expr.kind;
50+
if let ExprKind::MethodCall(path, [unwrap_self], unwrap_span) = &expr.kind;
5151
if path.ident.name == sym::unwrap;
52-
if let ExprKind::MethodCall(caller_path, [or_self, or_arg], or_span) = unwrap_self.kind;
52+
if let ExprKind::MethodCall(caller_path, [or_self, or_arg], or_span) = &unwrap_self.kind;
5353
if caller_path.ident.name == sym::or;
5454
then {
55-
let ty = cx.typeck_results().expr_ty(&or_self); // get type of x (we later check if it's Option or Result)
55+
let ty = cx.typeck_results().expr_ty(or_self); // get type of x (we later check if it's Option or Result)
5656
let title;
5757

5858
if is_type_diagnostic_item(cx, ty, sym::Option) {
@@ -74,7 +74,7 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
7474
span_lint_and_help(
7575
cx,
7676
USE_UNWRAP_OR,
77-
or_span.to(unwrap_span),
77+
or_span.to(*unwrap_span),
7878
title,
7979
None,
8080
"use `unwrap_or()` instead"

tests/ui/use_unwrap_or.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![warn(clippy::use_unwrap_or)]
2+
#![allow(clippy::map_identity)]
23

34
struct SomeStruct {}
45
impl SomeStruct {
@@ -8,6 +9,14 @@ impl SomeStruct {
89
fn unwrap(&self) {}
910
}
1011

12+
struct SomeOtherStruct {}
13+
impl SomeOtherStruct {
14+
fn or(self) -> Self {
15+
self
16+
}
17+
fn unwrap(&self) {}
18+
}
19+
1120
fn main() {
1221
let option: Option<&str> = None;
1322
let _ = option.or(Some("fallback")).unwrap(); // should trigger lint
@@ -19,11 +28,18 @@ fn main() {
1928
let instance = SomeStruct {};
2029
let _ = instance.or(Some(SomeStruct {})).unwrap(); // should not trigger lint
2130

31+
let instance = SomeOtherStruct {};
32+
let _ = instance.or().unwrap(); // should not trigger lint and should not panic
33+
2234
// None in or
2335
let option: Option<&str> = None;
2436
let _ = option.or(None).unwrap(); // should not trigger lint
2537

2638
// Not Err in or
2739
let result: Result<&str, &str> = Err("Error");
2840
let _ = result.or::<&str>(Err("Other Error")).unwrap(); // should not trigger lint
41+
42+
// other function between
43+
let option: Option<&str> = None;
44+
let _ = option.or(Some("fallback")).map(|v| v).unwrap(); // should not trigger lint
2945
}

tests/ui/use_unwrap_or.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: .or(Some(…)).unwrap() found
2-
--> $DIR/use_unwrap_or.rs:13:20
2+
--> $DIR/use_unwrap_or.rs:22:20
33
|
44
LL | let _ = option.or(Some("fallback")).unwrap(); // should trigger lint
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -8,7 +8,7 @@ LL | let _ = option.or(Some("fallback")).unwrap(); // should trigger lint
88
= help: use `unwrap_or()` instead
99

1010
error: .or(Ok(…)).unwrap() found
11-
--> $DIR/use_unwrap_or.rs:16:20
11+
--> $DIR/use_unwrap_or.rs:25:20
1212
|
1313
LL | let _ = result.or::<&str>(Ok("fallback")).unwrap(); // should trigger lint
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)