]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/tests/ui/equatable_if_let.rs
Merge commit 'f4850f7292efa33759b4f7f9b7621268979e9914' into clippyup
[rust.git] / src / tools / clippy / tests / ui / equatable_if_let.rs
1 // run-rustfix
2 // aux-build:macro_rules.rs
3
4 #![allow(unused_variables, dead_code, clippy::derive_partial_eq_without_eq)]
5 #![warn(clippy::equatable_if_let)]
6
7 #[macro_use]
8 extern crate macro_rules;
9
10 use std::cmp::Ordering;
11
12 #[derive(PartialEq)]
13 enum Enum {
14     TupleVariant(i32, u64),
15     RecordVariant { a: i64, b: u32 },
16     UnitVariant,
17     Recursive(Struct),
18 }
19
20 #[derive(PartialEq)]
21 struct Struct {
22     a: i32,
23     b: bool,
24 }
25
26 struct NoPartialEqStruct {
27     a: i32,
28     b: bool,
29 }
30
31 enum NotPartialEq {
32     A,
33     B,
34 }
35
36 enum NotStructuralEq {
37     A,
38     B,
39 }
40
41 impl PartialEq for NotStructuralEq {
42     fn eq(&self, _: &NotStructuralEq) -> bool {
43         false
44     }
45 }
46
47 fn main() {
48     let a = 2;
49     let b = 3;
50     let c = Some(2);
51     let d = Struct { a: 2, b: false };
52     let e = Enum::UnitVariant;
53     let f = NotPartialEq::A;
54     let g = NotStructuralEq::A;
55     let h = NoPartialEqStruct { a: 2, b: false };
56
57     // true
58
59     if let 2 = a {}
60     if let Ordering::Greater = a.cmp(&b) {}
61     if let Some(2) = c {}
62     if let Struct { a: 2, b: false } = d {}
63     if let Enum::TupleVariant(32, 64) = e {}
64     if let Enum::RecordVariant { a: 64, b: 32 } = e {}
65     if let Enum::UnitVariant = e {}
66     if let (Enum::UnitVariant, &Struct { a: 2, b: false }) = (e, &d) {}
67
68     // false
69
70     if let 2 | 3 = a {}
71     if let x @ 2 = a {}
72     if let Some(3 | 4) = c {}
73     if let Struct { a, b: false } = d {}
74     if let Struct { a: 2, b: x } = d {}
75     if let NotPartialEq::A = f {}
76     if let NotStructuralEq::A = g {}
77     if let Some(NotPartialEq::A) = Some(f) {}
78     if let Some(NotStructuralEq::A) = Some(g) {}
79     if let NoPartialEqStruct { a: 2, b: false } = h {}
80
81     macro_rules! m1 {
82         (x) => {
83             "abc"
84         };
85     }
86     if let m1!(x) = "abc" {
87         println!("OK");
88     }
89
90     equatable_if_let!(a);
91 }