Skip to content

Commit

Permalink
Implement the lint for expect
Browse files Browse the repository at this point in the history
  • Loading branch information
pksunkara committed Jun 12, 2023
1 parent 0b1bb5f commit 21b88ce
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 9 additions & 4 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3658,10 +3658,15 @@ impl Methods {
case_sensitive_file_extension_comparisons::check(cx, expr, span, recv, arg);
}
},
("expect", [_]) => match method_call(recv) {
Some(("ok", recv, [], _, _)) => ok_expect::check(cx, expr, recv),
Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, span, err_span, &self.msrv),
_ => expect_used::check(cx, expr, recv, false, self.allow_expect_in_tests),
("expect", [_]) => {
match method_call(recv) {
Some(("ok", recv, [], _, _)) => ok_expect::check(cx, expr, recv),
Some(("err", recv, [], err_span, _)) => err_expect::check(cx, expr, recv, span, err_span, &self.msrv),
_ => expect_used::check(cx, expr, recv, false, self.allow_expect_in_tests),
}
if let ExprKind::Call(recv, _) = recv.kind {
unnecessary_literal_unwrap::check(cx, expr, recv, name);
}
},
("expect_err", [_]) => expect_used::check(cx, expr, recv, true, self.allow_expect_in_tests),
("extend", [arg]) => {
Expand Down
10 changes: 9 additions & 1 deletion tests/ui/unnecessary_literal_unwrap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ LL | let val = Some(1).unwrap();
= help:
= note: `-D clippy::unnecessary-literal-unwrap` implied by `-D warnings`

error: aborting due to previous error
error: used `expect()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:5:15
|
LL | let val = Some(1).expect("this never happens");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help:

error: aborting due to 2 previous errors

0 comments on commit 21b88ce

Please sign in to comment.