]> git.lizzy.rs Git - rust.git/blob - tests/ui/same_functions_in_if_condition.rs
Auto merge of #9684 - kraktus:ref_option_ref, r=xFrednet
[rust.git] / tests / ui / same_functions_in_if_condition.rs
1 #![feature(adt_const_params)]
2 #![warn(clippy::same_functions_in_if_condition)]
3 // ifs_same_cond warning is different from `ifs_same_cond`.
4 // clippy::if_same_then_else, clippy::comparison_chain -- all empty blocks
5 #![allow(incomplete_features)]
6 #![allow(
7     clippy::comparison_chain,
8     clippy::if_same_then_else,
9     clippy::ifs_same_cond,
10     clippy::uninlined_format_args
11 )]
12
13 fn function() -> bool {
14     true
15 }
16
17 fn fn_arg(_arg: u8) -> bool {
18     true
19 }
20
21 struct Struct;
22
23 impl Struct {
24     fn method(&self) -> bool {
25         true
26     }
27     fn method_arg(&self, _arg: u8) -> bool {
28         true
29     }
30 }
31
32 fn ifs_same_cond_fn() {
33     let a = 0;
34     let obj = Struct;
35
36     if function() {
37     } else if function() {
38         //~ ERROR ifs same condition
39     }
40
41     if fn_arg(a) {
42     } else if fn_arg(a) {
43         //~ ERROR ifs same condition
44     }
45
46     if obj.method() {
47     } else if obj.method() {
48         //~ ERROR ifs same condition
49     }
50
51     if obj.method_arg(a) {
52     } else if obj.method_arg(a) {
53         //~ ERROR ifs same condition
54     }
55
56     let mut v = vec![1];
57     if v.pop().is_none() {
58         //~ ERROR ifs same condition
59     } else if v.pop().is_none() {
60     }
61
62     if v.len() == 42 {
63         //~ ERROR ifs same condition
64     } else if v.len() == 42 {
65     }
66
67     if v.len() == 1 {
68         // ok, different conditions
69     } else if v.len() == 2 {
70     }
71
72     if fn_arg(0) {
73         // ok, different arguments.
74     } else if fn_arg(1) {
75     }
76
77     if obj.method_arg(0) {
78         // ok, different arguments.
79     } else if obj.method_arg(1) {
80     }
81
82     if a == 1 {
83         // ok, warning is on `ifs_same_cond` behalf.
84     } else if a == 1 {
85     }
86 }
87
88 fn main() {
89     // macro as condition (see #6168)
90     let os = if cfg!(target_os = "macos") {
91         "macos"
92     } else if cfg!(target_os = "windows") {
93         "windows"
94     } else {
95         "linux"
96     };
97     println!("{}", os);
98
99     #[derive(PartialEq, Eq)]
100     enum E {
101         A,
102         B,
103     }
104     fn generic<const P: E>() -> bool {
105         match P {
106             E::A => true,
107             E::B => false,
108         }
109     }
110     if generic::<{ E::A }>() {
111         println!("A");
112     } else if generic::<{ E::B }>() {
113         println!("B");
114     }
115 }