From 02b8eaa7b2e1fb697c95cc16cfb55e5a1a077ede Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Sat, 1 Apr 2023 15:58:00 +0200 Subject: [PATCH] more --- src/lib.rs | 8 +++++++- src/passes/item_deleter.rs | 22 ++++++++++++++++------ src/processor/checker.rs | 6 +++++- src/processor/mod.rs | 2 +- tests/helper.rs | 1 + 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index d36e2d4..4dd3648 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,5 @@ +#![feature(non_exhaustive_omitted_patterns_lint)] + #[macro_use] extern crate tracing; @@ -32,7 +34,7 @@ pub enum Cargo { Minimize(Options), } -#[derive(clap::Args, Debug)] +#[derive(clap::Args, Debug, Clone)] pub struct Options { /// Additional arguments to pass to cargo/rustc, separated by whitespace. #[arg(long)] @@ -99,6 +101,9 @@ pub struct Options { /// Do not touch the following files. #[arg(long)] pub ignore_file: Vec, + + #[arg(skip)] + pub no_delete_functions: bool, } #[derive(Debug, Clone)] @@ -177,6 +182,7 @@ impl Default for Options { script_path: None, script_path_lints: None, ignore_file: Vec::new(), + no_delete_functions: false, } } } diff --git a/src/passes/item_deleter.rs b/src/passes/item_deleter.rs index 7fac7ec..18f1335 100644 --- a/src/passes/item_deleter.rs +++ b/src/passes/item_deleter.rs @@ -1,7 +1,7 @@ use quote::ToTokens; use syn::{ - visit_mut::VisitMut, Item, ItemConst, ItemEnum, ItemMacro, ItemMacro2, ItemMod, ItemStatic, - ItemStruct, ItemTrait, ItemType, ItemUnion, + visit_mut::VisitMut, Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn, ItemMacro, ItemMacro2, + ItemMod, ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, ItemUnion, Signature, }; use crate::processor::{tracking, Pass, PassController, ProcessState, SourceFile}; @@ -31,8 +31,6 @@ impl<'a> Visitor<'a> { fn consider_deleting_item(&mut self, item: &Item) -> bool { match item { - // N.B. Do not delete ItemFn because that makes testing way harder - // and also the dead_lint should cover it all. Item::Impl(impl_) => { self.current_path .push(impl_.self_ty.clone().into_token_stream().to_string()); @@ -42,18 +40,25 @@ impl<'a> Visitor<'a> { self.current_path.pop(); should_retain } - Item::Struct(ItemStruct { ident, .. }) + Item::Fn(_) if self.checker.options.no_delete_functions => true, + Item::Fn(ItemFn { + sig: Signature { ident, .. }, + .. + }) + | Item::Struct(ItemStruct { ident, .. }) | Item::Enum(ItemEnum { ident, .. }) | Item::Union(ItemUnion { ident, .. }) | Item::Const(ItemConst { ident, .. }) | Item::Type(ItemType { ident, .. }) | Item::Trait(ItemTrait { ident, .. }) + | Item::TraitAlias(ItemTraitAlias { ident, .. }) | Item::Macro(ItemMacro { ident: Some(ident), .. }) | Item::Macro2(ItemMacro2 { ident, .. }) | Item::Static(ItemStatic { ident, .. }) - | Item::Mod(ItemMod { ident, .. }) => { + | Item::Mod(ItemMod { ident, .. }) + | Item::ExternCrate(ItemExternCrate { ident, .. }) => { self.current_path.push(ident.to_string()); let should_retain = self.should_retain_item(); @@ -61,6 +66,11 @@ impl<'a> Visitor<'a> { self.current_path.pop(); should_retain } + Item::ForeignMod(_) => true, + // We hope for the unused imports to show them all. + Item::Use(_) => true, + Item::Verbatim(_) => true, + #[deny(non_exhaustive_omitted_patterns)] _ => true, } } diff --git a/src/processor/checker.rs b/src/processor/checker.rs index 4e8e8bc..40695bb 100644 --- a/src/processor/checker.rs +++ b/src/processor/checker.rs @@ -1,5 +1,7 @@ use std::{borrow::Borrow, collections::BTreeSet, fmt::Debug, mem}; +use crate::Options; + use self::worklist::Worklist; #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] @@ -20,6 +22,7 @@ impl Debug for AstPath { #[derive(Debug)] pub(crate) struct PassController { state: PassControllerState, + pub(crate) options: Options, } #[derive(Debug)] @@ -86,11 +89,12 @@ fn split_owned, A: FromIterator, B: FromItera } impl PassController { - pub fn new() -> Self { + pub fn new(options: Options) -> Self { Self { state: PassControllerState::InitialCollection { candidates: Vec::new(), }, + options, } } diff --git a/src/processor/mod.rs b/src/processor/mod.rs index c438029..eeee8c9 100644 --- a/src/processor/mod.rs +++ b/src/processor/mod.rs @@ -181,7 +181,7 @@ impl Minimizer { invalidated_files: &mut HashSet<&'file SourceFile>, changes: &mut Changes, ) -> Result<()> { - let mut checker = PassController::new(); + let mut checker = PassController::new(self.options.clone()); loop { let file_display = file.path.display(); let mut change = file.try_change(changes)?; diff --git a/tests/helper.rs b/tests/helper.rs index 878dd16..6785bba 100644 --- a/tests/helper.rs +++ b/tests/helper.rs @@ -52,6 +52,7 @@ pub fn run_test(code: &str, minimizes_to: &str, options: impl FnOnce(&mut Option opts.project_dir = Some(cargo_dir); opts.path = path; + opts.no_delete_functions = true; options(&mut opts); cargo_minimize::minimize(opts, Arc::new(AtomicBool::new(false)))?;