]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/checked_unwrap/simple_conditionals.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / src / tools / clippy / tests / ui / checked_unwrap / simple_conditionals.rs
CommitLineData
f20569fa
XL
1#![deny(clippy::panicking_unwrap, clippy::unnecessary_unwrap)]
2#![allow(clippy::if_same_then_else)]
3
4macro_rules! m {
5 ($a:expr) => {
6 if $a.is_some() {
7 $a.unwrap(); // unnecessary
8 }
9 };
10}
11
12macro_rules! checks_in_param {
13 ($a:expr, $b:expr) => {
14 if $a {
15 $b;
16 }
17 };
18}
19
20macro_rules! checks_unwrap {
21 ($a:expr, $b:expr) => {
22 if $a.is_some() {
23 $b;
24 }
25 };
26}
27
28macro_rules! checks_some {
29 ($a:expr, $b:expr) => {
30 if $a {
31 $b.unwrap();
32 }
33 };
34}
35
36fn main() {
37 let x = Some(());
38 if x.is_some() {
39 x.unwrap(); // unnecessary
40 } else {
41 x.unwrap(); // will panic
42 }
43 if x.is_none() {
44 x.unwrap(); // will panic
45 } else {
46 x.unwrap(); // unnecessary
47 }
48 m!(x);
49 checks_in_param!(x.is_some(), x.unwrap()); // ok
50 checks_unwrap!(x, x.unwrap()); // ok
51 checks_some!(x.is_some(), x); // ok
52 let mut x: Result<(), ()> = Ok(());
53 if x.is_ok() {
54 x.unwrap(); // unnecessary
55 x.unwrap_err(); // will panic
56 } else {
57 x.unwrap(); // will panic
58 x.unwrap_err(); // unnecessary
59 }
60 if x.is_err() {
61 x.unwrap(); // will panic
62 x.unwrap_err(); // unnecessary
63 } else {
64 x.unwrap(); // unnecessary
65 x.unwrap_err(); // will panic
66 }
67 if x.is_ok() {
68 x = Err(());
69 // not unnecessary because of mutation of x
70 // it will always panic but the lint is not smart enough to see this (it only
71 // checks if conditions).
72 x.unwrap();
73 } else {
74 x = Ok(());
75 // not unnecessary because of mutation of x
76 // it will always panic but the lint is not smart enough to see this (it
77 // only checks if conditions).
78 x.unwrap_err();
79 }
80
81 assert!(x.is_ok(), "{:?}", x.unwrap_err()); // ok, it's a common test pattern
82}