Skip to content

Commit 17d3828

Browse files
committed
[unnecessary_literal_unwrap]: lint unwrap_unchecked
1 parent ba3bd8f commit 17d3828

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
@@ -3966,6 +3966,9 @@ impl Methods {
39663966
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
39673967
unwrap_used::check(cx, expr, recv, false, self.allow_unwrap_in_tests);
39683968
},
3969+
("unwrap_unchecked", []) => {
3970+
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
3971+
}
39693972
("unwrap_err", []) => {
39703973
unnecessary_literal_unwrap::check(cx, expr, recv, name, args);
39713974
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
@@ -62,6 +62,22 @@ pub(super) fn check(
6262
(expr.span.with_hi(args[0].span.lo()), "panic!(".to_string()),
6363
(expr.span.with_lo(args[0].span.hi()), ")".to_string()),
6464
]),
65+
("Some" | "Ok", "unwrap_unchecked", _) => {
66+
let mut suggs = vec![
67+
(recv.span.with_hi(call_args[0].span.lo()), String::new()),
68+
(expr.span.with_lo(call_args[0].span.hi()), String::new()),
69+
];
70+
// try to also remove the unsafe block if present
71+
if let hir::Node::Block(block) = cx.tcx.hir().get_parent(expr.hir_id)
72+
&& let hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::UserProvided) = block.rules
73+
{
74+
suggs.extend([
75+
(block.span.shrink_to_lo().to(expr.span.shrink_to_lo()), String::new()),
76+
(expr.span.shrink_to_hi().to(block.span.shrink_to_hi()), String::new())
77+
]);
78+
}
79+
Some(suggs)
80+
},
6581
(_, _, Some(_)) => None,
6682
("Ok", "unwrap_err", None) | ("Err", "unwrap", None) => Some(vec![
6783
(

tests/ui/unnecessary_literal_unwrap.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,21 @@ fn unwrap_methods_result() {
6868
1;
6969
}
7070

71+
fn unwrap_unchecked() {
72+
let _ = 1;
73+
let _ = unsafe { 1 + *(&1 as *const i32) }; // needs to keep the unsafe block
74+
let _ = 1 + 1;
75+
let _ = 1;
76+
let _ = unsafe { 1 + *(&1 as *const i32) };
77+
let _ = 1 + 1;
78+
}
79+
7180
fn main() {
7281
unwrap_option_some();
7382
unwrap_option_none();
7483
unwrap_result_ok();
7584
unwrap_result_err();
7685
unwrap_methods_option();
7786
unwrap_methods_result();
87+
unwrap_unchecked();
7888
}

tests/ui/unnecessary_literal_unwrap.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,21 @@ fn unwrap_methods_result() {
6868
Ok::<_, ()>(1).unwrap_or_else(|_| 2);
6969
}
7070

71+
fn unwrap_unchecked() {
72+
let _ = unsafe { Some(1).unwrap_unchecked() };
73+
let _ = unsafe { Some(1).unwrap_unchecked() + *(&1 as *const i32) }; // needs to keep the unsafe block
74+
let _ = unsafe { Some(1).unwrap_unchecked() } + 1;
75+
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() };
76+
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() + *(&1 as *const i32) };
77+
let _ = unsafe { Ok::<_, ()>(1).unwrap_unchecked() } + 1;
78+
}
79+
7180
fn main() {
7281
unwrap_option_some();
7382
unwrap_option_none();
7483
unwrap_result_ok();
7584
unwrap_result_err();
7685
unwrap_methods_option();
7786
unwrap_methods_result();
87+
unwrap_unchecked();
7888
}

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:72: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:73: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:74: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:75: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:76: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:77: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)