2020-10-20 21:29:31 +02:00
|
|
|
//! Generates descriptors structure for unstable feature from Unstable Book
|
2021-06-04 18:55:08 +02:00
|
|
|
use std::borrow::Cow;
|
2021-02-27 16:25:06 +02:00
|
|
|
use std::fmt::Write;
|
2020-10-20 21:29:31 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
use xshell::{cmd, read_file};
|
|
|
|
|
2021-03-08 21:39:09 +03:00
|
|
|
use crate::codegen::{ensure_file_contents, project_root, reformat, Result};
|
2020-10-20 21:29:31 +02:00
|
|
|
|
2021-03-08 21:39:09 +03:00
|
|
|
pub(crate) fn generate_lint_completions() -> Result<()> {
|
2021-03-08 21:49:25 +03:00
|
|
|
if !project_root().join("./target/rust").exists() {
|
2020-10-20 21:29:31 +02:00
|
|
|
cmd!("git clone --depth=1 https://github.com/rust-lang/rust ./target/rust").run()?;
|
|
|
|
}
|
|
|
|
|
2021-06-04 18:35:19 +02:00
|
|
|
let mut contents = String::from(
|
|
|
|
r#"pub struct Lint {
|
|
|
|
pub label: &'static str,
|
|
|
|
pub description: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
generate_lint_descriptor(&mut contents)?;
|
|
|
|
contents.push('\n');
|
|
|
|
|
|
|
|
generate_feature_descriptor(&mut contents, "./target/rust/src/doc/unstable-book/src".into())?;
|
2021-02-27 16:25:06 +02:00
|
|
|
contents.push('\n');
|
2020-10-20 21:29:31 +02:00
|
|
|
|
2021-06-07 10:40:12 +03:00
|
|
|
cmd!("curl https://rust-lang.github.io/rust-clippy/master/lints.json --output ./target/clippy_lints.json").run()?;
|
2021-02-27 16:25:06 +02:00
|
|
|
generate_descriptor_clippy(&mut contents, &Path::new("./target/clippy_lints.json"))?;
|
|
|
|
let contents = reformat(&contents)?;
|
2020-10-20 21:29:31 +02:00
|
|
|
|
2021-06-04 18:35:19 +02:00
|
|
|
let destination = project_root().join("crates/ide_db/src/helpers/generated_lints.rs");
|
2021-03-08 21:39:09 +03:00
|
|
|
ensure_file_contents(destination.as_path(), &contents)?;
|
2020-10-20 21:29:31 +02:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-04 18:35:19 +02:00
|
|
|
fn generate_lint_descriptor(buf: &mut String) -> Result<()> {
|
|
|
|
let stdout = cmd!("rustc -W help").read()?;
|
2021-06-04 18:55:08 +02:00
|
|
|
let start_lints =
|
|
|
|
stdout.find("---- ------- -------").ok_or_else(|| anyhow::format_err!(""))?;
|
|
|
|
let start_lint_groups =
|
|
|
|
stdout.find("---- ---------").ok_or_else(|| anyhow::format_err!(""))?;
|
|
|
|
let end_lints =
|
|
|
|
stdout.find("Lint groups provided by rustc:").ok_or_else(|| anyhow::format_err!(""))?;
|
|
|
|
let end_lint_groups = stdout
|
|
|
|
.find("Lint tools like Clippy can provide additional lints and lint groups.")
|
|
|
|
.ok_or_else(|| anyhow::format_err!(""))?;
|
2021-06-04 18:35:19 +02:00
|
|
|
buf.push_str(r#"pub const DEFAULT_LINTS: &[Lint] = &["#);
|
|
|
|
buf.push('\n');
|
2021-06-04 18:55:08 +02:00
|
|
|
let mut lints = stdout[start_lints..end_lints]
|
2021-06-04 18:35:19 +02:00
|
|
|
.lines()
|
2021-06-04 19:03:45 +02:00
|
|
|
.skip(1)
|
2021-06-04 18:35:19 +02:00
|
|
|
.filter(|l| !l.is_empty())
|
2021-06-04 18:55:08 +02:00
|
|
|
.map(|line| {
|
|
|
|
let (name, rest) = line.trim().split_once(char::is_whitespace).unwrap();
|
|
|
|
let (_default_level, description) =
|
|
|
|
rest.trim().split_once(char::is_whitespace).unwrap();
|
|
|
|
(name.trim(), Cow::Borrowed(description.trim()))
|
2021-06-04 18:35:19 +02:00
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
2021-06-04 19:03:45 +02:00
|
|
|
lints.extend(
|
|
|
|
stdout[start_lint_groups..end_lint_groups].lines().skip(1).filter(|l| !l.is_empty()).map(
|
|
|
|
|line| {
|
|
|
|
let (name, lints) = line.trim().split_once(char::is_whitespace).unwrap();
|
|
|
|
(name.trim(), format!("lint group for: {}", lints.trim()).into())
|
|
|
|
},
|
|
|
|
),
|
|
|
|
);
|
2021-06-04 18:55:08 +02:00
|
|
|
|
2021-06-04 18:35:19 +02:00
|
|
|
lints.sort_by(|(ident, _), (ident2, _)| ident.cmp(ident2));
|
2021-06-04 19:03:45 +02:00
|
|
|
lints.into_iter().for_each(|(name, description)| {
|
|
|
|
push_lint_completion(buf, &name.replace("-", "_"), &description)
|
|
|
|
});
|
2021-06-04 18:35:19 +02:00
|
|
|
buf.push_str("];\n");
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn generate_feature_descriptor(buf: &mut String, src_dir: PathBuf) -> Result<()> {
|
|
|
|
buf.push_str(r#"pub const FEATURES: &[Lint] = &["#);
|
2021-02-27 16:25:06 +02:00
|
|
|
buf.push('\n');
|
2021-06-04 17:03:18 +02:00
|
|
|
let mut vec = ["language-features", "library-features"]
|
2020-10-20 21:29:31 +02:00
|
|
|
.iter()
|
|
|
|
.flat_map(|it| WalkDir::new(src_dir.join(it)))
|
|
|
|
.filter_map(|e| e.ok())
|
|
|
|
.filter(|entry| {
|
|
|
|
// Get all `.md ` files
|
|
|
|
entry.file_type().is_file() && entry.path().extension().unwrap_or_default() == "md"
|
|
|
|
})
|
2021-06-04 17:03:18 +02:00
|
|
|
.map(|entry| {
|
2020-10-20 21:29:31 +02:00
|
|
|
let path = entry.path();
|
|
|
|
let feature_ident = path.file_stem().unwrap().to_str().unwrap().replace("-", "_");
|
|
|
|
let doc = read_file(path).unwrap();
|
2021-06-04 17:03:18 +02:00
|
|
|
(feature_ident, doc)
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
vec.sort_by(|(feature_ident, _), (feature_ident2, _)| feature_ident.cmp(feature_ident2));
|
2021-06-04 18:35:19 +02:00
|
|
|
vec.into_iter()
|
|
|
|
.for_each(|(feature_ident, doc)| push_lint_completion(buf, &feature_ident, &doc));
|
2021-02-27 16:25:06 +02:00
|
|
|
buf.push_str("];\n");
|
|
|
|
Ok(())
|
2020-10-20 21:29:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct ClippyLint {
|
|
|
|
help: String,
|
|
|
|
id: String,
|
|
|
|
}
|
|
|
|
|
2021-06-05 19:14:53 +02:00
|
|
|
fn unescape(s: &str) -> String {
|
|
|
|
s.replace(r#"\""#, "").replace(r#"\n"#, "\n").replace(r#"\r"#, "")
|
|
|
|
}
|
|
|
|
|
2021-02-27 16:25:06 +02:00
|
|
|
fn generate_descriptor_clippy(buf: &mut String, path: &Path) -> Result<()> {
|
2020-10-20 21:29:31 +02:00
|
|
|
let file_content = read_file(path)?;
|
|
|
|
let mut clippy_lints: Vec<ClippyLint> = vec![];
|
|
|
|
|
|
|
|
for line in file_content.lines().map(|line| line.trim()) {
|
|
|
|
if line.starts_with(r#""id":"#) {
|
|
|
|
let clippy_lint = ClippyLint {
|
|
|
|
id: line
|
|
|
|
.strip_prefix(r#""id": ""#)
|
|
|
|
.expect("should be prefixed by id")
|
|
|
|
.strip_suffix(r#"","#)
|
|
|
|
.expect("should be suffixed by comma")
|
|
|
|
.into(),
|
|
|
|
help: String::new(),
|
|
|
|
};
|
|
|
|
clippy_lints.push(clippy_lint)
|
|
|
|
} else if line.starts_with(r#""What it does":"#) {
|
|
|
|
// Typical line to strip: "What is doest": "Here is my useful content",
|
|
|
|
let prefix_to_strip = r#""What it does": ""#;
|
|
|
|
let suffix_to_strip = r#"","#;
|
|
|
|
|
|
|
|
let clippy_lint = clippy_lints.last_mut().expect("clippy lint must already exist");
|
|
|
|
clippy_lint.help = line
|
|
|
|
.strip_prefix(prefix_to_strip)
|
|
|
|
.expect("should be prefixed by what it does")
|
|
|
|
.strip_suffix(suffix_to_strip)
|
2021-06-05 19:14:53 +02:00
|
|
|
.map(unescape)
|
2020-10-20 21:29:31 +02:00
|
|
|
.expect("should be suffixed by comma")
|
|
|
|
.into();
|
|
|
|
}
|
|
|
|
}
|
2021-06-04 17:03:18 +02:00
|
|
|
clippy_lints.sort_by(|lint, lint2| lint.id.cmp(&lint2.id));
|
2021-06-04 18:35:19 +02:00
|
|
|
buf.push_str(r#"pub const CLIPPY_LINTS: &[Lint] = &["#);
|
2021-02-27 16:25:06 +02:00
|
|
|
buf.push('\n');
|
|
|
|
clippy_lints.into_iter().for_each(|clippy_lint| {
|
2020-10-20 21:29:31 +02:00
|
|
|
let lint_ident = format!("clippy::{}", clippy_lint.id);
|
|
|
|
let doc = clippy_lint.help;
|
2021-02-27 16:25:06 +02:00
|
|
|
push_lint_completion(buf, &lint_ident, &doc);
|
2020-10-20 21:29:31 +02:00
|
|
|
});
|
|
|
|
|
2021-02-27 16:25:06 +02:00
|
|
|
buf.push_str("];\n");
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2020-10-20 21:29:31 +02:00
|
|
|
|
2021-02-27 16:25:06 +02:00
|
|
|
fn push_lint_completion(buf: &mut String, label: &str, description: &str) {
|
|
|
|
writeln!(
|
|
|
|
buf,
|
2021-06-04 18:35:19 +02:00
|
|
|
r###" Lint {{
|
2021-02-27 16:25:06 +02:00
|
|
|
label: "{}",
|
|
|
|
description: r##"{}"##
|
|
|
|
}},"###,
|
|
|
|
label, description
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-10-20 21:29:31 +02:00
|
|
|
}
|