1
- use crate :: utils:: { clippy_project_root , clippy_version } ;
1
+ use crate :: utils:: Version ;
2
2
use clap:: ValueEnum ;
3
3
use indoc:: { formatdoc, writedoc} ;
4
4
use std:: fmt:: { self , Write as _} ;
@@ -22,11 +22,11 @@ impl fmt::Display for Pass {
22
22
}
23
23
24
24
struct LintData < ' a > {
25
+ clippy_version : Version ,
25
26
pass : Pass ,
26
27
name : & ' a str ,
27
28
category : & ' a str ,
28
29
ty : Option < & ' a str > ,
29
- project_root : PathBuf ,
30
30
}
31
31
32
32
trait Context {
@@ -50,18 +50,25 @@ impl<T> Context for io::Result<T> {
50
50
/// # Errors
51
51
///
52
52
/// This function errors out if the files couldn't be created or written to.
53
- pub fn create ( pass : Pass , name : & str , category : & str , mut ty : Option < & str > , msrv : bool ) -> io:: Result < ( ) > {
53
+ pub fn create (
54
+ clippy_version : Version ,
55
+ pass : Pass ,
56
+ name : & str ,
57
+ category : & str ,
58
+ mut ty : Option < & str > ,
59
+ msrv : bool ,
60
+ ) -> io:: Result < ( ) > {
54
61
if category == "cargo" && ty. is_none ( ) {
55
62
// `cargo` is a special category, these lints should always be in `clippy_lints/src/cargo`
56
63
ty = Some ( "cargo" ) ;
57
64
}
58
65
59
66
let lint = LintData {
67
+ clippy_version,
60
68
pass,
61
69
name,
62
70
category,
63
71
ty,
64
- project_root : clippy_project_root ( ) ,
65
72
} ;
66
73
67
74
create_lint ( & lint, msrv) . context ( "Unable to create lint implementation" ) ?;
@@ -88,7 +95,7 @@ fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
88
95
} else {
89
96
let lint_contents = get_lint_file_contents ( lint, enable_msrv) ;
90
97
let lint_path = format ! ( "clippy_lints/src/{}.rs" , lint. name) ;
91
- write_file ( lint . project_root . join ( & lint_path) , lint_contents. as_bytes ( ) ) ?;
98
+ write_file ( & lint_path, lint_contents. as_bytes ( ) ) ?;
92
99
println ! ( "Generated lint file: `{lint_path}`" ) ;
93
100
94
101
Ok ( ( ) )
@@ -115,8 +122,7 @@ fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
115
122
}
116
123
117
124
if lint. category == "cargo" {
118
- let relative_test_dir = format ! ( "tests/ui-cargo/{}" , lint. name) ;
119
- let test_dir = lint. project_root . join ( & relative_test_dir) ;
125
+ let test_dir = format ! ( "tests/ui-cargo/{}" , lint. name) ;
120
126
fs:: create_dir ( & test_dir) ?;
121
127
122
128
create_project_layout (
@@ -134,11 +140,11 @@ fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
134
140
false ,
135
141
) ?;
136
142
137
- println ! ( "Generated test directories: `{relative_test_dir }/pass`, `{relative_test_dir }/fail`" ) ;
143
+ println ! ( "Generated test directories: `{test_dir }/pass`, `{test_dir }/fail`" ) ;
138
144
} else {
139
145
let test_path = format ! ( "tests/ui/{}.rs" , lint. name) ;
140
146
let test_contents = get_test_file_contents ( lint. name , msrv) ;
141
- write_file ( lint . project_root . join ( & test_path) , test_contents) ?;
147
+ write_file ( & test_path, test_contents) ?;
142
148
143
149
println ! ( "Generated test file: `{test_path}`" ) ;
144
150
}
@@ -193,11 +199,6 @@ fn to_camel_case(name: &str) -> String {
193
199
. collect ( )
194
200
}
195
201
196
- pub ( crate ) fn get_stabilization_version ( ) -> String {
197
- let ( minor, patch) = clippy_version ( ) ;
198
- format ! ( "{minor}.{patch}.0" )
199
- }
200
-
201
202
fn get_test_file_contents ( lint_name : & str , msrv : bool ) -> String {
202
203
let mut test = formatdoc ! (
203
204
r"
@@ -292,7 +293,11 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
292
293
) ;
293
294
}
294
295
295
- let _: fmt:: Result = writeln ! ( result, "{}" , get_lint_declaration( & name_upper, category) ) ;
296
+ let _: fmt:: Result = writeln ! (
297
+ result,
298
+ "{}" ,
299
+ get_lint_declaration( lint. clippy_version, & name_upper, category)
300
+ ) ;
296
301
297
302
if enable_msrv {
298
303
let _: fmt:: Result = writedoc ! (
@@ -330,7 +335,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
330
335
result
331
336
}
332
337
333
- fn get_lint_declaration ( name_upper : & str , category : & str ) -> String {
338
+ fn get_lint_declaration ( version : Version , name_upper : & str , category : & str ) -> String {
334
339
let justification_heading = if category == "restriction" {
335
340
"Why restrict this?"
336
341
} else {
@@ -357,7 +362,7 @@ fn get_lint_declaration(name_upper: &str, category: &str) -> String {
357
362
"default lint description"
358
363
}}
359
364
"# ,
360
- get_stabilization_version ( ) ,
365
+ version . rust_display ( ) ,
361
366
)
362
367
}
363
368
@@ -371,7 +376,7 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R
371
376
_ => { } ,
372
377
}
373
378
374
- let ty_dir = lint . project_root . join ( format ! ( "clippy_lints/src/{ty}" ) ) ;
379
+ let ty_dir = PathBuf :: from ( format ! ( "clippy_lints/src/{ty}" ) ) ;
375
380
assert ! (
376
381
ty_dir. exists( ) && ty_dir. is_dir( ) ,
377
382
"Directory `{}` does not exist!" ,
@@ -529,7 +534,10 @@ fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str>
529
534
file_contents. replace_range (
530
535
// Remove the trailing newline, which should always be present
531
536
last_decl_curly_offset..=last_decl_curly_offset,
532
- & format ! ( "\n \n {}" , get_lint_declaration( & lint_name_upper, lint. category) ) ,
537
+ & format ! (
538
+ "\n \n {}" ,
539
+ get_lint_declaration( lint. clippy_version, & lint_name_upper, lint. category)
540
+ ) ,
533
541
) ;
534
542
535
543
// Add the lint to `impl_lint_pass`/`declare_lint_pass`
0 commit comments