]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/new_lint.rs
Auto merge of #9574 - Alexendoo:unused-fixed, r=Jarcho
[rust.git] / clippy_dev / src / new_lint.rs
1 use crate::clippy_project_root;
2 use indoc::{formatdoc, writedoc};
3 use std::fmt::Write as _;
4 use std::fs::{self, OpenOptions};
5 use std::io::prelude::*;
6 use std::io::{self, ErrorKind};
7 use std::path::{Path, PathBuf};
8
9 struct LintData<'a> {
10     pass: &'a str,
11     name: &'a str,
12     category: &'a str,
13     ty: Option<&'a str>,
14     project_root: PathBuf,
15 }
16
17 trait Context {
18     fn context<C: AsRef<str>>(self, text: C) -> Self;
19 }
20
21 impl<T> Context for io::Result<T> {
22     fn context<C: AsRef<str>>(self, text: C) -> Self {
23         match self {
24             Ok(t) => Ok(t),
25             Err(e) => {
26                 let message = format!("{}: {e}", text.as_ref());
27                 Err(io::Error::new(ErrorKind::Other, message))
28             },
29         }
30     }
31 }
32
33 /// Creates the files required to implement and test a new lint and runs `update_lints`.
34 ///
35 /// # Errors
36 ///
37 /// This function errors out if the files couldn't be created or written to.
38 pub fn create(
39     pass: Option<&String>,
40     lint_name: Option<&String>,
41     category: Option<&str>,
42     mut ty: Option<&str>,
43     msrv: bool,
44 ) -> io::Result<()> {
45     if category == Some("cargo") && ty.is_none() {
46         // `cargo` is a special category, these lints should always be in `clippy_lints/src/cargo`
47         ty = Some("cargo");
48     }
49
50     let lint = LintData {
51         pass: pass.map_or("", String::as_str),
52         name: lint_name.expect("`name` argument is validated by clap"),
53         category: category.expect("`category` argument is validated by clap"),
54         ty,
55         project_root: clippy_project_root(),
56     };
57
58     create_lint(&lint, msrv).context("Unable to create lint implementation")?;
59     create_test(&lint).context("Unable to create a test for the new lint")?;
60
61     if lint.ty.is_none() {
62         add_lint(&lint, msrv).context("Unable to add lint to clippy_lints/src/lib.rs")?;
63     }
64
65     Ok(())
66 }
67
68 fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
69     if let Some(ty) = lint.ty {
70         create_lint_for_ty(lint, enable_msrv, ty)
71     } else {
72         let lint_contents = get_lint_file_contents(lint, enable_msrv);
73         let lint_path = format!("clippy_lints/src/{}.rs", lint.name);
74         write_file(lint.project_root.join(&lint_path), lint_contents.as_bytes())?;
75         println!("Generated lint file: `{lint_path}`");
76
77         Ok(())
78     }
79 }
80
81 fn create_test(lint: &LintData<'_>) -> io::Result<()> {
82     fn create_project_layout<P: Into<PathBuf>>(lint_name: &str, location: P, case: &str, hint: &str) -> io::Result<()> {
83         let mut path = location.into().join(case);
84         fs::create_dir(&path)?;
85         write_file(path.join("Cargo.toml"), get_manifest_contents(lint_name, hint))?;
86
87         path.push("src");
88         fs::create_dir(&path)?;
89         let header = format!("// compile-flags: --crate-name={lint_name}");
90         write_file(path.join("main.rs"), get_test_file_contents(lint_name, Some(&header)))?;
91
92         Ok(())
93     }
94
95     if lint.category == "cargo" {
96         let relative_test_dir = format!("tests/ui-cargo/{}", lint.name);
97         let test_dir = lint.project_root.join(&relative_test_dir);
98         fs::create_dir(&test_dir)?;
99
100         create_project_layout(lint.name, &test_dir, "fail", "Content that triggers the lint goes here")?;
101         create_project_layout(lint.name, &test_dir, "pass", "This file should not trigger the lint")?;
102
103         println!("Generated test directories: `{relative_test_dir}/pass`, `{relative_test_dir}/fail`");
104     } else {
105         let test_path = format!("tests/ui/{}.rs", lint.name);
106         let test_contents = get_test_file_contents(lint.name, None);
107         write_file(lint.project_root.join(&test_path), test_contents)?;
108
109         println!("Generated test file: `{test_path}`");
110     }
111
112     Ok(())
113 }
114
115 fn add_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
116     let path = "clippy_lints/src/lib.rs";
117     let mut lib_rs = fs::read_to_string(path).context("reading")?;
118
119     let comment_start = lib_rs.find("// add lints here,").expect("Couldn't find comment");
120
121     let new_lint = if enable_msrv {
122         format!(
123             "store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv)));\n    ",
124             lint_pass = lint.pass,
125             ctor_arg = if lint.pass == "late" { "_" } else { "" },
126             module_name = lint.name,
127             camel_name = to_camel_case(lint.name),
128         )
129     } else {
130         format!(
131             "store.register_{lint_pass}_pass(|{ctor_arg}| Box::new({module_name}::{camel_name}));\n    ",
132             lint_pass = lint.pass,
133             ctor_arg = if lint.pass == "late" { "_" } else { "" },
134             module_name = lint.name,
135             camel_name = to_camel_case(lint.name),
136         )
137     };
138
139     lib_rs.insert_str(comment_start, &new_lint);
140
141     fs::write(path, lib_rs).context("writing")
142 }
143
144 fn write_file<P: AsRef<Path>, C: AsRef<[u8]>>(path: P, contents: C) -> io::Result<()> {
145     fn inner(path: &Path, contents: &[u8]) -> io::Result<()> {
146         OpenOptions::new()
147             .write(true)
148             .create_new(true)
149             .open(path)?
150             .write_all(contents)
151     }
152
153     inner(path.as_ref(), contents.as_ref()).context(format!("writing to file: {}", path.as_ref().display()))
154 }
155
156 fn to_camel_case(name: &str) -> String {
157     name.split('_')
158         .map(|s| {
159             if s.is_empty() {
160                 String::new()
161             } else {
162                 [&s[0..1].to_uppercase(), &s[1..]].concat()
163             }
164         })
165         .collect()
166 }
167
168 pub(crate) fn get_stabilization_version() -> String {
169     fn parse_manifest(contents: &str) -> Option<String> {
170         let version = contents
171             .lines()
172             .filter_map(|l| l.split_once('='))
173             .find_map(|(k, v)| (k.trim() == "version").then(|| v.trim()))?;
174         let Some(("0", version)) = version.get(1..version.len() - 1)?.split_once('.') else {
175             return None;
176         };
177         let (minor, patch) = version.split_once('.')?;
178         Some(format!(
179             "{}.{}.0",
180             minor.parse::<u32>().ok()?,
181             patch.parse::<u32>().ok()?
182         ))
183     }
184     let contents = fs::read_to_string("Cargo.toml").expect("Unable to read `Cargo.toml`");
185     parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
186 }
187
188 fn get_test_file_contents(lint_name: &str, header_commands: Option<&str>) -> String {
189     let mut contents = formatdoc!(
190         r#"
191         #![allow(unused)]
192         #![warn(clippy::{lint_name})]
193
194         fn main() {{
195             // test code goes here
196         }}
197     "#
198     );
199
200     if let Some(header) = header_commands {
201         contents = format!("{header}\n{contents}");
202     }
203
204     contents
205 }
206
207 fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
208     formatdoc!(
209         r#"
210         # {hint}
211
212         [package]
213         name = "{lint_name}"
214         version = "0.1.0"
215         publish = false
216
217         [workspace]
218     "#
219     )
220 }
221
222 fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
223     let mut result = String::new();
224
225     let (pass_type, pass_lifetimes, pass_import, context_import) = match lint.pass {
226         "early" => ("EarlyLintPass", "", "use rustc_ast::ast::*;", "EarlyContext"),
227         "late" => ("LateLintPass", "<'_>", "use rustc_hir::*;", "LateContext"),
228         _ => {
229             unreachable!("`pass_type` should only ever be `early` or `late`!");
230         },
231     };
232
233     let lint_name = lint.name;
234     let category = lint.category;
235     let name_camel = to_camel_case(lint.name);
236     let name_upper = lint_name.to_uppercase();
237
238     result.push_str(&if enable_msrv {
239         formatdoc!(
240             r#"
241             use clippy_utils::msrvs;
242             {pass_import}
243             use rustc_lint::{{{context_import}, {pass_type}, LintContext}};
244             use rustc_semver::RustcVersion;
245             use rustc_session::{{declare_tool_lint, impl_lint_pass}};
246
247         "#
248         )
249     } else {
250         formatdoc!(
251             r#"
252             {pass_import}
253             use rustc_lint::{{{context_import}, {pass_type}}};
254             use rustc_session::{{declare_lint_pass, declare_tool_lint}};
255
256         "#
257         )
258     });
259
260     let _ = write!(result, "{}", get_lint_declaration(&name_upper, category));
261
262     result.push_str(&if enable_msrv {
263         formatdoc!(
264             r#"
265             pub struct {name_camel} {{
266                 msrv: Option<RustcVersion>,
267             }}
268
269             impl {name_camel} {{
270                 #[must_use]
271                 pub fn new(msrv: Option<RustcVersion>) -> Self {{
272                     Self {{ msrv }}
273                 }}
274             }}
275
276             impl_lint_pass!({name_camel} => [{name_upper}]);
277
278             impl {pass_type}{pass_lifetimes} for {name_camel} {{
279                 extract_msrv_attr!({context_import});
280             }}
281
282             // TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
283             // TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
284             // TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
285         "#
286         )
287     } else {
288         formatdoc!(
289             r#"
290             declare_lint_pass!({name_camel} => [{name_upper}]);
291
292             impl {pass_type}{pass_lifetimes} for {name_camel} {{}}
293         "#
294         )
295     });
296
297     result
298 }
299
300 fn get_lint_declaration(name_upper: &str, category: &str) -> String {
301     formatdoc!(
302         r#"
303             declare_clippy_lint! {{
304                 /// ### What it does
305                 ///
306                 /// ### Why is this bad?
307                 ///
308                 /// ### Example
309                 /// ```rust
310                 /// // example code where clippy issues a warning
311                 /// ```
312                 /// Use instead:
313                 /// ```rust
314                 /// // example code which does not raise clippy warning
315                 /// ```
316                 #[clippy::version = "{}"]
317                 pub {name_upper},
318                 {category},
319                 "default lint description"
320             }}
321         "#,
322         get_stabilization_version(),
323     )
324 }
325
326 fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::Result<()> {
327     match ty {
328         "cargo" => assert_eq!(
329             lint.category, "cargo",
330             "Lints of type `cargo` must have the `cargo` category"
331         ),
332         _ if lint.category == "cargo" => panic!("Lints of category `cargo` must have the `cargo` type"),
333         _ => {},
334     }
335
336     let ty_dir = lint.project_root.join(format!("clippy_lints/src/{ty}"));
337     assert!(
338         ty_dir.exists() && ty_dir.is_dir(),
339         "Directory `{}` does not exist!",
340         ty_dir.display()
341     );
342
343     let lint_file_path = ty_dir.join(format!("{}.rs", lint.name));
344     assert!(
345         !lint_file_path.exists(),
346         "File `{}` already exists",
347         lint_file_path.display()
348     );
349
350     let mod_file_path = ty_dir.join("mod.rs");
351     let context_import = setup_mod_file(&mod_file_path, lint)?;
352
353     let name_upper = lint.name.to_uppercase();
354     let mut lint_file_contents = String::new();
355
356     if enable_msrv {
357         let _ = writedoc!(
358             lint_file_contents,
359             r#"
360                 use clippy_utils::{{meets_msrv, msrvs}};
361                 use rustc_lint::{{{context_import}, LintContext}};
362                 use rustc_semver::RustcVersion;
363
364                 use super::{name_upper};
365
366                 // TODO: Adjust the parameters as necessary
367                 pub(super) fn check(cx: &{context_import}, msrv: Option<RustcVersion>) {{
368                     if !meets_msrv(msrv, todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{
369                         return;
370                     }}
371                     todo!();
372                 }}
373            "#,
374             context_import = context_import,
375             name_upper = name_upper,
376         );
377     } else {
378         let _ = writedoc!(
379             lint_file_contents,
380             r#"
381                 use rustc_lint::{{{context_import}, LintContext}};
382
383                 use super::{name_upper};
384
385                 // TODO: Adjust the parameters as necessary
386                 pub(super) fn check(cx: &{context_import}) {{
387                     todo!();
388                 }}
389            "#,
390             context_import = context_import,
391             name_upper = name_upper,
392         );
393     }
394
395     write_file(lint_file_path.as_path(), lint_file_contents)?;
396     println!("Generated lint file: `clippy_lints/src/{ty}/{}.rs`", lint.name);
397     println!(
398         "Be sure to add a call to `{}::check` in `clippy_lints/src/{ty}/mod.rs`!",
399         lint.name
400     );
401
402     Ok(())
403 }
404
405 #[allow(clippy::too_many_lines)]
406 fn setup_mod_file(path: &Path, lint: &LintData<'_>) -> io::Result<&'static str> {
407     use super::update_lints::{match_tokens, LintDeclSearchResult};
408     use rustc_lexer::TokenKind;
409
410     let lint_name_upper = lint.name.to_uppercase();
411
412     let mut file_contents = fs::read_to_string(path)?;
413     assert!(
414         !file_contents.contains(&lint_name_upper),
415         "Lint `{}` already defined in `{}`",
416         lint.name,
417         path.display()
418     );
419
420     let mut offset = 0usize;
421     let mut last_decl_curly_offset = None;
422     let mut lint_context = None;
423
424     let mut iter = rustc_lexer::tokenize(&file_contents).map(|t| {
425         let range = offset..offset + t.len as usize;
426         offset = range.end;
427
428         LintDeclSearchResult {
429             token_kind: t.kind,
430             content: &file_contents[range.clone()],
431             range,
432         }
433     });
434
435     // Find both the last lint declaration (declare_clippy_lint!) and the lint pass impl
436     while let Some(LintDeclSearchResult { content, .. }) = iter.find(|result| result.token_kind == TokenKind::Ident) {
437         let mut iter = iter
438             .by_ref()
439             .filter(|t| !matches!(t.token_kind, TokenKind::Whitespace | TokenKind::LineComment { .. }));
440
441         match content {
442             "declare_clippy_lint" => {
443                 // matches `!{`
444                 match_tokens!(iter, Bang OpenBrace);
445                 if let Some(LintDeclSearchResult { range, .. }) =
446                     iter.find(|result| result.token_kind == TokenKind::CloseBrace)
447                 {
448                     last_decl_curly_offset = Some(range.end);
449                 }
450             },
451             "impl" => {
452                 let mut token = iter.next();
453                 match token {
454                     // matches <'foo>
455                     Some(LintDeclSearchResult {
456                         token_kind: TokenKind::Lt,
457                         ..
458                     }) => {
459                         match_tokens!(iter, Lifetime { .. } Gt);
460                         token = iter.next();
461                     },
462                     None => break,
463                     _ => {},
464                 }
465
466                 if let Some(LintDeclSearchResult {
467                     token_kind: TokenKind::Ident,
468                     content,
469                     ..
470                 }) = token
471                 {
472                     // Get the appropriate lint context struct
473                     lint_context = match content {
474                         "LateLintPass" => Some("LateContext"),
475                         "EarlyLintPass" => Some("EarlyContext"),
476                         _ => continue,
477                     };
478                 }
479             },
480             _ => {},
481         }
482     }
483
484     drop(iter);
485
486     let last_decl_curly_offset =
487         last_decl_curly_offset.unwrap_or_else(|| panic!("No lint declarations found in `{}`", path.display()));
488     let lint_context =
489         lint_context.unwrap_or_else(|| panic!("No lint pass implementation found in `{}`", path.display()));
490
491     // Add the lint declaration to `mod.rs`
492     file_contents.replace_range(
493         // Remove the trailing newline, which should always be present
494         last_decl_curly_offset..=last_decl_curly_offset,
495         &format!("\n\n{}", get_lint_declaration(&lint_name_upper, lint.category)),
496     );
497
498     // Add the lint to `impl_lint_pass`/`declare_lint_pass`
499     let impl_lint_pass_start = file_contents.find("impl_lint_pass!").unwrap_or_else(|| {
500         file_contents
501             .find("declare_lint_pass!")
502             .unwrap_or_else(|| panic!("failed to find `impl_lint_pass`/`declare_lint_pass`"))
503     });
504
505     let mut arr_start = file_contents[impl_lint_pass_start..].find('[').unwrap_or_else(|| {
506         panic!("malformed `impl_lint_pass`/`declare_lint_pass`");
507     });
508
509     arr_start += impl_lint_pass_start;
510
511     let mut arr_end = file_contents[arr_start..]
512         .find(']')
513         .expect("failed to find `impl_lint_pass` terminator");
514
515     arr_end += arr_start;
516
517     let mut arr_content = file_contents[arr_start + 1..arr_end].to_string();
518     arr_content.retain(|c| !c.is_whitespace());
519
520     let mut new_arr_content = String::new();
521     for ident in arr_content
522         .split(',')
523         .chain(std::iter::once(&*lint_name_upper))
524         .filter(|s| !s.is_empty())
525     {
526         let _ = write!(new_arr_content, "\n    {ident},");
527     }
528     new_arr_content.push('\n');
529
530     file_contents.replace_range(arr_start + 1..arr_end, &new_arr_content);
531
532     // Just add the mod declaration at the top, it'll be fixed by rustfmt
533     file_contents.insert_str(0, &format!("mod {};\n", &lint.name));
534
535     let mut file = OpenOptions::new()
536         .write(true)
537         .truncate(true)
538         .open(path)
539         .context(format!("trying to open: `{}`", path.display()))?;
540     file.write_all(file_contents.as_bytes())
541         .context(format!("writing to file: `{}`", path.display()))?;
542
543     Ok(lint_context)
544 }
545
546 #[test]
547 fn test_camel_case() {
548     let s = "a_lint";
549     let s2 = to_camel_case(s);
550     assert_eq!(s2, "ALint");
551
552     let name = "a_really_long_new_lint";
553     let name2 = to_camel_case(name);
554     assert_eq!(name2, "AReallyLongNewLint");
555
556     let name3 = "lint__name";
557     let name4 = to_camel_case(name3);
558     assert_eq!(name4, "LintName");
559 }