Skip to content

Commit cd1c853

Browse files
committed
[unnecessary_literal_unwrap]: lint unwrap_unchecked
1 parent 3be3fb7 commit cd1c853

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4000,6 +4000,9 @@ impl Methods {
40004000
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
40014001
unwrap_used::check(cx, expr, recv, false, self.allow_unwrap_in_tests);
40024002
},
4003+
("unwrap_unchecked", []) => {
4004+
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
4005+
}
40034006
("unwrap_err", []) => {
40044007
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
40054008
unwrap_used::check(cx, expr, recv, true, self.allow_unwrap_in_tests);

clippy_lints/src/methods/unnecessary_literal_unwrap.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ pub(super) fn check(
6767
(expr.span.with_hi(args[0].span.lo()), "panic!(".to_string()),
6868
(expr.span.with_lo(args[0].span.hi()), ")".to_string()),
6969
]),
70+
("Some" | "Ok", "unwrap_unchecked", _) => {
71+
let mut suggs = vec![
72+
(recv.span.with_hi(call_args[0].span.lo()), String::new()),
73+
(expr.span.with_lo(call_args[0].span.hi()), String::new()),
74+
];
75+
// try to also remove the unsafe block if present
76+
if let hir::Node::Block(block) = cx.tcx.hir().get_parent(expr.hir_id)
77+
&& let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules
78+
{
79+
suggs.extend([
80+
(block.span.shrink_to_lo().to(expr.span.shrink_to_lo()), String::new()),
81+
(expr.span.shrink_to_hi().to(block.span.shrink_to_hi()), String::new())
82+
]);
83+
}
84+
Some(suggs)
85+
},
7086
(_, _, Some(_)) => None,
7187
("Ok", "unwrap_err", None) | ("Err", "unwrap", None) => Some(vec![
7288
(

tests/ui/unnecessary_literal_unwrap.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,21 @@ fn unwrap_from_binding() {
7878
let _ = val.unwrap_or("");
7979
}
8080

81+
fn unwrap_unchecked() {
82+
let _ = 1;
83+
let _ = unsafe { 1 + *(&1 as *const i32) }; // needs to keep the unsafe block
84+
let _ = 1 + 1;
85+
let _ = 1;
86+
let _ = unsafe { 1 + *(&1 as *const i32) };
87+
let _ = 1 + 1;
88+
}
89+
8190
fn main() {
8291
unwrap_option_some();
8392
unwrap_option_none();
8493
unwrap_result_ok();
8594
unwrap_result_err();
8695
unwrap_methods_option();
8796
unwrap_methods_result();
97+
unwrap_unchecked();
8898
}

tests/ui/unnecessary_literal_unwrap.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,21 @@ fn unwrap_from_binding() {
7878
let _ = val.unwrap_or("");
7979
}
8080

81+
fn unwrap_unchecked() {
82+
let _ = unsafe { Some(1).unwrap_unchecked() };
83+
let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
84+
let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
85+
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
86+
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
87+
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
88+
}
89+
8190
fn main() {
8291
unwrap_option_some();
8392
unwrap_option_none();
8493
unwrap_result_ok();
8594
unwrap_result_err();
8695
unwrap_methods_option();
8796
unwrap_methods_result();
97+
unwrap_unchecked();
8898
}

tests/ui/unnecessary_literal_unwrap.stderr

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,5 +409,77 @@ LL - Ok::<_, ()>(1).unwrap_or_else(|_| 2);
409409
LL + 1;
410410
|
411411

412-
error: aborting due to 36 previous errors
412+
error: used `unwrap_unchecked()` on `Some` value
413+
--> $DIR/unnecessary_literal_unwrap.rs:82:22
414+
|
415+
LL | let _ = unsafe { Some(1).unwrap_unchecked() };
416+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
417+
|
418+
help: remove the `Some` and `unwrap_unchecked()`
419+
|
420+
LL - let _ = unsafe { Some(1).unwrap_unchecked() };
421+
LL + let _ = 1;
422+
|
423+
424+
error: used `unwrap_unchecked()` on `Some` value
425+
--> $DIR/unnecessary_literal_unwrap.rs:83:22
426+
|
427+
LL | let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
428+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
429+
|
430+
help: remove the `Some` and `unwrap_unchecked()`
431+
|
432+
LL - let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
433+
LL + let _ = unsafe { 1 + *(&1 as *const i32) }; // needs to keep the unsafe block
434+
|
435+
436+
error: used `unwrap_unchecked()` on `Some` value
437+
--> $DIR/unnecessary_literal_unwrap.rs:84:22
438+
|
439+
LL | let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
440+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
441+
|
442+
help: remove the `Some` and `unwrap_unchecked()`
443+
|
444+
LL - let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
445+
LL + let _ = 1 + 1;
446+
|
447+
448+
error: used `unwrap_unchecked()` on `Ok` value
449+
--> $DIR/unnecessary_literal_unwrap.rs:85:22
450+
|
451+
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
452+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
453+
|
454+
help: remove the `Ok` and `unwrap_unchecked()`
455+
|
456+
LL - let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
457+
LL + let _ = 1;
458+
|
459+
460+
error: used `unwrap_unchecked()` on `Ok` value
461+
--> $DIR/unnecessary_literal_unwrap.rs:86:22
462+
|
463+
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
464+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
465+
|
466+
help: remove the `Ok` and `unwrap_unchecked()`
467+
|
468+
LL - let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
469+
LL + let _ = unsafe { 1 + *(&1 as *const i32) };
470+
|
471+
472+
error: used `unwrap_unchecked()` on `Ok` value
473+
--> $DIR/unnecessary_literal_unwrap.rs:87:22
474+
|
475+
LL | let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
476+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
477+
|
478+
help: remove the `Ok` and `unwrap_unchecked()`
479+
|
480+
LL - let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
481+
LL + let _ = 1 + 1;
482+
|
483+
484+
error: aborting due to 42 previous errors
413485

0 commit comments

Comments
 (0)