]> git.lizzy.rs Git - rust.git/blob - clippy_dev/src/update_lints.rs
Merge commit 'd9ddce8a223cb9916389c039777b6966ea448dc8' into clippyup
[rust.git] / clippy_dev / src / update_lints.rs
1 use aho_corasick::AhoCorasickBuilder;
2 use core::fmt::Write as _;
3 use itertools::Itertools;
4 use rustc_lexer::{tokenize, unescape, LiteralKind, TokenKind};
5 use std::collections::{HashMap, HashSet};
6 use std::ffi::OsStr;
7 use std::fs;
8 use std::io::{self, Read as _, Seek as _, Write as _};
9 use std::path::{Path, PathBuf};
10 use walkdir::{DirEntry, WalkDir};
11
12 use crate::clippy_project_root;
13
14 const GENERATED_FILE_COMMENT: &str = "// This file was generated by `cargo dev update_lints`.\n\
15      // Use that command to update this file and do not edit by hand.\n\
16      // Manual edits will be overwritten.\n\n";
17
18 const DOCS_LINK: &str = "https://rust-lang.github.io/rust-clippy/master/index.html";
19
20 #[derive(Clone, Copy, PartialEq, Eq)]
21 pub enum UpdateMode {
22     Check,
23     Change,
24 }
25
26 /// Runs the `update_lints` command.
27 ///
28 /// This updates various generated values from the lint source code.
29 ///
30 /// `update_mode` indicates if the files should be updated or if updates should be checked for.
31 ///
32 /// # Panics
33 ///
34 /// Panics if a file path could not read from or then written to
35 pub fn update(update_mode: UpdateMode) {
36     let (lints, deprecated_lints, renamed_lints) = gather_all();
37     generate_lint_files(update_mode, &lints, &deprecated_lints, &renamed_lints);
38 }
39
40 fn generate_lint_files(
41     update_mode: UpdateMode,
42     lints: &[Lint],
43     deprecated_lints: &[DeprecatedLint],
44     renamed_lints: &[RenamedLint],
45 ) {
46     let internal_lints = Lint::internal_lints(lints);
47     let usable_lints = Lint::usable_lints(lints);
48     let mut sorted_usable_lints = usable_lints.clone();
49     sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
50
51     replace_region_in_file(
52         update_mode,
53         Path::new("README.md"),
54         "[There are over ",
55         " lints included in this crate!]",
56         |res| {
57             write!(res, "{}", round_to_fifty(usable_lints.len())).unwrap();
58         },
59     );
60
61     replace_region_in_file(
62         update_mode,
63         Path::new("CHANGELOG.md"),
64         "<!-- begin autogenerated links to lint list -->\n",
65         "<!-- end autogenerated links to lint list -->",
66         |res| {
67             for lint in usable_lints
68                 .iter()
69                 .map(|l| &*l.name)
70                 .chain(deprecated_lints.iter().map(|l| &*l.name))
71                 .chain(
72                     renamed_lints
73                         .iter()
74                         .map(|l| l.old_name.strip_prefix("clippy::").unwrap_or(&l.old_name)),
75                 )
76                 .sorted()
77             {
78                 writeln!(res, "[`{}`]: {}#{}", lint, DOCS_LINK, lint).unwrap();
79             }
80         },
81     );
82
83     // This has to be in lib.rs, otherwise rustfmt doesn't work
84     replace_region_in_file(
85         update_mode,
86         Path::new("clippy_lints/src/lib.rs"),
87         "// begin lints modules, do not remove this comment, it’s used in `update_lints`\n",
88         "// end lints modules, do not remove this comment, it’s used in `update_lints`",
89         |res| {
90             for lint_mod in usable_lints.iter().map(|l| &l.module).unique().sorted() {
91                 writeln!(res, "mod {};", lint_mod).unwrap();
92             }
93         },
94     );
95
96     process_file(
97         "clippy_lints/src/lib.register_lints.rs",
98         update_mode,
99         &gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
100     );
101     process_file(
102         "clippy_lints/src/lib.deprecated.rs",
103         update_mode,
104         &gen_deprecated(deprecated_lints),
105     );
106
107     let all_group_lints = usable_lints.iter().filter(|l| {
108         matches!(
109             &*l.group,
110             "correctness" | "suspicious" | "style" | "complexity" | "perf"
111         )
112     });
113     let content = gen_lint_group_list("all", all_group_lints);
114     process_file("clippy_lints/src/lib.register_all.rs", update_mode, &content);
115
116     for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
117         let content = gen_lint_group_list(&lint_group, lints.iter());
118         process_file(
119             &format!("clippy_lints/src/lib.register_{}.rs", lint_group),
120             update_mode,
121             &content,
122         );
123     }
124
125     let content = gen_deprecated_lints_test(deprecated_lints);
126     process_file("tests/ui/deprecated.rs", update_mode, &content);
127
128     let content = gen_renamed_lints_test(renamed_lints);
129     process_file("tests/ui/rename.rs", update_mode, &content);
130 }
131
132 pub fn print_lints() {
133     let (lint_list, _, _) = gather_all();
134     let usable_lints = Lint::usable_lints(&lint_list);
135     let usable_lint_count = usable_lints.len();
136     let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
137
138     for (lint_group, mut lints) in grouped_by_lint_group {
139         println!("\n## {}", lint_group);
140
141         lints.sort_by_key(|l| l.name.clone());
142
143         for lint in lints {
144             println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc);
145         }
146     }
147
148     println!("there are {} lints", usable_lint_count);
149 }
150
151 /// Runs the `rename_lint` command.
152 ///
153 /// This does the following:
154 /// * Adds an entry to `renamed_lints.rs`.
155 /// * Renames all lint attributes to the new name (e.g. `#[allow(clippy::lint_name)]`).
156 /// * Renames the lint struct to the new name.
157 /// * Renames the module containing the lint struct to the new name if it shares a name with the
158 ///   lint.
159 ///
160 /// # Panics
161 /// Panics for the following conditions:
162 /// * If a file path could not read from or then written to
163 /// * If either lint name has a prefix
164 /// * If `old_name` doesn't name an existing lint.
165 /// * If `old_name` names a deprecated or renamed lint.
166 #[allow(clippy::too_many_lines)]
167 pub fn rename(old_name: &str, new_name: &str, uplift: bool) {
168     if let Some((prefix, _)) = old_name.split_once("::") {
169         panic!("`{}` should not contain the `{}` prefix", old_name, prefix);
170     }
171     if let Some((prefix, _)) = new_name.split_once("::") {
172         panic!("`{}` should not contain the `{}` prefix", new_name, prefix);
173     }
174
175     let (mut lints, deprecated_lints, mut renamed_lints) = gather_all();
176     let mut old_lint_index = None;
177     let mut found_new_name = false;
178     for (i, lint) in lints.iter().enumerate() {
179         if lint.name == old_name {
180             old_lint_index = Some(i);
181         } else if lint.name == new_name {
182             found_new_name = true;
183         }
184     }
185     let old_lint_index = old_lint_index.unwrap_or_else(|| panic!("could not find lint `{}`", old_name));
186
187     let lint = RenamedLint {
188         old_name: format!("clippy::{}", old_name),
189         new_name: if uplift {
190             new_name.into()
191         } else {
192             format!("clippy::{}", new_name)
193         },
194     };
195
196     // Renamed lints and deprecated lints shouldn't have been found in the lint list, but check just in
197     // case.
198     assert!(
199         !renamed_lints.iter().any(|l| lint.old_name == l.old_name),
200         "`{}` has already been renamed",
201         old_name
202     );
203     assert!(
204         !deprecated_lints.iter().any(|l| lint.old_name == l.name),
205         "`{}` has already been deprecated",
206         old_name
207     );
208
209     // Update all lint level attributes. (`clippy::lint_name`)
210     for file in WalkDir::new(clippy_project_root())
211         .into_iter()
212         .map(Result::unwrap)
213         .filter(|f| {
214             let name = f.path().file_name();
215             let ext = f.path().extension();
216             (ext == Some(OsStr::new("rs")) || ext == Some(OsStr::new("fixed")))
217                 && name != Some(OsStr::new("rename.rs"))
218                 && name != Some(OsStr::new("renamed_lints.rs"))
219         })
220     {
221         rewrite_file(file.path(), |s| {
222             replace_ident_like(s, &[(&lint.old_name, &lint.new_name)])
223         });
224     }
225
226     renamed_lints.push(lint);
227     renamed_lints.sort_by(|lhs, rhs| {
228         lhs.new_name
229             .starts_with("clippy::")
230             .cmp(&rhs.new_name.starts_with("clippy::"))
231             .reverse()
232             .then_with(|| lhs.old_name.cmp(&rhs.old_name))
233     });
234
235     write_file(
236         Path::new("clippy_lints/src/renamed_lints.rs"),
237         &gen_renamed_lints_list(&renamed_lints),
238     );
239
240     if uplift {
241         write_file(Path::new("tests/ui/rename.rs"), &gen_renamed_lints_test(&renamed_lints));
242         println!(
243             "`{}` has be uplifted. All the code inside `clippy_lints` related to it needs to be removed manually.",
244             old_name
245         );
246     } else if found_new_name {
247         write_file(Path::new("tests/ui/rename.rs"), &gen_renamed_lints_test(&renamed_lints));
248         println!(
249             "`{}` is already defined. The old linting code inside `clippy_lints` needs to be updated/removed manually.",
250             new_name
251         );
252     } else {
253         // Rename the lint struct and source files sharing a name with the lint.
254         let lint = &mut lints[old_lint_index];
255         let old_name_upper = old_name.to_uppercase();
256         let new_name_upper = new_name.to_uppercase();
257         lint.name = new_name.into();
258
259         // Rename test files. only rename `.stderr` and `.fixed` files if the new test name doesn't exist.
260         if try_rename_file(
261             Path::new(&format!("tests/ui/{}.rs", old_name)),
262             Path::new(&format!("tests/ui/{}.rs", new_name)),
263         ) {
264             try_rename_file(
265                 Path::new(&format!("tests/ui/{}.stderr", old_name)),
266                 Path::new(&format!("tests/ui/{}.stderr", new_name)),
267             );
268             try_rename_file(
269                 Path::new(&format!("tests/ui/{}.fixed", old_name)),
270                 Path::new(&format!("tests/ui/{}.fixed", new_name)),
271             );
272         }
273
274         // Try to rename the file containing the lint if the file name matches the lint's name.
275         let replacements;
276         let replacements = if lint.module == old_name
277             && try_rename_file(
278                 Path::new(&format!("clippy_lints/src/{}.rs", old_name)),
279                 Path::new(&format!("clippy_lints/src/{}.rs", new_name)),
280             ) {
281             // Edit the module name in the lint list. Note there could be multiple lints.
282             for lint in lints.iter_mut().filter(|l| l.module == old_name) {
283                 lint.module = new_name.into();
284             }
285             replacements = [(&*old_name_upper, &*new_name_upper), (old_name, new_name)];
286             replacements.as_slice()
287         } else if !lint.module.contains("::")
288             // Catch cases like `methods/lint_name.rs` where the lint is stored in `methods/mod.rs`
289             && try_rename_file(
290                 Path::new(&format!("clippy_lints/src/{}/{}.rs", lint.module, old_name)),
291                 Path::new(&format!("clippy_lints/src/{}/{}.rs", lint.module, new_name)),
292             )
293         {
294             // Edit the module name in the lint list. Note there could be multiple lints, or none.
295             let renamed_mod = format!("{}::{}", lint.module, old_name);
296             for lint in lints.iter_mut().filter(|l| l.module == renamed_mod) {
297                 lint.module = format!("{}::{}", lint.module, new_name);
298             }
299             replacements = [(&*old_name_upper, &*new_name_upper), (old_name, new_name)];
300             replacements.as_slice()
301         } else {
302             replacements = [(&*old_name_upper, &*new_name_upper), ("", "")];
303             &replacements[0..1]
304         };
305
306         // Don't change `clippy_utils/src/renamed_lints.rs` here as it would try to edit the lint being
307         // renamed.
308         for (_, file) in clippy_lints_src_files().filter(|(rel_path, _)| rel_path != OsStr::new("renamed_lints.rs")) {
309             rewrite_file(file.path(), |s| replace_ident_like(s, replacements));
310         }
311
312         generate_lint_files(UpdateMode::Change, &lints, &deprecated_lints, &renamed_lints);
313         println!("{} has been successfully renamed", old_name);
314     }
315
316     println!("note: `cargo uitest` still needs to be run to update the test results");
317 }
318
319 /// Replace substrings if they aren't bordered by identifier characters. Returns `None` if there
320 /// were no replacements.
321 fn replace_ident_like(contents: &str, replacements: &[(&str, &str)]) -> Option<String> {
322     fn is_ident_char(c: u8) -> bool {
323         matches!(c, b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'_')
324     }
325
326     let searcher = AhoCorasickBuilder::new()
327         .dfa(true)
328         .match_kind(aho_corasick::MatchKind::LeftmostLongest)
329         .build_with_size::<u16, _, _>(replacements.iter().map(|&(x, _)| x.as_bytes()))
330         .unwrap();
331
332     let mut result = String::with_capacity(contents.len() + 1024);
333     let mut pos = 0;
334     let mut edited = false;
335     for m in searcher.find_iter(contents) {
336         let (old, new) = replacements[m.pattern()];
337         result.push_str(&contents[pos..m.start()]);
338         result.push_str(
339             if !is_ident_char(contents.as_bytes().get(m.start().wrapping_sub(1)).copied().unwrap_or(0))
340                 && !is_ident_char(contents.as_bytes().get(m.end()).copied().unwrap_or(0))
341             {
342                 edited = true;
343                 new
344             } else {
345                 old
346             },
347         );
348         pos = m.end();
349     }
350     result.push_str(&contents[pos..]);
351     edited.then(|| result)
352 }
353
354 fn round_to_fifty(count: usize) -> usize {
355     count / 50 * 50
356 }
357
358 fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, content: &str) {
359     if update_mode == UpdateMode::Check {
360         let old_content =
361             fs::read_to_string(&path).unwrap_or_else(|e| panic!("Cannot read from {}: {}", path.as_ref().display(), e));
362         if content != old_content {
363             exit_with_failure();
364         }
365     } else {
366         fs::write(&path, content.as_bytes())
367             .unwrap_or_else(|e| panic!("Cannot write to {}: {}", path.as_ref().display(), e));
368     }
369 }
370
371 fn exit_with_failure() {
372     println!(
373         "Not all lints defined properly. \
374                  Please run `cargo dev update_lints` to make sure all lints are defined properly."
375     );
376     std::process::exit(1);
377 }
378
379 /// Lint data parsed from the Clippy source code.
380 #[derive(Clone, PartialEq, Eq, Debug)]
381 struct Lint {
382     name: String,
383     group: String,
384     desc: String,
385     module: String,
386 }
387
388 impl Lint {
389     #[must_use]
390     fn new(name: &str, group: &str, desc: &str, module: &str) -> Self {
391         Self {
392             name: name.to_lowercase(),
393             group: group.into(),
394             desc: remove_line_splices(desc),
395             module: module.into(),
396         }
397     }
398
399     /// Returns all non-deprecated lints and non-internal lints
400     #[must_use]
401     fn usable_lints(lints: &[Self]) -> Vec<Self> {
402         lints
403             .iter()
404             .filter(|l| !l.group.starts_with("internal"))
405             .cloned()
406             .collect()
407     }
408
409     /// Returns all internal lints (not `internal_warn` lints)
410     #[must_use]
411     fn internal_lints(lints: &[Self]) -> Vec<Self> {
412         lints.iter().filter(|l| l.group == "internal").cloned().collect()
413     }
414
415     /// Returns the lints in a `HashMap`, grouped by the different lint groups
416     #[must_use]
417     fn by_lint_group(lints: impl Iterator<Item = Self>) -> HashMap<String, Vec<Self>> {
418         lints.map(|lint| (lint.group.to_string(), lint)).into_group_map()
419     }
420 }
421
422 #[derive(Clone, PartialEq, Eq, Debug)]
423 struct DeprecatedLint {
424     name: String,
425     reason: String,
426 }
427 impl DeprecatedLint {
428     fn new(name: &str, reason: &str) -> Self {
429         Self {
430             name: name.to_lowercase(),
431             reason: remove_line_splices(reason),
432         }
433     }
434 }
435
436 struct RenamedLint {
437     old_name: String,
438     new_name: String,
439 }
440 impl RenamedLint {
441     fn new(old_name: &str, new_name: &str) -> Self {
442         Self {
443             old_name: remove_line_splices(old_name),
444             new_name: remove_line_splices(new_name),
445         }
446     }
447 }
448
449 /// Generates the code for registering a group
450 fn gen_lint_group_list<'a>(group_name: &str, lints: impl Iterator<Item = &'a Lint>) -> String {
451     let mut details: Vec<_> = lints.map(|l| (&l.module, l.name.to_uppercase())).collect();
452     details.sort_unstable();
453
454     let mut output = GENERATED_FILE_COMMENT.to_string();
455
456     let _ = writeln!(
457         output,
458         "store.register_group(true, \"clippy::{0}\", Some(\"clippy_{0}\"), vec![",
459         group_name
460     );
461     for (module, name) in details {
462         let _ = writeln!(output, "    LintId::of({}::{}),", module, name);
463     }
464     output.push_str("])\n");
465
466     output
467 }
468
469 /// Generates the `register_removed` code
470 #[must_use]
471 fn gen_deprecated(lints: &[DeprecatedLint]) -> String {
472     let mut output = GENERATED_FILE_COMMENT.to_string();
473     output.push_str("{\n");
474     for lint in lints {
475         let _ = write!(
476             output,
477             concat!(
478                 "    store.register_removed(\n",
479                 "        \"clippy::{}\",\n",
480                 "        \"{}\",\n",
481                 "    );\n"
482             ),
483             lint.name, lint.reason,
484         );
485     }
486     output.push_str("}\n");
487
488     output
489 }
490
491 /// Generates the code for registering lints
492 #[must_use]
493 fn gen_register_lint_list<'a>(
494     internal_lints: impl Iterator<Item = &'a Lint>,
495     usable_lints: impl Iterator<Item = &'a Lint>,
496 ) -> String {
497     let mut details: Vec<_> = internal_lints
498         .map(|l| (false, &l.module, l.name.to_uppercase()))
499         .chain(usable_lints.map(|l| (true, &l.module, l.name.to_uppercase())))
500         .collect();
501     details.sort_unstable();
502
503     let mut output = GENERATED_FILE_COMMENT.to_string();
504     output.push_str("store.register_lints(&[\n");
505
506     for (is_public, module_name, lint_name) in details {
507         if !is_public {
508             output.push_str("    #[cfg(feature = \"internal\")]\n");
509         }
510         let _ = writeln!(output, "    {}::{},", module_name, lint_name);
511     }
512     output.push_str("])\n");
513
514     output
515 }
516
517 fn gen_deprecated_lints_test(lints: &[DeprecatedLint]) -> String {
518     let mut res: String = GENERATED_FILE_COMMENT.into();
519     for lint in lints {
520         writeln!(res, "#![warn(clippy::{})]", lint.name).unwrap();
521     }
522     res.push_str("\nfn main() {}\n");
523     res
524 }
525
526 fn gen_renamed_lints_test(lints: &[RenamedLint]) -> String {
527     let mut seen_lints = HashSet::new();
528     let mut res: String = GENERATED_FILE_COMMENT.into();
529     res.push_str("// run-rustfix\n\n");
530     for lint in lints {
531         if seen_lints.insert(&lint.new_name) {
532             writeln!(res, "#![allow({})]", lint.new_name).unwrap();
533         }
534     }
535     seen_lints.clear();
536     for lint in lints {
537         if seen_lints.insert(&lint.old_name) {
538             writeln!(res, "#![warn({})]", lint.old_name).unwrap();
539         }
540     }
541     res.push_str("\nfn main() {}\n");
542     res
543 }
544
545 fn gen_renamed_lints_list(lints: &[RenamedLint]) -> String {
546     const HEADER: &str = "\
547         // This file is managed by `cargo dev rename_lint`. Prefer using that when possible.\n\n\
548         #[rustfmt::skip]\n\
549         pub static RENAMED_LINTS: &[(&str, &str)] = &[\n";
550
551     let mut res = String::from(HEADER);
552     for lint in lints {
553         writeln!(res, "    (\"{}\", \"{}\"),", lint.old_name, lint.new_name).unwrap();
554     }
555     res.push_str("];\n");
556     res
557 }
558
559 /// Gathers all lints defined in `clippy_lints/src`
560 fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>, Vec<RenamedLint>) {
561     let mut lints = Vec::with_capacity(1000);
562     let mut deprecated_lints = Vec::with_capacity(50);
563     let mut renamed_lints = Vec::with_capacity(50);
564
565     for (rel_path, file) in clippy_lints_src_files() {
566         let path = file.path();
567         let contents =
568             fs::read_to_string(path).unwrap_or_else(|e| panic!("Cannot read from `{}`: {}", path.display(), e));
569         let module = rel_path
570             .components()
571             .map(|c| c.as_os_str().to_str().unwrap())
572             .collect::<Vec<_>>()
573             .join("::");
574
575         // If the lints are stored in mod.rs, we get the module name from
576         // the containing directory:
577         let module = if let Some(module) = module.strip_suffix("::mod.rs") {
578             module
579         } else {
580             module.strip_suffix(".rs").unwrap_or(&module)
581         };
582
583         match module {
584             "deprecated_lints" => parse_deprecated_contents(&contents, &mut deprecated_lints),
585             "renamed_lints" => parse_renamed_contents(&contents, &mut renamed_lints),
586             _ => parse_contents(&contents, module, &mut lints),
587         }
588     }
589     (lints, deprecated_lints, renamed_lints)
590 }
591
592 fn clippy_lints_src_files() -> impl Iterator<Item = (PathBuf, DirEntry)> {
593     let root_path = clippy_project_root().join("clippy_lints/src");
594     let iter = WalkDir::new(&root_path).into_iter();
595     iter.map(Result::unwrap)
596         .filter(|f| f.path().extension() == Some(OsStr::new("rs")))
597         .map(move |f| (f.path().strip_prefix(&root_path).unwrap().to_path_buf(), f))
598 }
599
600 macro_rules! match_tokens {
601     ($iter:ident, $($token:ident $({$($fields:tt)*})? $(($capture:ident))?)*) => {
602          {
603             $($(let $capture =)? if let Some((TokenKind::$token $({$($fields)*})?, _x)) = $iter.next() {
604                 _x
605             } else {
606                 continue;
607             };)*
608             #[allow(clippy::unused_unit)]
609             { ($($($capture,)?)*) }
610         }
611     }
612 }
613
614 /// Parse a source file looking for `declare_clippy_lint` macro invocations.
615 fn parse_contents(contents: &str, module: &str, lints: &mut Vec<Lint>) {
616     let mut offset = 0usize;
617     let mut iter = tokenize(contents).map(|t| {
618         let range = offset..offset + t.len;
619         offset = range.end;
620         (t.kind, &contents[range])
621     });
622
623     while iter.any(|(kind, s)| kind == TokenKind::Ident && s == "declare_clippy_lint") {
624         let mut iter = iter
625             .by_ref()
626             .filter(|&(kind, _)| !matches!(kind, TokenKind::Whitespace | TokenKind::LineComment { .. }));
627         // matches `!{`
628         match_tokens!(iter, Bang OpenBrace);
629         match iter.next() {
630             // #[clippy::version = "version"] pub
631             Some((TokenKind::Pound, _)) => {
632                 match_tokens!(iter, OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket Ident);
633             },
634             // pub
635             Some((TokenKind::Ident, _)) => (),
636             _ => continue,
637         }
638         let (name, group, desc) = match_tokens!(
639             iter,
640             // LINT_NAME
641             Ident(name) Comma
642             // group,
643             Ident(group) Comma
644             // "description" }
645             Literal{..}(desc) CloseBrace
646         );
647         lints.push(Lint::new(name, group, desc, module));
648     }
649 }
650
651 /// Parse a source file looking for `declare_deprecated_lint` macro invocations.
652 fn parse_deprecated_contents(contents: &str, lints: &mut Vec<DeprecatedLint>) {
653     let mut offset = 0usize;
654     let mut iter = tokenize(contents).map(|t| {
655         let range = offset..offset + t.len;
656         offset = range.end;
657         (t.kind, &contents[range])
658     });
659     while iter.any(|(kind, s)| kind == TokenKind::Ident && s == "declare_deprecated_lint") {
660         let mut iter = iter
661             .by_ref()
662             .filter(|&(kind, _)| !matches!(kind, TokenKind::Whitespace | TokenKind::LineComment { .. }));
663         let (name, reason) = match_tokens!(
664             iter,
665             // !{
666             Bang OpenBrace
667             // #[clippy::version = "version"]
668             Pound OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket
669             // pub LINT_NAME,
670             Ident Ident(name) Comma
671             // "description"
672             Literal{kind: LiteralKind::Str{..},..}(reason)
673             // }
674             CloseBrace
675         );
676         lints.push(DeprecatedLint::new(name, reason));
677     }
678 }
679
680 fn parse_renamed_contents(contents: &str, lints: &mut Vec<RenamedLint>) {
681     for line in contents.lines() {
682         let mut offset = 0usize;
683         let mut iter = tokenize(line).map(|t| {
684             let range = offset..offset + t.len;
685             offset = range.end;
686             (t.kind, &line[range])
687         });
688         let (old_name, new_name) = match_tokens!(
689             iter,
690             // ("old_name",
691             Whitespace OpenParen Literal{kind: LiteralKind::Str{..},..}(old_name) Comma
692             // "new_name"),
693             Whitespace Literal{kind: LiteralKind::Str{..},..}(new_name) CloseParen Comma
694         );
695         lints.push(RenamedLint::new(old_name, new_name));
696     }
697 }
698
699 /// Removes the line splices and surrounding quotes from a string literal
700 fn remove_line_splices(s: &str) -> String {
701     let s = s
702         .strip_prefix('r')
703         .unwrap_or(s)
704         .trim_matches('#')
705         .strip_prefix('"')
706         .and_then(|s| s.strip_suffix('"'))
707         .unwrap_or_else(|| panic!("expected quoted string, found `{}`", s));
708     let mut res = String::with_capacity(s.len());
709     unescape::unescape_literal(s, unescape::Mode::Str, &mut |range, _| res.push_str(&s[range]));
710     res
711 }
712
713 /// Replaces a region in a file delimited by two lines matching regexes.
714 ///
715 /// `path` is the relative path to the file on which you want to perform the replacement.
716 ///
717 /// See `replace_region_in_text` for documentation of the other options.
718 ///
719 /// # Panics
720 ///
721 /// Panics if the path could not read or then written
722 fn replace_region_in_file(
723     update_mode: UpdateMode,
724     path: &Path,
725     start: &str,
726     end: &str,
727     write_replacement: impl FnMut(&mut String),
728 ) {
729     let contents = fs::read_to_string(path).unwrap_or_else(|e| panic!("Cannot read from `{}`: {}", path.display(), e));
730     let new_contents = match replace_region_in_text(&contents, start, end, write_replacement) {
731         Ok(x) => x,
732         Err(delim) => panic!("Couldn't find `{}` in file `{}`", delim, path.display()),
733     };
734
735     match update_mode {
736         UpdateMode::Check if contents != new_contents => exit_with_failure(),
737         UpdateMode::Check => (),
738         UpdateMode::Change => {
739             if let Err(e) = fs::write(path, new_contents.as_bytes()) {
740                 panic!("Cannot write to `{}`: {}", path.display(), e);
741             }
742         },
743     }
744 }
745
746 /// Replaces a region in a text delimited by two strings. Returns the new text if both delimiters
747 /// were found, or the missing delimiter if not.
748 fn replace_region_in_text<'a>(
749     text: &str,
750     start: &'a str,
751     end: &'a str,
752     mut write_replacement: impl FnMut(&mut String),
753 ) -> Result<String, &'a str> {
754     let (text_start, rest) = text.split_once(start).ok_or(start)?;
755     let (_, text_end) = rest.split_once(end).ok_or(end)?;
756
757     let mut res = String::with_capacity(text.len() + 4096);
758     res.push_str(text_start);
759     res.push_str(start);
760     write_replacement(&mut res);
761     res.push_str(end);
762     res.push_str(text_end);
763
764     Ok(res)
765 }
766
767 fn try_rename_file(old_name: &Path, new_name: &Path) -> bool {
768     match fs::OpenOptions::new().create_new(true).write(true).open(new_name) {
769         Ok(file) => drop(file),
770         Err(e) if matches!(e.kind(), io::ErrorKind::AlreadyExists | io::ErrorKind::NotFound) => return false,
771         Err(e) => panic_file(e, new_name, "create"),
772     };
773     match fs::rename(old_name, new_name) {
774         Ok(()) => true,
775         Err(e) => {
776             drop(fs::remove_file(new_name));
777             if e.kind() == io::ErrorKind::NotFound {
778                 false
779             } else {
780                 panic_file(e, old_name, "rename");
781             }
782         },
783     }
784 }
785
786 #[allow(clippy::needless_pass_by_value)]
787 fn panic_file(error: io::Error, name: &Path, action: &str) -> ! {
788     panic!("failed to {} file `{}`: {}", action, name.display(), error)
789 }
790
791 fn rewrite_file(path: &Path, f: impl FnOnce(&str) -> Option<String>) {
792     let mut file = fs::OpenOptions::new()
793         .write(true)
794         .read(true)
795         .open(path)
796         .unwrap_or_else(|e| panic_file(e, path, "open"));
797     let mut buf = String::new();
798     file.read_to_string(&mut buf)
799         .unwrap_or_else(|e| panic_file(e, path, "read"));
800     if let Some(new_contents) = f(&buf) {
801         file.rewind().unwrap_or_else(|e| panic_file(e, path, "write"));
802         file.write_all(new_contents.as_bytes())
803             .unwrap_or_else(|e| panic_file(e, path, "write"));
804         file.set_len(new_contents.len() as u64)
805             .unwrap_or_else(|e| panic_file(e, path, "write"));
806     }
807 }
808
809 fn write_file(path: &Path, contents: &str) {
810     fs::write(path, contents).unwrap_or_else(|e| panic_file(e, path, "write"));
811 }
812
813 #[cfg(test)]
814 mod tests {
815     use super::*;
816
817     #[test]
818     fn test_parse_contents() {
819         static CONTENTS: &str = r#"
820             declare_clippy_lint! {
821                 #[clippy::version = "Hello Clippy!"]
822                 pub PTR_ARG,
823                 style,
824                 "really long \
825                 text"
826             }
827
828             declare_clippy_lint!{
829                 #[clippy::version = "Test version"]
830                 pub DOC_MARKDOWN,
831                 pedantic,
832                 "single line"
833             }
834         "#;
835         let mut result = Vec::new();
836         parse_contents(CONTENTS, "module_name", &mut result);
837
838         let expected = vec![
839             Lint::new("ptr_arg", "style", "\"really long text\"", "module_name"),
840             Lint::new("doc_markdown", "pedantic", "\"single line\"", "module_name"),
841         ];
842         assert_eq!(expected, result);
843     }
844
845     #[test]
846     fn test_parse_deprecated_contents() {
847         static DEPRECATED_CONTENTS: &str = r#"
848             /// some doc comment
849             declare_deprecated_lint! {
850                 #[clippy::version = "I'm a version"]
851                 pub SHOULD_ASSERT_EQ,
852                 "`assert!()` will be more flexible with RFC 2011"
853             }
854         "#;
855
856         let mut result = Vec::new();
857         parse_deprecated_contents(DEPRECATED_CONTENTS, &mut result);
858
859         let expected = vec![DeprecatedLint::new(
860             "should_assert_eq",
861             "\"`assert!()` will be more flexible with RFC 2011\"",
862         )];
863         assert_eq!(expected, result);
864     }
865
866     #[test]
867     fn test_usable_lints() {
868         let lints = vec![
869             Lint::new("should_assert_eq2", "Not Deprecated", "\"abc\"", "module_name"),
870             Lint::new("should_assert_eq2", "internal", "\"abc\"", "module_name"),
871             Lint::new("should_assert_eq2", "internal_style", "\"abc\"", "module_name"),
872         ];
873         let expected = vec![Lint::new(
874             "should_assert_eq2",
875             "Not Deprecated",
876             "\"abc\"",
877             "module_name",
878         )];
879         assert_eq!(expected, Lint::usable_lints(&lints));
880     }
881
882     #[test]
883     fn test_by_lint_group() {
884         let lints = vec![
885             Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name"),
886             Lint::new("should_assert_eq2", "group2", "\"abc\"", "module_name"),
887             Lint::new("incorrect_match", "group1", "\"abc\"", "module_name"),
888         ];
889         let mut expected: HashMap<String, Vec<Lint>> = HashMap::new();
890         expected.insert(
891             "group1".to_string(),
892             vec![
893                 Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name"),
894                 Lint::new("incorrect_match", "group1", "\"abc\"", "module_name"),
895             ],
896         );
897         expected.insert(
898             "group2".to_string(),
899             vec![Lint::new("should_assert_eq2", "group2", "\"abc\"", "module_name")],
900         );
901         assert_eq!(expected, Lint::by_lint_group(lints.into_iter()));
902     }
903
904     #[test]
905     fn test_gen_deprecated() {
906         let lints = vec![
907             DeprecatedLint::new("should_assert_eq", "\"has been superseded by should_assert_eq2\""),
908             DeprecatedLint::new("another_deprecated", "\"will be removed\""),
909         ];
910
911         let expected = GENERATED_FILE_COMMENT.to_string()
912             + &[
913                 "{",
914                 "    store.register_removed(",
915                 "        \"clippy::should_assert_eq\",",
916                 "        \"has been superseded by should_assert_eq2\",",
917                 "    );",
918                 "    store.register_removed(",
919                 "        \"clippy::another_deprecated\",",
920                 "        \"will be removed\",",
921                 "    );",
922                 "}",
923             ]
924             .join("\n")
925             + "\n";
926
927         assert_eq!(expected, gen_deprecated(&lints));
928     }
929
930     #[test]
931     fn test_gen_lint_group_list() {
932         let lints = vec![
933             Lint::new("abc", "group1", "\"abc\"", "module_name"),
934             Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name"),
935             Lint::new("internal", "internal_style", "\"abc\"", "module_name"),
936         ];
937         let expected = GENERATED_FILE_COMMENT.to_string()
938             + &[
939                 "store.register_group(true, \"clippy::group1\", Some(\"clippy_group1\"), vec![",
940                 "    LintId::of(module_name::ABC),",
941                 "    LintId::of(module_name::INTERNAL),",
942                 "    LintId::of(module_name::SHOULD_ASSERT_EQ),",
943                 "])",
944             ]
945             .join("\n")
946             + "\n";
947
948         let result = gen_lint_group_list("group1", lints.iter());
949
950         assert_eq!(expected, result);
951     }
952 }