Skip to content

make unwrap_used also trigger on .get().unwrap() #8125

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2314,10 +2314,13 @@ fn check_methods<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, msrv: Optio
("to_os_string" | "to_owned" | "to_path_buf" | "to_vec", []) => {
implicit_clone::check(cx, name, expr, recv, span);
},
("unwrap", []) => match method_call!(recv) {
Some(("get", [recv, get_arg], _)) => get_unwrap::check(cx, expr, recv, get_arg, false),
Some(("get_mut", [recv, get_arg], _)) => get_unwrap::check(cx, expr, recv, get_arg, true),
_ => unwrap_used::check(cx, expr, recv),
("unwrap", []) => {
match method_call!(recv) {
Some(("get", [recv, get_arg], _)) => get_unwrap::check(cx, expr, recv, get_arg, false),
Some(("get_mut", [recv, get_arg], _)) => get_unwrap::check(cx, expr, recv, get_arg, true),
_ => {},
}
unwrap_used::check(cx, expr, recv);
},
("unwrap_or", [u_arg]) => match method_call!(recv) {
Some((arith @ ("checked_add" | "checked_sub" | "checked_mul"), [lhs, rhs], _)) => {
Expand Down
42 changes: 42 additions & 0 deletions tests/ui/unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,49 @@ fn unwrap_result() {
let _ = res.unwrap();
}

fn unwrap_get() {
let v = vec![1, 2, 3];
let _ = v.get(0).unwrap();
}

fn unwrap_get_mut() {
let mut v = vec![1, 2, 3];
let _ = v.get_mut(0).unwrap();
}

#[warn(clippy::get_unwrap)]
fn unwrap_get2() {
let v = vec![1, 2, 3];
let _ = v.get(0).unwrap();
}

#[warn(clippy::get_unwrap)]
fn unwrap_get_mut2() {
let mut v = vec![1, 2, 3];
let _ = v.get_mut(0).unwrap();
}

#[warn(clippy::get_unwrap)]
#[allow(clippy::unwrap_used)]
fn unwrap_get3() {
let v = vec![1, 2, 3];
let _ = v.get(0).unwrap();
}

#[warn(clippy::get_unwrap)]
#[allow(clippy::unwrap_used)]
fn unwrap_get_mut3() {
let mut v = vec![1, 2, 3];
let _ = v.get_mut(0).unwrap();
}
Comment on lines +23 to +47
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have to tet get_unwrap in this file. It's tested in tests/ui/get_unwrap.rs.


fn main() {
unwrap_option();
unwrap_result();
unwrap_get();
unwrap_get_mut();
unwrap_get2();
unwrap_get_mut2();
unwrap_get3();
unwrap_get_mut3();
}
60 changes: 59 additions & 1 deletion tests/ui/unwrap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,63 @@ LL | let _ = res.unwrap();
|
= help: if you don't want to handle the `Err` case gracefully, consider using `expect()` to provide a better panic message

error: aborting due to 2 previous errors
error: used `unwrap()` on `an Option` value
--> $DIR/unwrap.rs:15:13
|
LL | let _ = v.get(0).unwrap();
| ^^^^^^^^^^^^^^^^^
|
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message

error: used `unwrap()` on `an Option` value
--> $DIR/unwrap.rs:20:13
|
LL | let _ = v.get_mut(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message

error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
--> $DIR/unwrap.rs:26:13
|
LL | let _ = v.get(0).unwrap();
| ^^^^^^^^^^^^^^^^^ help: try this: `&v[0]`
|
= note: `-D clippy::get-unwrap` implied by `-D warnings`

error: used `unwrap()` on `an Option` value
--> $DIR/unwrap.rs:26:13
|
LL | let _ = v.get(0).unwrap();
| ^^^^^^^^^^^^^^^^^
|
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message

error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
--> $DIR/unwrap.rs:32:13
|
LL | let _ = v.get_mut(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut v[0]`

error: used `unwrap()` on `an Option` value
--> $DIR/unwrap.rs:32:13
|
LL | let _ = v.get_mut(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^
|
= help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message

error: called `.get().unwrap()` on a Vec. Using `[]` is more clear and more concise
--> $DIR/unwrap.rs:39:13
|
LL | let _ = v.get(0).unwrap();
| ^^^^^^^^^^^^^^^^^ help: try this: `&v[0]`

error: called `.get_mut().unwrap()` on a Vec. Using `[]` is more clear and more concise
--> $DIR/unwrap.rs:46:13
|
LL | let _ = v.get_mut(0).unwrap();
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `&mut v[0]`

error: aborting due to 10 previous errors