]> git.lizzy.rs Git - rust.git/blob - tests/ui/min_rust_version_attr.rs
Auto merge of #85538 - r00ster91:iterrepeat, r=Mark-Simulacrum
[rust.git] / tests / ui / min_rust_version_attr.rs
1 #![allow(clippy::redundant_clone)]
2 #![feature(custom_inner_attributes)]
3 #![clippy::msrv = "1.0.0"]
4
5 use std::ops::{Deref, RangeFrom};
6
7 fn cloned_instead_of_copied() {
8     let _ = [1].iter().cloned();
9 }
10
11 fn option_as_ref_deref() {
12     let mut opt = Some(String::from("123"));
13
14     let _ = opt.as_ref().map(String::as_str);
15     let _ = opt.as_ref().map(|x| x.as_str());
16     let _ = opt.as_mut().map(String::as_mut_str);
17     let _ = opt.as_mut().map(|x| x.as_mut_str());
18 }
19
20 fn match_like_matches() {
21     let _y = match Some(5) {
22         Some(0) => true,
23         _ => false,
24     };
25 }
26
27 fn match_same_arms() {
28     match (1, 2, 3) {
29         (1, .., 3) => 42,
30         (.., 3) => 42, //~ ERROR match arms have same body
31         _ => 0,
32     };
33 }
34
35 fn match_same_arms2() {
36     let _ = match Some(42) {
37         Some(_) => 24,
38         None => 24, //~ ERROR match arms have same body
39     };
40 }
41
42 pub fn manual_strip_msrv() {
43     let s = "hello, world!";
44     if s.starts_with("hello, ") {
45         assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
46     }
47 }
48
49 pub fn redundant_fieldnames() {
50     let start = 0;
51     let _ = RangeFrom { start: start };
52 }
53
54 pub fn redundant_static_lifetime() {
55     const VAR_ONE: &'static str = "Test constant #1";
56 }
57
58 pub fn checked_conversion() {
59     let value: i64 = 42;
60     let _ = value <= (u32::max_value() as i64) && value >= 0;
61     let _ = value <= (u32::MAX as i64) && value >= 0;
62 }
63
64 pub struct FromOverInto(String);
65
66 impl Into<FromOverInto> for String {
67     fn into(self) -> FromOverInto {
68         FromOverInto(self)
69     }
70 }
71
72 pub fn filter_map_next() {
73     let a = ["1", "lol", "3", "NaN", "5"];
74
75     #[rustfmt::skip]
76     let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
77         .into_iter()
78         .filter_map(|x| {
79             if x == 2 {
80                 Some(x * 2)
81             } else {
82                 None
83             }
84         })
85         .next();
86 }
87
88 #[allow(clippy::no_effect)]
89 #[allow(clippy::short_circuit_statement)]
90 #[allow(clippy::unnecessary_operation)]
91 pub fn manual_range_contains() {
92     let x = 5;
93     x >= 8 && x < 12;
94 }
95
96 pub fn use_self() {
97     struct Foo {}
98
99     impl Foo {
100         fn new() -> Foo {
101             Foo {}
102         }
103         fn test() -> Foo {
104             Foo::new()
105         }
106     }
107 }
108
109 fn replace_with_default() {
110     let mut s = String::from("foo");
111     let _ = std::mem::replace(&mut s, String::default());
112 }
113
114 fn map_unwrap_or() {
115     let opt = Some(1);
116
117     // Check for `option.map(_).unwrap_or(_)` use.
118     // Single line case.
119     let _ = opt
120         .map(|x| x + 1)
121         // Should lint even though this call is on a separate line.
122         .unwrap_or(0);
123 }
124
125 // Could be const
126 fn missing_const_for_fn() -> i32 {
127     1
128 }
129
130 fn unnest_or_patterns() {
131     struct TS(u8, u8);
132     if let TS(0, x) | TS(1, x) = TS(0, 0) {}
133 }
134
135 fn main() {
136     filter_map_next();
137     checked_conversion();
138     redundant_fieldnames();
139     redundant_static_lifetime();
140     option_as_ref_deref();
141     match_like_matches();
142     match_same_arms();
143     match_same_arms2();
144     manual_strip_msrv();
145     manual_range_contains();
146     use_self();
147     replace_with_default();
148     map_unwrap_or();
149     missing_const_for_fn();
150     unnest_or_patterns();
151 }
152
153 mod meets_msrv {
154     #![feature(custom_inner_attributes)]
155     #![clippy::msrv = "1.45.0"]
156
157     fn main() {
158         let s = "hello, world!";
159         if s.starts_with("hello, ") {
160             assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
161         }
162     }
163 }
164
165 mod just_under_msrv {
166     #![feature(custom_inner_attributes)]
167     #![clippy::msrv = "1.46.0"]
168
169     fn main() {
170         let s = "hello, world!";
171         if s.starts_with("hello, ") {
172             assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
173         }
174     }
175 }
176
177 mod just_above_msrv {
178     #![feature(custom_inner_attributes)]
179     #![clippy::msrv = "1.44.0"]
180
181     fn main() {
182         let s = "hello, world!";
183         if s.starts_with("hello, ") {
184             assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
185         }
186     }
187 }