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