Skip to content

Commit 0d1e176

Browse files
committed
Auto merge of #5655 - matthiaskrgr:source_mod, r=flip1995
cargo_dev: add ra_setup It takes an absolute path to a rustc repo and adds path-dependencies that point towards the respective rustc subcrates into the Cargo.tomls of the clippy and clippy_lints crate. This allows rustc-analyzer to show proper type annotations etc on rustc-internals inside the clippy repo. Usage: cargo dev ra-setup /absolute/path/to/rust/ cc rust-lang/rust-analyzer#3517 cc #5514 changelog: none
2 parents 7ea7cd1 + 7b49090 commit 0d1e176

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

clippy_dev/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use walkdir::WalkDir;
1111

1212
pub mod fmt;
1313
pub mod new_lint;
14+
pub mod ra_setup;
1415
pub mod stderr_length_check;
1516
pub mod update_lints;
1617

@@ -400,7 +401,7 @@ fn test_replace_region_no_changes() {
400401
changed: false,
401402
new_lines: "123\n456\n789".to_string(),
402403
};
403-
let result = replace_region_in_text(text, r#"^\s*123$"#, r#"^\s*456"#, false, || vec![]);
404+
let result = replace_region_in_text(text, r#"^\s*123$"#, r#"^\s*456"#, false, Vec::new);
404405
assert_eq!(expected, result);
405406
}
406407

clippy_dev/src/main.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
22

33
use clap::{App, Arg, SubCommand};
4-
use clippy_dev::{fmt, new_lint, stderr_length_check, update_lints};
4+
use clippy_dev::{fmt, new_lint, ra_setup, stderr_length_check, update_lints};
55

66
fn main() {
77
let matches = App::new("Clippy developer tooling")
@@ -87,6 +87,19 @@ fn main() {
8787
SubCommand::with_name("limit_stderr_length")
8888
.about("Ensures that stderr files do not grow longer than a certain amount of lines."),
8989
)
90+
.subcommand(
91+
SubCommand::with_name("ra-setup")
92+
.about("Alter dependencies so rust-analyzer can find rustc internals")
93+
.arg(
94+
Arg::with_name("rustc-repo-path")
95+
.long("repo-path")
96+
.short("r")
97+
.help("The path to a rustc repo that will be used for setting the dependencies")
98+
.takes_value(true)
99+
.value_name("path")
100+
.required(true),
101+
),
102+
)
90103
.get_matches();
91104

92105
match matches.subcommand() {
@@ -115,6 +128,7 @@ fn main() {
115128
("limit_stderr_length", _) => {
116129
stderr_length_check::check();
117130
},
131+
("ra-setup", Some(matches)) => ra_setup::run(matches.value_of("rustc-repo-path")),
118132
_ => {},
119133
}
120134
}

clippy_dev/src/ra_setup.rs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#![allow(clippy::filter_map)]
2+
3+
use std::fs;
4+
use std::fs::File;
5+
use std::io::prelude::*;
6+
use std::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 here
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("src");
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: &PathBuf,
48+
manifest_path: &str,
49+
cargo_toml: &str,
50+
lib_rs: &str,
51+
) -> std::io::Result<()> {
52+
let extern_crates = lib_rs
53+
.lines()
54+
// get the deps
55+
.filter(|line| line.starts_with("extern crate"))
56+
// we have something like "extern crate foo;", we only care about the "foo"
57+
// ↓ ↓
58+
// extern crate rustc_middle;
59+
.map(|s| &s[13..(s.len() - 1)]);
60+
61+
let new_deps = extern_crates.map(|dep| {
62+
// format the dependencies that are going to be put inside the Cargo.toml
63+
format!(
64+
"{dep} = {{ path = \"{source_path}/lib{dep}\" }}\n",
65+
dep = dep,
66+
source_path = rustc_source_dir.display()
67+
)
68+
});
69+
70+
// format a new [dependencies]-block with the new deps we need to inject
71+
let mut all_deps = String::from("[dependencies]\n");
72+
new_deps.for_each(|dep_line| {
73+
all_deps.push_str(&dep_line);
74+
});
75+
76+
// replace "[dependencies]" with
77+
// [dependencies]
78+
// dep1 = { path = ... }
79+
// dep2 = { path = ... }
80+
// etc
81+
let new_manifest = cargo_toml.replacen("[dependencies]\n", &all_deps, 1);
82+
83+
// println!("{}", new_manifest);
84+
let mut file = File::create(manifest_path)?;
85+
file.write_all(new_manifest.as_bytes())?;
86+
87+
println!("Dependency paths injected: {}", manifest_path);
88+
89+
Ok(())
90+
}

0 commit comments

Comments
 (0)