Skip to content

[unnecessary_literal_unwrap]: also lint unwrap_(err_)unchecked #11098

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

Merged
merged 2 commits into from
Jul 12, 2023
Merged
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
2 changes: 1 addition & 1 deletion clippy_lints/src/methods/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4019,7 +4019,7 @@ impl Methods {
}
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
},
("unwrap_or_default", []) => {
("unwrap_or_default" | "unwrap_unchecked" | "unwrap_err_unchecked", []) => {
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
}
("unwrap_or_else", [u_arg]) => {
Expand Down
16 changes: 16 additions & 0 deletions clippy_lints/src/methods/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ pub(super) fn check(
(expr.span.with_hi(args[0].span.lo()), "panic!(".to_string()),
(expr.span.with_lo(args[0].span.hi()), ")".to_string()),
]),
("Some" | "Ok", "unwrap_unchecked", _) | ("Err", "unwrap_err_unchecked", _) => {
let mut suggs = vec![
(recv.span.with_hi(call_args[0].span.lo()), String::new()),
(expr.span.with_lo(call_args[0].span.hi()), String::new()),
];
// try to also remove the unsafe block if present
if let hir::Node::Block(block) = cx.tcx.hir().get_parent(expr.hir_id)
&& let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules
{
suggs.extend([
(block.span.shrink_to_lo().to(expr.span.shrink_to_lo()), String::new()),
(expr.span.shrink_to_hi().to(block.span.shrink_to_hi()), String::new())
]);
}
Some(suggs)
},
(_, _, Some(_)) => None,
("Ok", "unwrap_err", None) | ("Err", "unwrap", None) => Some(vec![
(
Expand Down
11 changes: 11 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,22 @@ fn unwrap_from_binding() {
let _ = val.unwrap_or("");
}

fn unwrap_unchecked() {
let _ = 1;
let _ = unsafe { 1 + *(&1 as *const i32) }; // needs to keep the unsafe block
let _ = 1 + 1;
let _ = 1;
let _ = unsafe { 1 + *(&1 as *const i32) };
let _ = 1 + 1;
let _ = 123;
}

fn main() {
unwrap_option_some();
unwrap_option_none();
unwrap_result_ok();
unwrap_result_err();
unwrap_methods_option();
unwrap_methods_result();
unwrap_unchecked();
}
11 changes: 11 additions & 0 deletions tests/ui/unnecessary_literal_unwrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,22 @@ fn unwrap_from_binding() {
let _ = val.unwrap_or("");
}

fn unwrap_unchecked() {
let _ = unsafe { Some(1).unwrap_unchecked() };
let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
let _ = unsafe { Err::<(), i32>(123).unwrap_err_unchecked() };
}

fn main() {
unwrap_option_some();
unwrap_option_none();
unwrap_result_ok();
unwrap_result_err();
unwrap_methods_option();
unwrap_methods_result();
unwrap_unchecked();
}
86 changes: 85 additions & 1 deletion tests/ui/unnecessary_literal_unwrap.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,89 @@ LL - Ok::<_, ()>(1).unwrap_or_else(|_| 2);
LL + 1;
|

error: aborting due to 36 previous errors
error: used `unwrap_unchecked()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:82:22
|
LL | let _ = unsafe { Some(1).unwrap_unchecked() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Some` and `unwrap_unchecked()`
|
LL - let _ = unsafe { Some(1).unwrap_unchecked() };
LL + let _ = 1;
|

error: used `unwrap_unchecked()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:83:22
|
LL | let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Some` and `unwrap_unchecked()`
|
LL - let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
LL + let _ = unsafe { 1 + *(&1 as *const i32) }; // needs to keep the unsafe block
|

error: used `unwrap_unchecked()` on `Some` value
--> $DIR/unnecessary_literal_unwrap.rs:84:22
|
LL | let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Some` and `unwrap_unchecked()`
|
LL - let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
LL + let _ = 1 + 1;
|

error: used `unwrap_unchecked()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:85:22
|
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Ok` and `unwrap_unchecked()`
|
LL - let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
LL + let _ = 1;
|

error: used `unwrap_unchecked()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:86:22
|
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Ok` and `unwrap_unchecked()`
|
LL - let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
LL + let _ = unsafe { 1 + *(&1 as *const i32) };
|

error: used `unwrap_unchecked()` on `Ok` value
--> $DIR/unnecessary_literal_unwrap.rs:87:22
|
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Ok` and `unwrap_unchecked()`
|
LL - let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
LL + let _ = 1 + 1;
|

error: used `unwrap_err_unchecked()` on `Err` value
--> $DIR/unnecessary_literal_unwrap.rs:88:22
|
LL | let _ = unsafe { Err::<(), i32>(123).unwrap_err_unchecked() };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: remove the `Err` and `unwrap_err_unchecked()`
|
LL - let _ = unsafe { Err::<(), i32>(123).unwrap_err_unchecked() };
LL + let _ = 123;
|

error: aborting due to 43 previous errors