2022-04-07 18:39:59 +01:00
|
|
|
|
use core::fmt::Write;
|
2021-10-07 11:21:30 +02:00
|
|
|
|
use itertools::Itertools;
|
2022-04-07 18:39:59 +01:00
|
|
|
|
use rustc_lexer::{tokenize, unescape, LiteralKind, TokenKind};
|
2021-10-07 11:21:30 +02:00
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
use std::ffi::OsStr;
|
|
|
|
|
use std::fs;
|
2020-03-31 15:14:29 +02:00
|
|
|
|
use std::path::Path;
|
2021-10-07 11:21:30 +02:00
|
|
|
|
use walkdir::WalkDir;
|
|
|
|
|
|
|
|
|
|
use crate::clippy_project_root;
|
|
|
|
|
|
|
|
|
|
const GENERATED_FILE_COMMENT: &str = "// This file was generated by `cargo dev update_lints`.\n\
|
|
|
|
|
// Use that command to update this file and do not edit by hand.\n\
|
|
|
|
|
// Manual edits will be overwritten.\n\n";
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
const DOCS_LINK: &str = "https://rust-lang.github.io/rust-clippy/master/index.html";
|
2020-03-31 15:14:29 +02:00
|
|
|
|
|
|
|
|
|
#[derive(Clone, Copy, PartialEq)]
|
|
|
|
|
pub enum UpdateMode {
|
|
|
|
|
Check,
|
|
|
|
|
Change,
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 11:21:30 +02:00
|
|
|
|
/// Runs the `update_lints` command.
|
|
|
|
|
///
|
|
|
|
|
/// This updates various generated values from the lint source code.
|
|
|
|
|
///
|
|
|
|
|
/// `update_mode` indicates if the files should be updated or if updates should be checked for.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// Panics if a file path could not read from or then written to
|
2020-03-31 15:14:29 +02:00
|
|
|
|
#[allow(clippy::too_many_lines)]
|
|
|
|
|
pub fn run(update_mode: UpdateMode) {
|
2022-04-07 18:39:59 +01:00
|
|
|
|
let (lints, deprecated_lints) = gather_all();
|
2020-03-31 15:14:29 +02:00
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
let internal_lints = Lint::internal_lints(&lints);
|
|
|
|
|
let usable_lints = Lint::usable_lints(&lints);
|
2020-03-31 15:14:29 +02:00
|
|
|
|
let mut sorted_usable_lints = usable_lints.clone();
|
|
|
|
|
sorted_usable_lints.sort_by_key(|lint| lint.name.clone());
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
replace_region_in_file(
|
|
|
|
|
update_mode,
|
2020-03-31 15:14:29 +02:00
|
|
|
|
Path::new("README.md"),
|
2022-04-07 18:39:59 +01:00
|
|
|
|
"[There are over ",
|
|
|
|
|
" lints included in this crate!]",
|
|
|
|
|
|res| {
|
|
|
|
|
write!(res, "{}", round_to_fifty(usable_lints.len())).unwrap();
|
2020-03-31 15:14:29 +02:00
|
|
|
|
},
|
2022-04-07 18:39:59 +01:00
|
|
|
|
);
|
2020-03-31 15:14:29 +02:00
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
replace_region_in_file(
|
|
|
|
|
update_mode,
|
2020-03-31 15:14:29 +02:00
|
|
|
|
Path::new("CHANGELOG.md"),
|
2022-04-07 18:39:59 +01:00
|
|
|
|
"<!-- begin autogenerated links to lint list -->\n",
|
2020-03-31 15:14:29 +02:00
|
|
|
|
"<!-- end autogenerated links to lint list -->",
|
2022-04-07 18:39:59 +01:00
|
|
|
|
|res| {
|
|
|
|
|
for lint in usable_lints
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|l| &l.name)
|
|
|
|
|
.chain(deprecated_lints.iter().map(|l| &l.name))
|
|
|
|
|
.sorted()
|
|
|
|
|
{
|
|
|
|
|
writeln!(res, "[`{}`]: {}#{}", lint, DOCS_LINK, lint).unwrap();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-03-31 15:14:29 +02:00
|
|
|
|
|
2021-10-07 11:21:30 +02:00
|
|
|
|
// This has to be in lib.rs, otherwise rustfmt doesn't work
|
2022-04-07 18:39:59 +01:00
|
|
|
|
replace_region_in_file(
|
|
|
|
|
update_mode,
|
2020-03-31 15:14:29 +02:00
|
|
|
|
Path::new("clippy_lints/src/lib.rs"),
|
2022-04-07 18:39:59 +01:00
|
|
|
|
"// begin lints modules, do not remove this comment, it’s used in `update_lints`\n",
|
|
|
|
|
"// end lints modules, do not remove this comment, it’s used in `update_lints`",
|
|
|
|
|
|res| {
|
|
|
|
|
for lint_mod in usable_lints.iter().map(|l| &l.module).unique().sorted() {
|
|
|
|
|
writeln!(res, "mod {};", lint_mod).unwrap();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|
2020-03-31 15:14:29 +02:00
|
|
|
|
|
2021-10-07 11:21:30 +02:00
|
|
|
|
process_file(
|
|
|
|
|
"clippy_lints/src/lib.register_lints.rs",
|
|
|
|
|
update_mode,
|
|
|
|
|
&gen_register_lint_list(internal_lints.iter(), usable_lints.iter()),
|
|
|
|
|
);
|
|
|
|
|
process_file(
|
|
|
|
|
"clippy_lints/src/lib.deprecated.rs",
|
|
|
|
|
update_mode,
|
2022-04-07 18:39:59 +01:00
|
|
|
|
&gen_deprecated(&deprecated_lints),
|
2021-10-07 11:21:30 +02:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let all_group_lints = usable_lints.iter().filter(|l| {
|
|
|
|
|
matches!(
|
|
|
|
|
&*l.group,
|
|
|
|
|
"correctness" | "suspicious" | "style" | "complexity" | "perf"
|
2020-03-31 15:14:29 +02:00
|
|
|
|
)
|
2021-10-07 11:21:30 +02:00
|
|
|
|
});
|
|
|
|
|
let content = gen_lint_group_list("all", all_group_lints);
|
|
|
|
|
process_file("clippy_lints/src/lib.register_all.rs", update_mode, &content);
|
2020-03-31 15:14:29 +02:00
|
|
|
|
|
2021-10-07 11:21:30 +02:00
|
|
|
|
for (lint_group, lints) in Lint::by_lint_group(usable_lints.into_iter().chain(internal_lints)) {
|
|
|
|
|
let content = gen_lint_group_list(&lint_group, lints.iter());
|
|
|
|
|
process_file(
|
|
|
|
|
&format!("clippy_lints/src/lib.register_{}.rs", lint_group),
|
|
|
|
|
update_mode,
|
|
|
|
|
&content,
|
2020-03-31 15:14:29 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn print_lints() {
|
2022-04-07 18:39:59 +01:00
|
|
|
|
let (lint_list, _) = gather_all();
|
2020-04-02 22:04:54 +02:00
|
|
|
|
let usable_lints = Lint::usable_lints(&lint_list);
|
2020-03-31 15:14:29 +02:00
|
|
|
|
let usable_lint_count = usable_lints.len();
|
|
|
|
|
let grouped_by_lint_group = Lint::by_lint_group(usable_lints.into_iter());
|
|
|
|
|
|
|
|
|
|
for (lint_group, mut lints) in grouped_by_lint_group {
|
|
|
|
|
println!("\n## {}", lint_group);
|
|
|
|
|
|
|
|
|
|
lints.sort_by_key(|l| l.name.clone());
|
|
|
|
|
|
|
|
|
|
for lint in lints {
|
|
|
|
|
println!("* [{}]({}#{}) ({})", lint.name, DOCS_LINK, lint.name, lint.desc);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println!("there are {} lints", usable_lint_count);
|
|
|
|
|
}
|
2020-03-31 17:23:14 +02:00
|
|
|
|
|
|
|
|
|
fn round_to_fifty(count: usize) -> usize {
|
|
|
|
|
count / 50 * 50
|
|
|
|
|
}
|
2021-10-07 11:21:30 +02:00
|
|
|
|
|
|
|
|
|
fn process_file(path: impl AsRef<Path>, update_mode: UpdateMode, content: &str) {
|
|
|
|
|
if update_mode == UpdateMode::Check {
|
|
|
|
|
let old_content =
|
|
|
|
|
fs::read_to_string(&path).unwrap_or_else(|e| panic!("Cannot read from {}: {}", path.as_ref().display(), e));
|
|
|
|
|
if content != old_content {
|
|
|
|
|
exit_with_failure();
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
fs::write(&path, content.as_bytes())
|
|
|
|
|
.unwrap_or_else(|e| panic!("Cannot write to {}: {}", path.as_ref().display(), e));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn exit_with_failure() {
|
|
|
|
|
println!(
|
|
|
|
|
"Not all lints defined properly. \
|
|
|
|
|
Please run `cargo dev update_lints` to make sure all lints are defined properly."
|
|
|
|
|
);
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Lint data parsed from the Clippy source code.
|
|
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
|
|
|
|
struct Lint {
|
|
|
|
|
name: String,
|
|
|
|
|
group: String,
|
|
|
|
|
desc: String,
|
|
|
|
|
module: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Lint {
|
|
|
|
|
#[must_use]
|
2022-04-07 18:39:59 +01:00
|
|
|
|
fn new(name: &str, group: &str, desc: &str, module: &str) -> Self {
|
2021-10-07 11:21:30 +02:00
|
|
|
|
Self {
|
|
|
|
|
name: name.to_lowercase(),
|
2022-04-07 18:39:59 +01:00
|
|
|
|
group: group.into(),
|
|
|
|
|
desc: remove_line_splices(desc),
|
|
|
|
|
module: module.into(),
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns all non-deprecated lints and non-internal lints
|
|
|
|
|
#[must_use]
|
|
|
|
|
fn usable_lints(lints: &[Self]) -> Vec<Self> {
|
|
|
|
|
lints
|
|
|
|
|
.iter()
|
2022-04-07 18:39:59 +01:00
|
|
|
|
.filter(|l| !l.group.starts_with("internal"))
|
2021-10-07 11:21:30 +02:00
|
|
|
|
.cloned()
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns all internal lints (not `internal_warn` lints)
|
|
|
|
|
#[must_use]
|
|
|
|
|
fn internal_lints(lints: &[Self]) -> Vec<Self> {
|
|
|
|
|
lints.iter().filter(|l| l.group == "internal").cloned().collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the lints in a `HashMap`, grouped by the different lint groups
|
|
|
|
|
#[must_use]
|
|
|
|
|
fn by_lint_group(lints: impl Iterator<Item = Self>) -> HashMap<String, Vec<Self>> {
|
|
|
|
|
lints.map(|lint| (lint.group.to_string(), lint)).into_group_map()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
#[derive(Clone, PartialEq, Debug)]
|
|
|
|
|
struct DeprecatedLint {
|
|
|
|
|
name: String,
|
|
|
|
|
reason: String,
|
|
|
|
|
}
|
|
|
|
|
impl DeprecatedLint {
|
|
|
|
|
fn new(name: &str, reason: &str) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
name: name.to_lowercase(),
|
|
|
|
|
reason: remove_line_splices(reason),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-07 11:21:30 +02:00
|
|
|
|
/// Generates the code for registering a group
|
|
|
|
|
fn gen_lint_group_list<'a>(group_name: &str, lints: impl Iterator<Item = &'a Lint>) -> String {
|
|
|
|
|
let mut details: Vec<_> = lints.map(|l| (&l.module, l.name.to_uppercase())).collect();
|
|
|
|
|
details.sort_unstable();
|
|
|
|
|
|
|
|
|
|
let mut output = GENERATED_FILE_COMMENT.to_string();
|
|
|
|
|
|
|
|
|
|
output.push_str(&format!(
|
|
|
|
|
"store.register_group(true, \"clippy::{0}\", Some(\"clippy_{0}\"), vec![\n",
|
|
|
|
|
group_name
|
|
|
|
|
));
|
|
|
|
|
for (module, name) in details {
|
|
|
|
|
output.push_str(&format!(" LintId::of({}::{}),\n", module, name));
|
|
|
|
|
}
|
|
|
|
|
output.push_str("])\n");
|
|
|
|
|
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generates the `register_removed` code
|
|
|
|
|
#[must_use]
|
2022-04-07 18:39:59 +01:00
|
|
|
|
fn gen_deprecated(lints: &[DeprecatedLint]) -> String {
|
2021-10-07 11:21:30 +02:00
|
|
|
|
let mut output = GENERATED_FILE_COMMENT.to_string();
|
|
|
|
|
output.push_str("{\n");
|
2022-04-07 18:39:59 +01:00
|
|
|
|
for lint in lints {
|
2021-10-07 11:21:30 +02:00
|
|
|
|
output.push_str(&format!(
|
|
|
|
|
concat!(
|
|
|
|
|
" store.register_removed(\n",
|
|
|
|
|
" \"clippy::{}\",\n",
|
|
|
|
|
" \"{}\",\n",
|
|
|
|
|
" );\n"
|
|
|
|
|
),
|
2022-04-07 18:39:59 +01:00
|
|
|
|
lint.name, lint.reason,
|
2021-10-07 11:21:30 +02:00
|
|
|
|
));
|
|
|
|
|
}
|
|
|
|
|
output.push_str("}\n");
|
|
|
|
|
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Generates the code for registering lints
|
|
|
|
|
#[must_use]
|
|
|
|
|
fn gen_register_lint_list<'a>(
|
|
|
|
|
internal_lints: impl Iterator<Item = &'a Lint>,
|
|
|
|
|
usable_lints: impl Iterator<Item = &'a Lint>,
|
|
|
|
|
) -> String {
|
|
|
|
|
let mut details: Vec<_> = internal_lints
|
|
|
|
|
.map(|l| (false, &l.module, l.name.to_uppercase()))
|
|
|
|
|
.chain(usable_lints.map(|l| (true, &l.module, l.name.to_uppercase())))
|
|
|
|
|
.collect();
|
|
|
|
|
details.sort_unstable();
|
|
|
|
|
|
|
|
|
|
let mut output = GENERATED_FILE_COMMENT.to_string();
|
|
|
|
|
output.push_str("store.register_lints(&[\n");
|
|
|
|
|
|
|
|
|
|
for (is_public, module_name, lint_name) in details {
|
|
|
|
|
if !is_public {
|
2022-01-13 13:18:19 +01:00
|
|
|
|
output.push_str(" #[cfg(feature = \"internal\")]\n");
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
output.push_str(&format!(" {}::{},\n", module_name, lint_name));
|
|
|
|
|
}
|
|
|
|
|
output.push_str("])\n");
|
|
|
|
|
|
|
|
|
|
output
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
/// Gathers all lints defined in `clippy_lints/src`
|
|
|
|
|
fn gather_all() -> (Vec<Lint>, Vec<DeprecatedLint>) {
|
|
|
|
|
let mut lints = Vec::with_capacity(1000);
|
|
|
|
|
let mut deprecated_lints = Vec::with_capacity(50);
|
|
|
|
|
let root_path = clippy_project_root().join("clippy_lints/src");
|
2021-10-07 11:21:30 +02:00
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
for (rel_path, file) in WalkDir::new(&root_path)
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(Result::unwrap)
|
|
|
|
|
.filter(|f| f.path().extension() == Some(OsStr::new("rs")))
|
|
|
|
|
.map(|f| (f.path().strip_prefix(&root_path).unwrap().to_path_buf(), f))
|
|
|
|
|
{
|
|
|
|
|
let path = file.path();
|
|
|
|
|
let contents =
|
|
|
|
|
fs::read_to_string(path).unwrap_or_else(|e| panic!("Cannot read from `{}`: {}", path.display(), e));
|
|
|
|
|
let module = rel_path
|
|
|
|
|
.components()
|
|
|
|
|
.map(|c| c.as_os_str().to_str().unwrap())
|
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
|
.join("::");
|
|
|
|
|
|
|
|
|
|
// If the lints are stored in mod.rs, we get the module name from
|
|
|
|
|
// the containing directory:
|
|
|
|
|
let module = if let Some(module) = module.strip_suffix("::mod.rs") {
|
|
|
|
|
module
|
|
|
|
|
} else {
|
|
|
|
|
module.strip_suffix(".rs").unwrap_or(&module)
|
|
|
|
|
};
|
2021-10-07 11:21:30 +02:00
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
if module == "deprecated_lints" {
|
|
|
|
|
parse_deprecated_contents(&contents, &mut deprecated_lints);
|
|
|
|
|
} else {
|
|
|
|
|
parse_contents(&contents, module, &mut lints);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
(lints, deprecated_lints)
|
|
|
|
|
}
|
2021-10-07 11:21:30 +02:00
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
macro_rules! match_tokens {
|
|
|
|
|
($iter:ident, $($token:ident $({$($fields:tt)*})? $(($capture:ident))?)*) => {
|
|
|
|
|
{
|
|
|
|
|
$($(let $capture =)? if let Some((TokenKind::$token $({$($fields)*})?, _x)) = $iter.next() {
|
|
|
|
|
_x
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
};)*
|
|
|
|
|
#[allow(clippy::unused_unit)]
|
|
|
|
|
{ ($($($capture,)?)*) }
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
/// Parse a source file looking for `declare_clippy_lint` macro invocations.
|
|
|
|
|
fn parse_contents(contents: &str, module: &str, lints: &mut Vec<Lint>) {
|
|
|
|
|
let mut offset = 0usize;
|
|
|
|
|
let mut iter = tokenize(contents).map(|t| {
|
|
|
|
|
let range = offset..offset + t.len;
|
|
|
|
|
offset = range.end;
|
|
|
|
|
(t.kind, &contents[range])
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
while iter.any(|(kind, s)| kind == TokenKind::Ident && s == "declare_clippy_lint") {
|
|
|
|
|
let mut iter = iter
|
|
|
|
|
.by_ref()
|
|
|
|
|
.filter(|&(kind, _)| !matches!(kind, TokenKind::Whitespace | TokenKind::LineComment { .. }));
|
|
|
|
|
// matches `!{`
|
|
|
|
|
match_tokens!(iter, Bang OpenBrace);
|
|
|
|
|
match iter.next() {
|
|
|
|
|
// #[clippy::version = "version"] pub
|
|
|
|
|
Some((TokenKind::Pound, _)) => {
|
|
|
|
|
match_tokens!(iter, OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket Ident);
|
|
|
|
|
},
|
|
|
|
|
// pub
|
|
|
|
|
Some((TokenKind::Ident, _)) => (),
|
|
|
|
|
_ => continue,
|
|
|
|
|
}
|
|
|
|
|
let (name, group, desc) = match_tokens!(
|
|
|
|
|
iter,
|
|
|
|
|
// LINT_NAME
|
|
|
|
|
Ident(name) Comma
|
|
|
|
|
// group,
|
|
|
|
|
Ident(group) Comma
|
|
|
|
|
// "description" }
|
|
|
|
|
Literal{..}(desc) CloseBrace
|
|
|
|
|
);
|
|
|
|
|
lints.push(Lint::new(name, group, desc, module));
|
|
|
|
|
}
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
/// Parse a source file looking for `declare_deprecated_lint` macro invocations.
|
|
|
|
|
fn parse_deprecated_contents(contents: &str, lints: &mut Vec<DeprecatedLint>) {
|
|
|
|
|
let mut offset = 0usize;
|
|
|
|
|
let mut iter = tokenize(contents).map(|t| {
|
|
|
|
|
let range = offset..offset + t.len;
|
|
|
|
|
offset = range.end;
|
|
|
|
|
(t.kind, &contents[range])
|
|
|
|
|
});
|
|
|
|
|
while iter.any(|(kind, s)| kind == TokenKind::Ident && s == "declare_deprecated_lint") {
|
|
|
|
|
let mut iter = iter
|
|
|
|
|
.by_ref()
|
|
|
|
|
.filter(|&(kind, _)| !matches!(kind, TokenKind::Whitespace | TokenKind::LineComment { .. }));
|
|
|
|
|
let (name, reason) = match_tokens!(
|
|
|
|
|
iter,
|
|
|
|
|
// !{
|
|
|
|
|
Bang OpenBrace
|
|
|
|
|
// #[clippy::version = "version"]
|
|
|
|
|
Pound OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket
|
|
|
|
|
// pub LINT_NAME,
|
|
|
|
|
Ident Ident(name) Comma
|
|
|
|
|
// "description"
|
|
|
|
|
Literal{kind: LiteralKind::Str{..},..}(reason)
|
|
|
|
|
// }
|
|
|
|
|
CloseBrace
|
|
|
|
|
);
|
|
|
|
|
lints.push(DeprecatedLint::new(name, reason));
|
|
|
|
|
}
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
/// Removes the line splices and surrounding quotes from a string literal
|
|
|
|
|
fn remove_line_splices(s: &str) -> String {
|
|
|
|
|
let s = s
|
|
|
|
|
.strip_prefix('r')
|
|
|
|
|
.unwrap_or(s)
|
|
|
|
|
.trim_matches('#')
|
|
|
|
|
.strip_prefix('"')
|
|
|
|
|
.and_then(|s| s.strip_suffix('"'))
|
|
|
|
|
.unwrap_or_else(|| panic!("expected quoted string, found `{}`", s));
|
|
|
|
|
let mut res = String::with_capacity(s.len());
|
|
|
|
|
unescape::unescape_literal(s, unescape::Mode::Str, &mut |range, _| res.push_str(&s[range]));
|
|
|
|
|
res
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Replaces a region in a file delimited by two lines matching regexes.
|
|
|
|
|
///
|
|
|
|
|
/// `path` is the relative path to the file on which you want to perform the replacement.
|
|
|
|
|
///
|
|
|
|
|
/// See `replace_region_in_text` for documentation of the other options.
|
|
|
|
|
///
|
|
|
|
|
/// # Panics
|
|
|
|
|
///
|
|
|
|
|
/// Panics if the path could not read or then written
|
2022-04-07 18:39:59 +01:00
|
|
|
|
fn replace_region_in_file(
|
|
|
|
|
update_mode: UpdateMode,
|
2021-10-07 11:21:30 +02:00
|
|
|
|
path: &Path,
|
|
|
|
|
start: &str,
|
|
|
|
|
end: &str,
|
2022-04-07 18:39:59 +01:00
|
|
|
|
write_replacement: impl FnMut(&mut String),
|
|
|
|
|
) {
|
|
|
|
|
let contents = fs::read_to_string(path).unwrap_or_else(|e| panic!("Cannot read from `{}`: {}", path.display(), e));
|
|
|
|
|
let new_contents = match replace_region_in_text(&contents, start, end, write_replacement) {
|
|
|
|
|
Ok(x) => x,
|
|
|
|
|
Err(delim) => panic!("Couldn't find `{}` in file `{}`", delim, path.display()),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match update_mode {
|
|
|
|
|
UpdateMode::Check if contents != new_contents => exit_with_failure(),
|
|
|
|
|
UpdateMode::Check => (),
|
|
|
|
|
UpdateMode::Change => {
|
|
|
|
|
if let Err(e) = fs::write(path, new_contents.as_bytes()) {
|
|
|
|
|
panic!("Cannot write to `{}`: {}", path.display(), e);
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
2022-04-07 18:39:59 +01:00
|
|
|
|
},
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
/// Replaces a region in a text delimited by two strings. Returns the new text if both delimiters
|
|
|
|
|
/// were found, or the missing delimiter if not.
|
|
|
|
|
fn replace_region_in_text<'a>(
|
|
|
|
|
text: &str,
|
|
|
|
|
start: &'a str,
|
|
|
|
|
end: &'a str,
|
|
|
|
|
mut write_replacement: impl FnMut(&mut String),
|
|
|
|
|
) -> Result<String, &'a str> {
|
|
|
|
|
let (text_start, rest) = text.split_once(start).ok_or(start)?;
|
|
|
|
|
let (_, text_end) = rest.split_once(end).ok_or(end)?;
|
|
|
|
|
|
|
|
|
|
let mut res = String::with_capacity(text.len() + 4096);
|
|
|
|
|
res.push_str(text_start);
|
|
|
|
|
res.push_str(start);
|
|
|
|
|
write_replacement(&mut res);
|
|
|
|
|
res.push_str(end);
|
|
|
|
|
res.push_str(text_end);
|
|
|
|
|
|
|
|
|
|
Ok(res)
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-04-07 18:39:59 +01:00
|
|
|
|
fn test_parse_contents() {
|
|
|
|
|
static CONTENTS: &str = r#"
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
|
#[clippy::version = "Hello Clippy!"]
|
|
|
|
|
pub PTR_ARG,
|
|
|
|
|
style,
|
|
|
|
|
"really long \
|
|
|
|
|
text"
|
|
|
|
|
}
|
2021-10-07 11:21:30 +02:00
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
declare_clippy_lint!{
|
|
|
|
|
#[clippy::version = "Test version"]
|
|
|
|
|
pub DOC_MARKDOWN,
|
|
|
|
|
pedantic,
|
|
|
|
|
"single line"
|
|
|
|
|
}
|
|
|
|
|
"#;
|
|
|
|
|
let mut result = Vec::new();
|
|
|
|
|
parse_contents(CONTENTS, "module_name", &mut result);
|
|
|
|
|
|
|
|
|
|
let expected = vec![
|
|
|
|
|
Lint::new("ptr_arg", "style", "\"really long text\"", "module_name"),
|
|
|
|
|
Lint::new("doc_markdown", "pedantic", "\"single line\"", "module_name"),
|
|
|
|
|
];
|
2021-10-07 11:21:30 +02:00
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
2022-04-07 18:39:59 +01:00
|
|
|
|
fn test_parse_deprecated_contents() {
|
|
|
|
|
static DEPRECATED_CONTENTS: &str = r#"
|
|
|
|
|
/// some doc comment
|
|
|
|
|
declare_deprecated_lint! {
|
|
|
|
|
#[clippy::version = "I'm a version"]
|
|
|
|
|
pub SHOULD_ASSERT_EQ,
|
|
|
|
|
"`assert!()` will be more flexible with RFC 2011"
|
|
|
|
|
}
|
|
|
|
|
"#;
|
|
|
|
|
|
|
|
|
|
let mut result = Vec::new();
|
|
|
|
|
parse_deprecated_contents(DEPRECATED_CONTENTS, &mut result);
|
|
|
|
|
|
|
|
|
|
let expected = vec![DeprecatedLint::new(
|
|
|
|
|
"should_assert_eq",
|
|
|
|
|
"\"`assert!()` will be more flexible with RFC 2011\"",
|
|
|
|
|
)];
|
2021-10-07 11:21:30 +02:00
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_usable_lints() {
|
|
|
|
|
let lints = vec![
|
2022-04-07 18:39:59 +01:00
|
|
|
|
Lint::new("should_assert_eq2", "Not Deprecated", "\"abc\"", "module_name"),
|
|
|
|
|
Lint::new("should_assert_eq2", "internal", "\"abc\"", "module_name"),
|
|
|
|
|
Lint::new("should_assert_eq2", "internal_style", "\"abc\"", "module_name"),
|
2021-10-07 11:21:30 +02:00
|
|
|
|
];
|
|
|
|
|
let expected = vec![Lint::new(
|
|
|
|
|
"should_assert_eq2",
|
|
|
|
|
"Not Deprecated",
|
2022-04-07 18:39:59 +01:00
|
|
|
|
"\"abc\"",
|
2021-10-07 11:21:30 +02:00
|
|
|
|
"module_name",
|
|
|
|
|
)];
|
|
|
|
|
assert_eq!(expected, Lint::usable_lints(&lints));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_by_lint_group() {
|
|
|
|
|
let lints = vec![
|
2022-04-07 18:39:59 +01:00
|
|
|
|
Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name"),
|
|
|
|
|
Lint::new("should_assert_eq2", "group2", "\"abc\"", "module_name"),
|
|
|
|
|
Lint::new("incorrect_match", "group1", "\"abc\"", "module_name"),
|
2021-10-07 11:21:30 +02:00
|
|
|
|
];
|
|
|
|
|
let mut expected: HashMap<String, Vec<Lint>> = HashMap::new();
|
|
|
|
|
expected.insert(
|
|
|
|
|
"group1".to_string(),
|
|
|
|
|
vec![
|
2022-04-07 18:39:59 +01:00
|
|
|
|
Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name"),
|
|
|
|
|
Lint::new("incorrect_match", "group1", "\"abc\"", "module_name"),
|
2021-10-07 11:21:30 +02:00
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
expected.insert(
|
|
|
|
|
"group2".to_string(),
|
2022-04-07 18:39:59 +01:00
|
|
|
|
vec![Lint::new("should_assert_eq2", "group2", "\"abc\"", "module_name")],
|
2021-10-07 11:21:30 +02:00
|
|
|
|
);
|
|
|
|
|
assert_eq!(expected, Lint::by_lint_group(lints.into_iter()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_gen_deprecated() {
|
|
|
|
|
let lints = vec![
|
2022-04-07 18:39:59 +01:00
|
|
|
|
DeprecatedLint::new("should_assert_eq", "\"has been superseded by should_assert_eq2\""),
|
|
|
|
|
DeprecatedLint::new("another_deprecated", "\"will be removed\""),
|
2021-10-07 11:21:30 +02:00
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
let expected = GENERATED_FILE_COMMENT.to_string()
|
|
|
|
|
+ &[
|
|
|
|
|
"{",
|
|
|
|
|
" store.register_removed(",
|
|
|
|
|
" \"clippy::should_assert_eq\",",
|
|
|
|
|
" \"has been superseded by should_assert_eq2\",",
|
|
|
|
|
" );",
|
|
|
|
|
" store.register_removed(",
|
|
|
|
|
" \"clippy::another_deprecated\",",
|
|
|
|
|
" \"will be removed\",",
|
|
|
|
|
" );",
|
|
|
|
|
"}",
|
|
|
|
|
]
|
|
|
|
|
.join("\n")
|
|
|
|
|
+ "\n";
|
|
|
|
|
|
2022-04-07 18:39:59 +01:00
|
|
|
|
assert_eq!(expected, gen_deprecated(&lints));
|
2021-10-07 11:21:30 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn test_gen_lint_group_list() {
|
|
|
|
|
let lints = vec![
|
2022-04-07 18:39:59 +01:00
|
|
|
|
Lint::new("abc", "group1", "\"abc\"", "module_name"),
|
|
|
|
|
Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name"),
|
|
|
|
|
Lint::new("internal", "internal_style", "\"abc\"", "module_name"),
|
2021-10-07 11:21:30 +02:00
|
|
|
|
];
|
|
|
|
|
let expected = GENERATED_FILE_COMMENT.to_string()
|
|
|
|
|
+ &[
|
|
|
|
|
"store.register_group(true, \"clippy::group1\", Some(\"clippy_group1\"), vec![",
|
|
|
|
|
" LintId::of(module_name::ABC),",
|
|
|
|
|
" LintId::of(module_name::INTERNAL),",
|
|
|
|
|
" LintId::of(module_name::SHOULD_ASSERT_EQ),",
|
|
|
|
|
"])",
|
|
|
|
|
]
|
|
|
|
|
.join("\n")
|
|
|
|
|
+ "\n";
|
|
|
|
|
|
|
|
|
|
let result = gen_lint_group_list("group1", lints.iter());
|
|
|
|
|
|
|
|
|
|
assert_eq!(expected, result);
|
|
|
|
|
}
|
|
|
|
|
}
|