From 4e8199b572f2035b7749cba276ece3a26630d23e Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 14:18:21 +0200 Subject: Adding upstream version 1.67.1+dfsg1. Signed-off-by: Daniel Baumann --- src/tools/clippy/clippy_dev/Cargo.toml | 1 - src/tools/clippy/clippy_dev/src/lint.rs | 8 - src/tools/clippy/clippy_dev/src/new_lint.rs | 16 +- src/tools/clippy/clippy_dev/src/setup/git_hook.rs | 2 +- src/tools/clippy/clippy_dev/src/update_lints.rs | 307 ++++++---------------- 5 files changed, 92 insertions(+), 242 deletions(-) (limited to 'src/tools/clippy/clippy_dev') diff --git a/src/tools/clippy/clippy_dev/Cargo.toml b/src/tools/clippy/clippy_dev/Cargo.toml index 2ac3b4fe2..510c7e852 100644 --- a/src/tools/clippy/clippy_dev/Cargo.toml +++ b/src/tools/clippy/clippy_dev/Cargo.toml @@ -10,7 +10,6 @@ indoc = "1.0" itertools = "0.10.1" opener = "0.5" shell-escape = "0.1" -tempfile = "3.2" walkdir = "2.3" [features] diff --git a/src/tools/clippy/clippy_dev/src/lint.rs b/src/tools/clippy/clippy_dev/src/lint.rs index 71005449b..aafd0f71a 100644 --- a/src/tools/clippy/clippy_dev/src/lint.rs +++ b/src/tools/clippy/clippy_dev/src/lint.rs @@ -36,20 +36,12 @@ pub fn run<'a>(path: &str, args: impl Iterator) { } else { exit_if_err(Command::new("cargo").arg("build").status()); - // Run in a tempdir as changes to clippy do not retrigger linting - let target = tempfile::Builder::new() - .prefix("clippy") - .tempdir() - .expect("failed to create tempdir"); - let status = Command::new(cargo_clippy_path()) .arg("clippy") .args(args) .current_dir(path) - .env("CARGO_TARGET_DIR", target.as_ref()) .status(); - target.close().expect("failed to remove tempdir"); exit_if_err(status); } } diff --git a/src/tools/clippy/clippy_dev/src/new_lint.rs b/src/tools/clippy/clippy_dev/src/new_lint.rs index 9e15f1504..ec7f1dd0d 100644 --- a/src/tools/clippy/clippy_dev/src/new_lint.rs +++ b/src/tools/clippy/clippy_dev/src/new_lint.rs @@ -120,7 +120,7 @@ fn add_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> { let new_lint = if enable_msrv { format!( - "store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv)));\n ", + "store.register_{lint_pass}_pass(move |{ctor_arg}| Box::new({module_name}::{camel_name}::new(msrv())));\n ", lint_pass = lint.pass, ctor_arg = if lint.pass == "late" { "_" } else { "" }, module_name = lint.name, @@ -238,10 +238,9 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String { result.push_str(&if enable_msrv { formatdoc!( r#" - use clippy_utils::msrvs; + use clippy_utils::msrvs::{{self, Msrv}}; {pass_import} use rustc_lint::{{{context_import}, {pass_type}, LintContext}}; - use rustc_semver::RustcVersion; use rustc_session::{{declare_tool_lint, impl_lint_pass}}; "# @@ -263,12 +262,12 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String { formatdoc!( r#" pub struct {name_camel} {{ - msrv: Option, + msrv: Msrv, }} impl {name_camel} {{ #[must_use] - pub fn new(msrv: Option) -> Self {{ + pub fn new(msrv: Msrv) -> Self {{ Self {{ msrv }} }} }} @@ -357,15 +356,14 @@ fn create_lint_for_ty(lint: &LintData<'_>, enable_msrv: bool, ty: &str) -> io::R let _ = writedoc!( lint_file_contents, r#" - use clippy_utils::{{meets_msrv, msrvs}}; + use clippy_utils::msrvs::{{self, Msrv}}; use rustc_lint::{{{context_import}, LintContext}}; - use rustc_semver::RustcVersion; use super::{name_upper}; // TODO: Adjust the parameters as necessary - pub(super) fn check(cx: &{context_import}, msrv: Option) {{ - if !meets_msrv(msrv, todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{ + pub(super) fn check(cx: &{context_import}, msrv: &Msrv) {{ + if !msrv.meets(todo!("Add a new entry in `clippy_utils/src/msrvs`")) {{ return; }} todo!(); diff --git a/src/tools/clippy/clippy_dev/src/setup/git_hook.rs b/src/tools/clippy/clippy_dev/src/setup/git_hook.rs index 1de5b1940..c7c53bc69 100644 --- a/src/tools/clippy/clippy_dev/src/setup/git_hook.rs +++ b/src/tools/clippy/clippy_dev/src/setup/git_hook.rs @@ -6,7 +6,7 @@ use super::verify_inside_clippy_dir; /// Rusts setup uses `git rev-parse --git-common-dir` to get the root directory of the repo. /// I've decided against this for the sake of simplicity and to make sure that it doesn't install /// the hook if `clippy_dev` would be used in the rust tree. The hook also references this tool -/// for formatting and should therefor only be used in a normal clone of clippy +/// for formatting and should therefore only be used in a normal clone of clippy const REPO_GIT_DIR: &str = ".git"; const HOOK_SOURCE_FILE: &str = "util/etc/pre-commit.sh"; const HOOK_TARGET_FILE: &str = ".git/hooks/pre-commit"; diff --git a/src/tools/clippy/clippy_dev/src/update_lints.rs b/src/tools/clippy/clippy_dev/src/update_lints.rs index e690bc369..837618c92 100644 --- a/src/tools/clippy/clippy_dev/src/update_lints.rs +++ b/src/tools/clippy/clippy_dev/src/update_lints.rs @@ -3,7 +3,7 @@ use aho_corasick::AhoCorasickBuilder; use indoc::writedoc; use itertools::Itertools; use rustc_lexer::{tokenize, unescape, LiteralKind, TokenKind}; -use std::collections::{BTreeSet, HashMap, HashSet}; +use std::collections::{HashMap, HashSet}; use std::ffi::OsStr; use std::fmt::Write; use std::fs::{self, OpenOptions}; @@ -36,6 +36,60 @@ pub enum UpdateMode { pub fn update(update_mode: UpdateMode) { let (lints, deprecated_lints, renamed_lints) = gather_all(); generate_lint_files(update_mode, &lints, &deprecated_lints, &renamed_lints); + remove_old_files(update_mode); +} + +/// Remove files no longer needed after +/// that may be reintroduced unintentionally +/// +/// FIXME: This is a temporary measure that should be removed when there are no more PRs that +/// include the stray files +fn remove_old_files(update_mode: UpdateMode) { + let mut failed = false; + let mut remove_file = |path: &Path| match update_mode { + UpdateMode::Check => { + if path.exists() { + failed = true; + println!("unexpected file: {}", path.display()); + } + }, + UpdateMode::Change => { + if fs::remove_file(path).is_ok() { + println!("removed file: {}", path.display()); + } + }, + }; + + let files = [ + "clippy_lints/src/lib.register_all.rs", + "clippy_lints/src/lib.register_cargo.rs", + "clippy_lints/src/lib.register_complexity.rs", + "clippy_lints/src/lib.register_correctness.rs", + "clippy_lints/src/lib.register_internal.rs", + "clippy_lints/src/lib.register_lints.rs", + "clippy_lints/src/lib.register_nursery.rs", + "clippy_lints/src/lib.register_pedantic.rs", + "clippy_lints/src/lib.register_perf.rs", + "clippy_lints/src/lib.register_restriction.rs", + "clippy_lints/src/lib.register_style.rs", + "clippy_lints/src/lib.register_suspicious.rs", + "src/docs.rs", + ]; + + for file in files { + remove_file(Path::new(file)); + } + + if let Ok(docs_dir) = fs::read_dir("src/docs") { + for doc_file in docs_dir { + let path = doc_file.unwrap().path(); + remove_file(&path); + } + } + + if failed { + exit_with_failure(); + } } fn generate_lint_files( @@ -104,9 +158,9 @@ fn generate_lint_files( ); process_file( - "clippy_lints/src/lib.register_lints.rs", + "clippy_lints/src/declared_lints.rs", update_mode, - &gen_register_lint_list(internal_lints.iter(), usable_lints.iter()), + &gen_declared_lints(internal_lints.iter(), usable_lints.iter()), ); process_file( "clippy_lints/src/lib.deprecated.rs", @@ -114,26 +168,6 @@ fn generate_lint_files( &gen_deprecated(deprecated_lints), ); - let all_group_lints = usable_lints.iter().filter(|l| { - matches!( - &*l.group, - "correctness" | "suspicious" | "style" | "complexity" | "perf" - ) - }); - let content = gen_lint_group_list("all", all_group_lints); - process_file("clippy_lints/src/lib.register_all.rs", update_mode, &content); - - update_docs(update_mode, &usable_lints); - - 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_{lint_group}.rs"), - update_mode, - &content, - ); - } - let content = gen_deprecated_lints_test(deprecated_lints); process_file("tests/ui/deprecated.rs", update_mode, &content); @@ -141,62 +175,6 @@ fn generate_lint_files( process_file("tests/ui/rename.rs", update_mode, &content); } -fn update_docs(update_mode: UpdateMode, usable_lints: &[Lint]) { - replace_region_in_file(update_mode, Path::new("src/docs.rs"), "docs! {\n", "\n}\n", |res| { - for name in usable_lints.iter().map(|lint| lint.name.clone()).sorted() { - writeln!(res, r#" "{name}","#).unwrap(); - } - }); - - if update_mode == UpdateMode::Check { - let mut extra = BTreeSet::new(); - let mut lint_names = usable_lints - .iter() - .map(|lint| lint.name.clone()) - .collect::>(); - for file in std::fs::read_dir("src/docs").unwrap() { - let filename = file.unwrap().file_name().into_string().unwrap(); - if let Some(name) = filename.strip_suffix(".txt") { - if !lint_names.remove(name) { - extra.insert(name.to_string()); - } - } - } - - let failed = print_lint_names("extra lint docs:", &extra) | print_lint_names("missing lint docs:", &lint_names); - - if failed { - exit_with_failure(); - } - } else { - if std::fs::remove_dir_all("src/docs").is_err() { - eprintln!("could not remove src/docs directory"); - } - if std::fs::create_dir("src/docs").is_err() { - eprintln!("could not recreate src/docs directory"); - } - } - for lint in usable_lints { - process_file( - Path::new("src/docs").join(lint.name.clone() + ".txt"), - update_mode, - &lint.documentation, - ); - } -} - -fn print_lint_names(header: &str, lints: &BTreeSet) -> bool { - if lints.is_empty() { - return false; - } - println!("{header}"); - for lint in lints.iter().sorted() { - println!(" {lint}"); - } - println!(); - true -} - pub fn print_lints() { let (lint_list, _, _) = gather_all(); let usable_lints = Lint::usable_lints(&lint_list); @@ -641,26 +619,17 @@ struct Lint { desc: String, module: String, declaration_range: Range, - documentation: String, } impl Lint { #[must_use] - fn new( - name: &str, - group: &str, - desc: &str, - module: &str, - declaration_range: Range, - documentation: String, - ) -> Self { + fn new(name: &str, group: &str, desc: &str, module: &str, declaration_range: Range) -> Self { Self { name: name.to_lowercase(), group: group.into(), desc: remove_line_splices(desc), module: module.into(), declaration_range, - documentation, } } @@ -716,25 +685,6 @@ impl RenamedLint { } } -/// Generates the code for registering a group -fn gen_lint_group_list<'a>(group_name: &str, lints: impl Iterator) -> 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(); - - let _ = writeln!( - output, - "store.register_group(true, \"clippy::{group_name}\", Some(\"clippy_{group_name}\"), vec![", - ); - for (module, name) in details { - let _ = writeln!(output, " LintId::of({module}::{name}),"); - } - output.push_str("])\n"); - - output -} - /// Generates the `register_removed` code #[must_use] fn gen_deprecated(lints: &[DeprecatedLint]) -> String { @@ -759,7 +709,7 @@ fn gen_deprecated(lints: &[DeprecatedLint]) -> String { /// Generates the code for registering lints #[must_use] -fn gen_register_lint_list<'a>( +fn gen_declared_lints<'a>( internal_lints: impl Iterator, usable_lints: impl Iterator, ) -> String { @@ -770,15 +720,15 @@ fn gen_register_lint_list<'a>( details.sort_unstable(); let mut output = GENERATED_FILE_COMMENT.to_string(); - output.push_str("store.register_lints(&[\n"); + output.push_str("pub(crate) static LINTS: &[&crate::LintInfo] = &[\n"); for (is_public, module_name, lint_name) in details { if !is_public { output.push_str(" #[cfg(feature = \"internal\")]\n"); } - let _ = writeln!(output, " {module_name}::{lint_name},"); + let _ = writeln!(output, " crate::{module_name}::{lint_name}_INFO,"); } - output.push_str("])\n"); + output.push_str("];\n"); output } @@ -910,35 +860,26 @@ fn parse_contents(contents: &str, module: &str, lints: &mut Vec) { }| token_kind == &TokenKind::Ident && *content == "declare_clippy_lint", ) { let start = range.start; - let mut docs = String::with_capacity(128); - let mut iter = iter.by_ref().filter(|t| !matches!(t.token_kind, TokenKind::Whitespace)); + let mut iter = iter + .by_ref() + .filter(|t| !matches!(t.token_kind, TokenKind::Whitespace | TokenKind::LineComment { .. })); // matches `!{` match_tokens!(iter, Bang OpenBrace); - let mut in_code = false; - while let Some(t) = iter.next() { - match t.token_kind { - TokenKind::LineComment { .. } => { - if let Some(line) = t.content.strip_prefix("/// ").or_else(|| t.content.strip_prefix("///")) { - if line.starts_with("```") { - docs += "```\n"; - in_code = !in_code; - } else if !(in_code && line.starts_with("# ")) { - docs += line; - docs.push('\n'); - } - } - }, - TokenKind::Pound => { - match_tokens!(iter, OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket Ident); - break; - }, - TokenKind::Ident => { - break; - }, - _ => {}, - } + match iter.next() { + // #[clippy::version = "version"] pub + Some(LintDeclSearchResult { + token_kind: TokenKind::Pound, + .. + }) => { + match_tokens!(iter, OpenBracket Ident Colon Colon Ident Eq Literal{..} CloseBracket Ident); + }, + // pub + Some(LintDeclSearchResult { + token_kind: TokenKind::Ident, + .. + }) => (), + _ => continue, } - docs.pop(); // remove final newline let (name, group, desc) = match_tokens!( iter, @@ -956,7 +897,7 @@ fn parse_contents(contents: &str, module: &str, lints: &mut Vec) { .. }) = iter.next() { - lints.push(Lint::new(name, group, desc, module, start..range.end, docs)); + lints.push(Lint::new(name, group, desc, module, start..range.end)); } } } @@ -1186,7 +1127,6 @@ mod tests { "\"really long text\"", "module_name", Range::default(), - String::new(), ), Lint::new( "doc_markdown", @@ -1194,7 +1134,6 @@ mod tests { "\"single line\"", "module_name", Range::default(), - String::new(), ), ]; assert_eq!(expected, result); @@ -1234,7 +1173,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), ), Lint::new( "should_assert_eq2", @@ -1242,7 +1180,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), ), Lint::new( "should_assert_eq2", @@ -1250,7 +1187,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), ), ]; let expected = vec![Lint::new( @@ -1259,7 +1195,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), )]; assert_eq!(expected, Lint::usable_lints(&lints)); } @@ -1267,51 +1202,22 @@ mod tests { #[test] fn test_by_lint_group() { let lints = vec![ - Lint::new( - "should_assert_eq", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), + Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name", Range::default()), Lint::new( "should_assert_eq2", "group2", "\"abc\"", "module_name", Range::default(), - String::new(), - ), - Lint::new( - "incorrect_match", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), ), + Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()), ]; let mut expected: HashMap> = HashMap::new(); expected.insert( "group1".to_string(), vec![ - Lint::new( - "should_assert_eq", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), - Lint::new( - "incorrect_match", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), + Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name", Range::default()), + Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()), ], ); expected.insert( @@ -1322,7 +1228,6 @@ mod tests { "\"abc\"", "module_name", Range::default(), - String::new(), )], ); assert_eq!(expected, Lint::by_lint_group(lints.into_iter())); @@ -1357,48 +1262,4 @@ mod tests { assert_eq!(expected, gen_deprecated(&lints)); } - - #[test] - fn test_gen_lint_group_list() { - let lints = vec![ - Lint::new( - "abc", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), - Lint::new( - "should_assert_eq", - "group1", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), - Lint::new( - "internal", - "internal_style", - "\"abc\"", - "module_name", - Range::default(), - String::new(), - ), - ]; - 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); - } } -- cgit v1.2.3