]> git.lizzy.rs Git - rust.git/blob - src/tools/clippy/clippy_dev/src/ra_setup.rs
Rollup merge of #81556 - nikomatsakis:forbidden-lint-groups-lint, r=pnkfelix
[rust.git] / src / tools / clippy / clippy_dev / src / ra_setup.rs
1 use std::fs;
2 use std::fs::File;
3 use std::io::prelude::*;
4 use std::path::{Path, PathBuf};
5
6 // This module takes an absolute path to a rustc repo and alters the dependencies to point towards
7 // the respective rustc subcrates instead of using extern crate xyz.
8 // This allows rust analyzer to analyze rustc internals and show proper information inside clippy
9 // code. See https://github.com/rust-analyzer/rust-analyzer/issues/3517 and https://github.com/rust-lang/rust-clippy/issues/5514 for details
10
11 /// # Panics
12 ///
13 /// Panics if `rustc_path` does not lead to a rustc repo or the files could not be read
14 pub fn run(rustc_path: Option<&str>) {
15     // we can unwrap here because the arg is required by clap
16     let rustc_path = PathBuf::from(rustc_path.unwrap());
17     assert!(rustc_path.is_dir(), "path is not a directory");
18     let rustc_source_basedir = rustc_path.join("compiler");
19     assert!(
20         rustc_source_basedir.is_dir(),
21         "are you sure the path leads to a rustc repo?"
22     );
23
24     let clippy_root_manifest = fs::read_to_string("Cargo.toml").expect("failed to read ./Cargo.toml");
25     let clippy_root_lib_rs = fs::read_to_string("src/driver.rs").expect("failed to read ./src/driver.rs");
26     inject_deps_into_manifest(
27         &rustc_source_basedir,
28         "Cargo.toml",
29         &clippy_root_manifest,
30         &clippy_root_lib_rs,
31     )
32     .expect("Failed to inject deps into ./Cargo.toml");
33
34     let clippy_lints_manifest =
35         fs::read_to_string("clippy_lints/Cargo.toml").expect("failed to read ./clippy_lints/Cargo.toml");
36     let clippy_lints_lib_rs =
37         fs::read_to_string("clippy_lints/src/lib.rs").expect("failed to read ./clippy_lints/src/lib.rs");
38     inject_deps_into_manifest(
39         &rustc_source_basedir,
40         "clippy_lints/Cargo.toml",
41         &clippy_lints_manifest,
42         &clippy_lints_lib_rs,
43     )
44     .expect("Failed to inject deps into ./clippy_lints/Cargo.toml");
45 }
46
47 fn inject_deps_into_manifest(
48     rustc_source_dir: &Path,
49     manifest_path: &str,
50     cargo_toml: &str,
51     lib_rs: &str,
52 ) -> std::io::Result<()> {
53     // do not inject deps if we have aleady done so
54     if cargo_toml.contains("[target.'cfg(NOT_A_PLATFORM)'.dependencies]") {
55         eprintln!(
56             "cargo dev ra_setup: warning: deps already found inside {}, doing nothing.",
57             manifest_path
58         );
59         return Ok(());
60     }
61
62     let extern_crates = lib_rs
63         .lines()
64         // get the deps
65         .filter(|line| line.starts_with("extern crate"))
66         // we have something like "extern crate foo;", we only care about the "foo"
67         //              ↓          ↓
68         // extern crate rustc_middle;
69         .map(|s| &s[13..(s.len() - 1)]);
70
71     let new_deps = extern_crates.map(|dep| {
72         // format the dependencies that are going to be put inside the Cargo.toml
73         format!(
74             "{dep} = {{ path = \"{source_path}/{dep}\" }}\n",
75             dep = dep,
76             source_path = rustc_source_dir.display()
77         )
78     });
79
80     // format a new [dependencies]-block with the new deps we need to inject
81     let mut all_deps = String::from("[target.'cfg(NOT_A_PLATFORM)'.dependencies]\n");
82     new_deps.for_each(|dep_line| {
83         all_deps.push_str(&dep_line);
84     });
85     all_deps.push_str("\n[dependencies]\n");
86
87     // replace "[dependencies]" with
88     // [dependencies]
89     // dep1 = { path = ... }
90     // dep2 = { path = ... }
91     // etc
92     let new_manifest = cargo_toml.replacen("[dependencies]\n", &all_deps, 1);
93
94     // println!("{}", new_manifest);
95     let mut file = File::create(manifest_path)?;
96     file.write_all(new_manifest.as_bytes())?;
97
98     println!("Dependency paths injected: {}", manifest_path);
99
100     Ok(())
101 }