]> git.lizzy.rs Git - rust.git/blob - tests/ui/strings.rs
Auto merge of #3603 - xfix:random-state-lint, r=phansch
[rust.git] / tests / ui / strings.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 #[warn(clippy::string_add)]
11 #[allow(clippy::string_add_assign)]
12 fn add_only() {
13     // ignores assignment distinction
14     let mut x = "".to_owned();
15
16     for _ in 1..3 {
17         x = x + ".";
18     }
19
20     let y = "".to_owned();
21     let z = y + "...";
22
23     assert_eq!(&x, &z);
24 }
25
26 #[warn(clippy::string_add_assign)]
27 fn add_assign_only() {
28     let mut x = "".to_owned();
29
30     for _ in 1..3 {
31         x = x + ".";
32     }
33
34     let y = "".to_owned();
35     let z = y + "...";
36
37     assert_eq!(&x, &z);
38 }
39
40 #[warn(clippy::string_add, clippy::string_add_assign)]
41 fn both() {
42     let mut x = "".to_owned();
43
44     for _ in 1..3 {
45         x = x + ".";
46     }
47
48     let y = "".to_owned();
49     let z = y + "...";
50
51     assert_eq!(&x, &z);
52 }
53
54 #[allow(dead_code, unused_variables)]
55 #[warn(clippy::string_lit_as_bytes)]
56 fn str_lit_as_bytes() {
57     let bs = "hello there".as_bytes();
58
59     let bs = r###"raw string with three ### in it and some " ""###.as_bytes();
60
61     // no warning, because this cannot be written as a byte string literal:
62     let ubs = "☃".as_bytes();
63
64     let strify = stringify!(foobar).as_bytes();
65
66     let includestr = include_str!("entry.rs").as_bytes();
67 }
68
69 #[allow(clippy::assign_op_pattern)]
70 fn main() {
71     add_only();
72     add_assign_only();
73     both();
74
75     // the add is only caught for `String`
76     let mut x = 1;
77     x = x + 1;
78     assert_eq!(2, x);
79 }