]> git.proxmox.com Git - rustc.git/blame - src/tools/clippy/tests/ui/branches_sharing_code/shared_at_top.rs
New upstream version 1.63.0+dfsg1
[rustc.git] / src / tools / clippy / tests / ui / branches_sharing_code / shared_at_top.rs
CommitLineData
923072b8 1#![allow(dead_code, clippy::mixed_read_write_in_expression)]
cdc7bbd5
XL
2#![deny(clippy::if_same_then_else, clippy::branches_sharing_code)]
3
4// This tests the branches_sharing_code lint at the start of blocks
5
6fn simple_examples() {
7 let x = 0;
8
9 // Simple
10 if true {
11 println!("Hello World!");
12 println!("I'm branch nr: 1");
13 } else {
14 println!("Hello World!");
15 println!("I'm branch nr: 2");
16 }
17
18 // Else if
19 if x == 0 {
20 let y = 9;
21 println!("The value y was set to: `{}`", y);
22 let _z = y;
23
24 println!("I'm the true start index of arrays");
25 } else if x == 1 {
26 let y = 9;
27 println!("The value y was set to: `{}`", y);
28 let _z = y;
29
30 println!("I start counting from 1 so my array starts from `1`");
31 } else {
32 let y = 9;
33 println!("The value y was set to: `{}`", y);
34 let _z = y;
35
36 println!("Ha, Pascal allows you to start the array where you want")
37 }
38
39 // Return a value
40 let _ = if x == 7 {
41 let y = 16;
42 println!("What can I say except: \"you're welcome?\"");
43 let _ = y;
44 x
45 } else {
46 let y = 16;
47 println!("Thank you");
48 y
49 };
50}
51
52/// Simple examples where the move can cause some problems due to moved values
53fn simple_but_suggestion_is_invalid() {
54 let x = 10;
55
56 // Can't be automatically moved because used_value_name is getting used again
57 let used_value_name = 19;
58 if x == 10 {
59 let used_value_name = "Different type";
60 println!("Str: {}", used_value_name);
61 let _ = 1;
62 } else {
63 let used_value_name = "Different type";
64 println!("Str: {}", used_value_name);
65 let _ = 2;
66 }
67 let _ = used_value_name;
68
69 // This can be automatically moved as `can_be_overridden` is not used again
70 let can_be_overridden = 8;
71 let _ = can_be_overridden;
72 if x == 11 {
73 let can_be_overridden = "Move me";
74 println!("I'm also moveable");
75 let _ = 111;
76 } else {
77 let can_be_overridden = "Move me";
78 println!("I'm also moveable");
79 let _ = 222;
80 }
81}
82
83/// This function tests that the `IS_SAME_THAN_ELSE` only covers the lint if it's enabled.
84fn check_if_same_than_else_mask() {
85 let x = 2021;
86
87 #[allow(clippy::if_same_then_else)]
88 if x == 2020 {
89 println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint.");
90 println!("Because `IF_SAME_THEN_ELSE` is allowed here");
91 } else {
92 println!("This should trigger the `SHARED_CODE_IN_IF_BLOCKS` lint.");
93 println!("Because `IF_SAME_THEN_ELSE` is allowed here");
94 }
95
96 if x == 2019 {
97 println!("This should trigger `IS_SAME_THAN_ELSE` as usual");
98 } else {
99 println!("This should trigger `IS_SAME_THAN_ELSE` as usual");
100 }
101}
102
103#[allow(clippy::vec_init_then_push)]
104fn pf_local_with_inferred_type_issue7053() {
105 if true {
106 let mut v = Vec::new();
107 v.push(0);
108 } else {
109 let mut v = Vec::new();
110 v.push("");
111 };
112}
113
114fn main() {}