Skip to content

Commit 3f00f07

Browse files
committed
fix: fix tests
1 parent f49a2c3 commit 3f00f07

File tree

3 files changed

+15
-17
lines changed

3 files changed

+15
-17
lines changed

clippy_lints/src/use_unwrap_or.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1+
use clippy_utils::diagnostics::span_lint_and_help;
2+
use clippy_utils::ty::is_type_diagnostic_item;
13
use if_chain::if_chain;
2-
use rustc_hir::*;
4+
use rustc_hir::{Expr, ExprKind, QPath};
35
use rustc_lint::{LateContext, LateLintPass};
46
use rustc_session::{declare_lint_pass, declare_tool_lint};
5-
use rustc_span::{Span, sym};
6-
use clippy_utils::diagnostics::span_lint_and_help;
7-
use clippy_utils::ty::is_type_diagnostic_item;
8-
7+
use rustc_span::{sym, Span};
98

109
declare_clippy_lint! {
1110
/// ### What it does
@@ -39,7 +38,6 @@ declare_lint_pass!(UseUnwrapOr => [USE_UNWRAP_OR]);
3938

4039
impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
4140
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
42-
4341
// look for x.or().unwrap()
4442
if_chain! {
4543
if let ExprKind::MethodCall(path, args, unwrap_span) = expr.kind;
@@ -52,13 +50,13 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
5250
let title;
5351
let arg = &caller_args[1]; // the argument or(xyz) is called with
5452

55-
if is_type_diagnostic_item(&cx, ty, sym::Option) {
53+
if is_type_diagnostic_item(cx, ty, sym::Option) {
5654
title = ".or(Some(…)).unwrap() found";
5755
if !is(arg, "Some") {
5856
return;
5957
}
6058

61-
} else if is_type_diagnostic_item(&cx, ty, sym::Result) {
59+
} else if is_type_diagnostic_item(cx, ty, sym::Result) {
6260
title = ".or(Ok(…)).unwrap() found";
6361
if !is(arg, "Ok") {
6462
return;
@@ -90,16 +88,14 @@ impl<'tcx> LateLintPass<'tcx> for UseUnwrapOr {
9088
fn is<'a>(expr: &Expr<'a>, name: &str) -> bool {
9189
if_chain! {
9290
if let ExprKind::Call(some_expr, _some_args) = expr.kind;
93-
if let ExprKind::Path(path) = &some_expr.kind;
94-
if let QPath::Resolved(_, path) = path;
91+
if let ExprKind::Path(QPath::Resolved(_, path)) = &some_expr.kind;
9592
if let Some(path_segment) = path.segments.first();
9693
if path_segment.ident.name.as_str() == name;
9794
then {
98-
return true;
95+
true
9996
}
10097
else {
101-
return false;
98+
false
10299
}
103100
}
104101
}
105-

tests/ui/use_unwrap_or.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
struct SomeStruct {}
44
impl SomeStruct {
5-
fn or(self, _: Option<Self>) -> Self { self }
6-
fn unwrap(&self){}
5+
fn or(self, _: Option<Self>) -> Self {
6+
self
7+
}
8+
fn unwrap(&self) {}
79
}
810

911
fn main() {

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:11:20
2+
--> $DIR/use_unwrap_or.rs:13: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:14:20
11+
--> $DIR/use_unwrap_or.rs:16:20
1212
|
1313
LL | let _ = result.or::<&str>(Ok("fallback")).unwrap(); // should trigger lint
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)