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