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