]> git.lizzy.rs Git - rust.git/blob - tests/ui/min_rust_version_attr.rs
Merge remote-tracking branch 'upstream/master' into rustup
[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 fn filter_map_next() {
61     let a = ["1", "lol", "3", "NaN", "5"];
62
63     #[rustfmt::skip]
64     let _: Option<u32> = vec![1, 2, 3, 4, 5, 6]
65         .into_iter()
66         .filter_map(|x| {
67             if x == 2 {
68                 Some(x * 2)
69             } else {
70                 None
71             }
72         })
73         .next();
74 }
75
76 #[allow(clippy::no_effect)]
77 #[allow(clippy::short_circuit_statement)]
78 #[allow(clippy::unnecessary_operation)]
79 pub fn manual_range_contains() {
80     let x = 5;
81     x >= 8 && x < 12;
82 }
83
84 pub fn use_self() {
85     struct Foo {}
86
87     impl Foo {
88         fn new() -> Foo {
89             Foo {}
90         }
91         fn test() -> Foo {
92             Foo::new()
93         }
94     }
95 }
96
97 fn replace_with_default() {
98     let mut s = String::from("foo");
99     let _ = std::mem::replace(&mut s, String::default());
100 }
101
102 fn map_unwrap_or() {
103     let opt = Some(1);
104
105     // Check for `option.map(_).unwrap_or(_)` use.
106     // Single line case.
107     let _ = opt
108         .map(|x| x + 1)
109         // Should lint even though this call is on a separate line.
110         .unwrap_or(0);
111 }
112
113 // Could be const
114 fn missing_const_for_fn() -> i32 {
115     1
116 }
117
118 fn main() {
119     filter_map_next();
120     checked_conversion();
121     redundant_fieldnames();
122     redundant_static_lifetime();
123     option_as_ref_deref();
124     match_like_matches();
125     match_same_arms();
126     match_same_arms2();
127     manual_strip_msrv();
128     manual_range_contains();
129     use_self();
130     replace_with_default();
131     map_unwrap_or();
132     missing_const_for_fn();
133 }
134
135 mod meets_msrv {
136     #![feature(custom_inner_attributes)]
137     #![clippy::msrv = "1.45.0"]
138
139     fn main() {
140         let s = "hello, world!";
141         if s.starts_with("hello, ") {
142             assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
143         }
144     }
145 }
146
147 mod just_under_msrv {
148     #![feature(custom_inner_attributes)]
149     #![clippy::msrv = "1.46.0"]
150
151     fn main() {
152         let s = "hello, world!";
153         if s.starts_with("hello, ") {
154             assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
155         }
156     }
157 }
158
159 mod just_above_msrv {
160     #![feature(custom_inner_attributes)]
161     #![clippy::msrv = "1.44.0"]
162
163     fn main() {
164         let s = "hello, world!";
165         if s.starts_with("hello, ") {
166             assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
167         }
168     }
169 }