]> git.lizzy.rs Git - rust.git/blob - tests/ui/lint/unused/issue-47390-unused-variable-in-struct-pattern.rs
Rollup merge of #106670 - albertlarsan68:check-docs-in-pr-ci, r=Mark-Simulacrum
[rust.git] / tests / ui / lint / unused / issue-47390-unused-variable-in-struct-pattern.rs
1 // check-pass
2
3 #![feature(box_patterns)]
4
5 #![warn(unused)] // UI tests pass `-A unused` (#43896)
6
7 struct SoulHistory {
8     corridors_of_light: usize,
9     hours_are_suns: bool,
10     endless_and_singing: bool
11 }
12
13 struct LovelyAmbition {
14     lips: usize,
15     fire: usize
16 }
17
18 #[derive(Clone, Copy)]
19 enum Large {
20     Suit { case: () }
21 }
22
23 struct Tuple(Large, ());
24
25 fn main() {
26     let i_think_continually = 2; //~ WARNING unused variable: `i_think_continually`
27     let who_from_the_womb_remembered = SoulHistory {
28         corridors_of_light: 5,
29         hours_are_suns: true,
30         endless_and_singing: true
31     };
32
33     let mut mut_unused_var = 1;
34     //~^ WARNING unused variable: `mut_unused_var`
35     //~| WARNING variable does not need to be mutable
36
37     let (mut var, unused_var) = (1, 2);
38     //~^ WARNING unused variable: `var`
39     //~| WARNING unused variable: `unused_var`
40     //~| WARNING variable does not need to be mutable
41     // NOTE: `var` comes after `unused_var` lexicographically yet the warning
42     // for `var` will be emitted before the one for `unused_var`. We use an
43     // `IndexMap` to ensure this is the case instead of a `BTreeMap`.
44
45     if let SoulHistory { corridors_of_light, //~ WARNING unused variable: `corridors_of_light`
46                          mut hours_are_suns, //~ WARNING `hours_are_suns` is assigned to, but
47                          endless_and_singing: true } = who_from_the_womb_remembered {
48         hours_are_suns = false; //~ WARNING unused_assignments
49     }
50
51     let the_spirit = LovelyAmbition { lips: 1, fire: 2 };
52     let LovelyAmbition { lips, fire } = the_spirit; //~ WARNING unused variable: `fire`
53     println!("{}", lips);
54
55     let bag = Large::Suit {
56         case: ()
57     };
58
59     // Plain struct
60     match bag {
61         Large::Suit { case } => {} //~ WARNING unused variable: `case`
62     };
63
64     // Referenced struct
65     match &bag {
66         &Large::Suit { case } => {} //~ WARNING unused variable: `case`
67     };
68
69     // Boxed struct
70     match Box::new(bag) {
71         box Large::Suit { case } => {} //~ WARNING unused variable: `case`
72     };
73
74     // Tuple with struct
75     match (bag,) {
76         (Large::Suit { case },) => {} //~ WARNING unused variable: `case`
77     };
78
79     // Slice with struct
80     match [bag] {
81         [Large::Suit { case }] => {} //~ WARNING unused variable: `case`
82     };
83
84     // Tuple struct with struct
85     match Tuple(bag, ()) {
86         Tuple(Large::Suit { case }, ()) => {} //~ WARNING unused variable: `case`
87     };
88 }