@@ -58,7 +58,7 @@ pub fn create(
58
58
} ;
59
59
60
60
create_lint ( & lint, msrv) . context ( "Unable to create lint implementation" ) ?;
61
- create_test ( & lint) . context ( "Unable to create a test for the new lint" ) ?;
61
+ create_test ( & lint, msrv ) . context ( "Unable to create a test for the new lint" ) ?;
62
62
63
63
if lint. ty . is_none ( ) {
64
64
add_lint ( & lint, msrv) . context ( "Unable to add lint to clippy_lints/src/lib.rs" ) ?;
@@ -88,15 +88,21 @@ fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
88
88
}
89
89
}
90
90
91
- fn create_test ( lint : & LintData < ' _ > ) -> io:: Result < ( ) > {
92
- fn create_project_layout < P : Into < PathBuf > > ( lint_name : & str , location : P , case : & str , hint : & str ) -> io:: Result < ( ) > {
91
+ fn create_test ( lint : & LintData < ' _ > , msrv : bool ) -> io:: Result < ( ) > {
92
+ fn create_project_layout < P : Into < PathBuf > > (
93
+ lint_name : & str ,
94
+ location : P ,
95
+ case : & str ,
96
+ hint : & str ,
97
+ msrv : bool ,
98
+ ) -> io:: Result < ( ) > {
93
99
let mut path = location. into ( ) . join ( case) ;
94
100
fs:: create_dir ( & path) ?;
95
101
write_file ( path. join ( "Cargo.toml" ) , get_manifest_contents ( lint_name, hint) ) ?;
96
102
97
103
path. push ( "src" ) ;
98
104
fs:: create_dir ( & path) ?;
99
- write_file ( path. join ( "main.rs" ) , get_test_file_contents ( lint_name) ) ?;
105
+ write_file ( path. join ( "main.rs" ) , get_test_file_contents ( lint_name, msrv ) ) ?;
100
106
101
107
Ok ( ( ) )
102
108
}
@@ -106,13 +112,25 @@ fn create_test(lint: &LintData<'_>) -> io::Result<()> {
106
112
let test_dir = lint. project_root . join ( & relative_test_dir) ;
107
113
fs:: create_dir ( & test_dir) ?;
108
114
109
- create_project_layout ( lint. name , & test_dir, "fail" , "Content that triggers the lint goes here" ) ?;
110
- create_project_layout ( lint. name , & test_dir, "pass" , "This file should not trigger the lint" ) ?;
115
+ create_project_layout (
116
+ lint. name ,
117
+ & test_dir,
118
+ "fail" ,
119
+ "Content that triggers the lint goes here" ,
120
+ msrv,
121
+ ) ?;
122
+ create_project_layout (
123
+ lint. name ,
124
+ & test_dir,
125
+ "pass" ,
126
+ "This file should not trigger the lint" ,
127
+ false ,
128
+ ) ?;
111
129
112
130
println ! ( "Generated test directories: `{relative_test_dir}/pass`, `{relative_test_dir}/fail`" ) ;
113
131
} else {
114
132
let test_path = format ! ( "tests/ui/{}.rs" , lint. name) ;
115
- let test_contents = get_test_file_contents ( lint. name ) ;
133
+ let test_contents = get_test_file_contents ( lint. name , msrv ) ;
116
134
write_file ( lint. project_root . join ( & test_path) , test_contents) ?;
117
135
118
136
println ! ( "Generated test file: `{test_path}`" ) ;
@@ -194,16 +212,38 @@ pub(crate) fn get_stabilization_version() -> String {
194
212
parse_manifest ( & contents) . expect ( "Unable to find package version in `Cargo.toml`" )
195
213
}
196
214
197
- fn get_test_file_contents ( lint_name : & str ) -> String {
198
- formatdoc ! (
215
+ fn get_test_file_contents ( lint_name : & str , msrv : bool ) -> String {
216
+ let mut test = formatdoc ! (
199
217
r#"
200
218
#![warn(clippy::{lint_name})]
201
219
202
220
fn main() {{
203
221
// test code goes here
204
222
}}
205
223
"#
206
- )
224
+ ) ;
225
+
226
+ if msrv {
227
+ let _ = writedoc ! (
228
+ test,
229
+ r#"
230
+
231
+ // TODO: set xx to the version one below the MSRV used by the lint, and yy to
232
+ // the version used by the lint
233
+ #[clippy::msrv = "1.xx"]
234
+ fn msrv_1_xx() {{
235
+ // a simple example that would trigger the lint if the MSRV were met
236
+ }}
237
+
238
+ #[clippy::msrv = "1.yy"]
239
+ fn msrv_1_yy() {{
240
+ // the same example as above
241
+ }}
242
+ "#
243
+ ) ;
244
+ }
245
+
246
+ test
207
247
}
208
248
209
249
fn get_manifest_contents ( lint_name : & str , hint : & str ) -> String {
@@ -258,7 +298,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
258
298
)
259
299
} ) ;
260
300
261
- let _: fmt:: Result = write ! ( result, "{}" , get_lint_declaration( & name_upper, category) ) ;
301
+ let _: fmt:: Result = writeln ! ( result, "{}" , get_lint_declaration( & name_upper, category) ) ;
262
302
263
303
result. push_str ( & if enable_msrv {
264
304
formatdoc ! (
@@ -281,7 +321,6 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
281
321
}}
282
322
283
323
// TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
284
- // TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
285
324
// TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
286
325
"#
287
326
)
0 commit comments