mirror of
https://github.com/Noratrieb/cargo-minimize.git
synced 2026-01-14 08:25:01 +01:00
more
This commit is contained in:
parent
9243224163
commit
02b8eaa7b2
5 changed files with 30 additions and 9 deletions
|
|
@ -1,3 +1,5 @@
|
||||||
|
#![feature(non_exhaustive_omitted_patterns_lint)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate tracing;
|
extern crate tracing;
|
||||||
|
|
||||||
|
|
@ -32,7 +34,7 @@ pub enum Cargo {
|
||||||
Minimize(Options),
|
Minimize(Options),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(clap::Args, Debug)]
|
#[derive(clap::Args, Debug, Clone)]
|
||||||
pub struct Options {
|
pub struct Options {
|
||||||
/// Additional arguments to pass to cargo/rustc, separated by whitespace.
|
/// Additional arguments to pass to cargo/rustc, separated by whitespace.
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
|
|
@ -99,6 +101,9 @@ pub struct Options {
|
||||||
/// Do not touch the following files.
|
/// Do not touch the following files.
|
||||||
#[arg(long)]
|
#[arg(long)]
|
||||||
pub ignore_file: Vec<PathBuf>,
|
pub ignore_file: Vec<PathBuf>,
|
||||||
|
|
||||||
|
#[arg(skip)]
|
||||||
|
pub no_delete_functions: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
|
@ -177,6 +182,7 @@ impl Default for Options {
|
||||||
script_path: None,
|
script_path: None,
|
||||||
script_path_lints: None,
|
script_path_lints: None,
|
||||||
ignore_file: Vec::new(),
|
ignore_file: Vec::new(),
|
||||||
|
no_delete_functions: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
use quote::ToTokens;
|
use quote::ToTokens;
|
||||||
use syn::{
|
use syn::{
|
||||||
visit_mut::VisitMut, Item, ItemConst, ItemEnum, ItemMacro, ItemMacro2, ItemMod, ItemStatic,
|
visit_mut::VisitMut, Item, ItemConst, ItemEnum, ItemExternCrate, ItemFn, ItemMacro, ItemMacro2,
|
||||||
ItemStruct, ItemTrait, ItemType, ItemUnion,
|
ItemMod, ItemStatic, ItemStruct, ItemTrait, ItemTraitAlias, ItemType, ItemUnion, Signature,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::processor::{tracking, Pass, PassController, ProcessState, SourceFile};
|
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 {
|
fn consider_deleting_item(&mut self, item: &Item) -> bool {
|
||||||
match item {
|
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_) => {
|
Item::Impl(impl_) => {
|
||||||
self.current_path
|
self.current_path
|
||||||
.push(impl_.self_ty.clone().into_token_stream().to_string());
|
.push(impl_.self_ty.clone().into_token_stream().to_string());
|
||||||
|
|
@ -42,18 +40,25 @@ impl<'a> Visitor<'a> {
|
||||||
self.current_path.pop();
|
self.current_path.pop();
|
||||||
should_retain
|
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::Enum(ItemEnum { ident, .. })
|
||||||
| Item::Union(ItemUnion { ident, .. })
|
| Item::Union(ItemUnion { ident, .. })
|
||||||
| Item::Const(ItemConst { ident, .. })
|
| Item::Const(ItemConst { ident, .. })
|
||||||
| Item::Type(ItemType { ident, .. })
|
| Item::Type(ItemType { ident, .. })
|
||||||
| Item::Trait(ItemTrait { ident, .. })
|
| Item::Trait(ItemTrait { ident, .. })
|
||||||
|
| Item::TraitAlias(ItemTraitAlias { ident, .. })
|
||||||
| Item::Macro(ItemMacro {
|
| Item::Macro(ItemMacro {
|
||||||
ident: Some(ident), ..
|
ident: Some(ident), ..
|
||||||
})
|
})
|
||||||
| Item::Macro2(ItemMacro2 { ident, .. })
|
| Item::Macro2(ItemMacro2 { ident, .. })
|
||||||
| Item::Static(ItemStatic { ident, .. })
|
| Item::Static(ItemStatic { ident, .. })
|
||||||
| Item::Mod(ItemMod { ident, .. }) => {
|
| Item::Mod(ItemMod { ident, .. })
|
||||||
|
| Item::ExternCrate(ItemExternCrate { ident, .. }) => {
|
||||||
self.current_path.push(ident.to_string());
|
self.current_path.push(ident.to_string());
|
||||||
|
|
||||||
let should_retain = self.should_retain_item();
|
let should_retain = self.should_retain_item();
|
||||||
|
|
@ -61,6 +66,11 @@ impl<'a> Visitor<'a> {
|
||||||
self.current_path.pop();
|
self.current_path.pop();
|
||||||
should_retain
|
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,
|
_ => true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
use std::{borrow::Borrow, collections::BTreeSet, fmt::Debug, mem};
|
use std::{borrow::Borrow, collections::BTreeSet, fmt::Debug, mem};
|
||||||
|
|
||||||
|
use crate::Options;
|
||||||
|
|
||||||
use self::worklist::Worklist;
|
use self::worklist::Worklist;
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
||||||
|
|
@ -20,6 +22,7 @@ impl Debug for AstPath {
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub(crate) struct PassController {
|
pub(crate) struct PassController {
|
||||||
state: PassControllerState,
|
state: PassControllerState,
|
||||||
|
pub(crate) options: Options,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
@ -86,11 +89,12 @@ fn split_owned<T, From: IntoIterator<Item = T>, A: FromIterator<T>, B: FromItera
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PassController {
|
impl PassController {
|
||||||
pub fn new() -> Self {
|
pub fn new(options: Options) -> Self {
|
||||||
Self {
|
Self {
|
||||||
state: PassControllerState::InitialCollection {
|
state: PassControllerState::InitialCollection {
|
||||||
candidates: Vec::new(),
|
candidates: Vec::new(),
|
||||||
},
|
},
|
||||||
|
options,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ impl Minimizer {
|
||||||
invalidated_files: &mut HashSet<&'file SourceFile>,
|
invalidated_files: &mut HashSet<&'file SourceFile>,
|
||||||
changes: &mut Changes,
|
changes: &mut Changes,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut checker = PassController::new();
|
let mut checker = PassController::new(self.options.clone());
|
||||||
loop {
|
loop {
|
||||||
let file_display = file.path.display();
|
let file_display = file.path.display();
|
||||||
let mut change = file.try_change(changes)?;
|
let mut change = file.try_change(changes)?;
|
||||||
|
|
|
||||||
|
|
@ -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.project_dir = Some(cargo_dir);
|
||||||
opts.path = path;
|
opts.path = path;
|
||||||
|
opts.no_delete_functions = true;
|
||||||
options(&mut opts);
|
options(&mut opts);
|
||||||
|
|
||||||
cargo_minimize::minimize(opts, Arc::new(AtomicBool::new(false)))?;
|
cargo_minimize::minimize(opts, Arc::new(AtomicBool::new(false)))?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue