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