]> git.lizzy.rs Git - rust.git/blob - tests/ui/no_effect.rs
Mark writes to constants as side-effect-less
[rust.git] / tests / ui / no_effect.rs
1 // Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution.
3 //
4 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7 // option. This file may not be copied, modified, or distributed
8 // except according to those terms.
9
10 #![feature(box_syntax)]
11 #![warn(clippy::no_effect)]
12 #![allow(dead_code)]
13 #![allow(path_statements)]
14 #![allow(clippy::deref_addrof)]
15 #![allow(clippy::redundant_field_names)]
16 #![feature(untagged_unions)]
17
18 struct Unit;
19 struct Tuple(i32);
20 struct Struct {
21     field: i32,
22 }
23 enum Enum {
24     Tuple(i32),
25     Struct { field: i32 },
26 }
27 struct DropUnit;
28 impl Drop for DropUnit {
29     fn drop(&mut self) {}
30 }
31 struct DropStruct {
32     field: i32,
33 }
34 impl Drop for DropStruct {
35     fn drop(&mut self) {}
36 }
37 struct DropTuple(i32);
38 impl Drop for DropTuple {
39     fn drop(&mut self) {}
40 }
41 enum DropEnum {
42     Tuple(i32),
43     Struct { field: i32 },
44 }
45 impl Drop for DropEnum {
46     fn drop(&mut self) {}
47 }
48 struct FooString {
49     s: String,
50 }
51 union Union {
52     a: u8,
53     b: f64,
54 }
55
56 fn get_number() -> i32 {
57     0
58 }
59 fn get_struct() -> Struct {
60     Struct { field: 0 }
61 }
62 fn get_drop_struct() -> DropStruct {
63     DropStruct { field: 0 }
64 }
65
66 unsafe fn unsafe_fn() -> i32 {
67     0
68 }
69
70 struct A(i32);
71 struct B {
72     field: i32,
73 }
74 struct C {
75     b: B,
76 }
77 const A_CONST: A = A(1);
78 const B: B = B { field: 1 };
79 const C: C = C { b: B { field: 1 } };
80
81 fn main() {
82     let s = get_struct();
83     let s2 = get_struct();
84
85     0;
86     s2;
87     Unit;
88     Tuple(0);
89     Struct { field: 0 };
90     Struct { ..s };
91     Union { a: 0 };
92     Enum::Tuple(0);
93     Enum::Struct { field: 0 };
94     5 + 6;
95     *&42;
96     &6;
97     (5, 6, 7);
98     box 42;
99     ..;
100     5..;
101     ..5;
102     5..6;
103     5..=6;
104     [42, 55];
105     [42, 55][1];
106     (42, 55).1;
107     [42; 55];
108     [42; 55][13];
109     let mut x = 0;
110     || x += 5;
111     let s: String = "foo".into();
112     FooString { s: s };
113     A_CONST.0 = 2;
114     B.field = 2;
115     C.b.field = 2;
116
117     // Do not warn
118     get_number();
119     unsafe { unsafe_fn() };
120     DropUnit;
121     DropStruct { field: 0 };
122     DropTuple(0);
123     DropEnum::Tuple(0);
124     DropEnum::Struct { field: 0 };
125     let mut a_mut = A(1);
126     a_mut.0 = 2;
127     let mut b_mut = B { field: 1 };
128     b_mut.field = 2;
129     let mut c_mut = C { b: B { field: 1 } };
130     c_mut.b.field = 2;
131 }