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