]> git.lizzy.rs Git - rust.git/blob - tests/ui/min_rust_version_attr.rs
add MSRV to more lints specified in #6097
[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 pub fn manual_range_contains() {
77     x >= 8 && x < 12;
78 }
79
80 pub fn use_self() {
81     struct Foo {}
82
83     impl Foo {
84         fn new() -> Foo {
85             Foo {}
86         }
87         fn test() -> Foo {
88             Foo::new()
89         }
90     }
91 }
92
93 fn replace_with_default() {
94     let mut s = String::from("foo");
95     let _ = std::mem::replace(s, String::default());
96 }
97
98 fn map_unwrap_or() {
99     let opt = Some(1);
100
101     // Check for `option.map(_).unwrap_or(_)` use.
102     // Single line case.
103     let _ = opt
104         .map(|x| x + 1)
105         // Should lint even though this call is on a separate line.
106         .unwrap_or(0);
107 }
108
109 fn main() {
110     filter_map_next();
111     checked_conversion();
112     redundant_fieldnames();
113     redundant_static_lifetime();
114     option_as_ref_deref();
115     match_like_matches();
116     match_same_arms();
117     match_same_arms2();
118     manual_strip_msrv();
119     manual_range_contains();
120     use_self();
121     replace_with_default();
122     map_unwrap_or();
123 }
124
125 mod meets_msrv {
126     #![feature(custom_inner_attributes)]
127     #![clippy::msrv = "1.45.0"]
128
129     fn main() {
130         let s = "hello, world!";
131         if s.starts_with("hello, ") {
132             assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
133         }
134     }
135 }
136
137 mod just_under_msrv {
138     #![feature(custom_inner_attributes)]
139     #![clippy::msrv = "1.46.0"]
140
141     fn main() {
142         let s = "hello, world!";
143         if s.starts_with("hello, ") {
144             assert_eq!(s["hello, ".len()..].to_uppercase(), "WORLD!");
145         }
146     }
147 }
148
149 mod just_above_msrv {
150     #![feature(custom_inner_attributes)]
151     #![clippy::msrv = "1.44.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 }