Skip to content

Commit

Permalink
Move Dylint into an opts module
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Feb 9, 2024
1 parent c3b9420 commit 83e5eb0
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 128 deletions.
14 changes: 7 additions & 7 deletions cargo-dylint/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ struct NameOpts {
}

#[allow(deprecated)]
impl From<Dylint> for dylint::Dylint {
impl From<Dylint> for dylint::opts::Dylint {
fn from(opts: Dylint) -> Self {
let opts = process_deprecated_options(opts);
let Dylint {
Expand Down Expand Up @@ -339,25 +339,25 @@ impl From<Dylint> for dylint::Dylint {
fn process_deprecated_options(mut opts: Dylint) -> Dylint {
if opts.list {
dylint::__warn(
&dylint::Dylint::default(),
&dylint::opts::Dylint::default(),
"`--list` is deprecated. Use subcommand `list`.",
);
}
if opts.new_path.is_some() {
dylint::__warn(
&dylint::Dylint::default(),
&dylint::opts::Dylint::default(),
"`--new` is deprecated. Use subcommand `new`.",
);
}
if opts.upgrade_path.is_some() {
dylint::__warn(
&dylint::Dylint::default(),
&dylint::opts::Dylint::default(),
"`--upgrade` is deprecated. Use subcommand `upgrade`.",
);
}
if !opts.names.is_empty() {
dylint::__warn(
&dylint::Dylint::default(),
&dylint::opts::Dylint::default(),
"Referring to libraries by bare name is deprecated. Use `--lib` or `--lib-path`.",
);
}
Expand Down Expand Up @@ -434,7 +434,7 @@ impl NameOpts {
fn main() -> dylint::ColorizedResult<()> {
env_logger::try_init().unwrap_or_else(|error| {
dylint::__warn(
&dylint::Dylint::default(),
&dylint::opts::Dylint::default(),
&format!("`env_logger` already initialized: {error}"),
);
});
Expand All @@ -446,7 +446,7 @@ fn main() -> dylint::ColorizedResult<()> {

fn cargo_dylint<T: AsRef<OsStr>>(args: &[T]) -> dylint::ColorizedResult<()> {
match Opts::parse_from(args).subcmd {
CargoSubcommand::Dylint(opts) => dylint::run(&dylint::Dylint::from(opts)),
CargoSubcommand::Dylint(opts) => dylint::run(&dylint::opts::Dylint::from(opts)),
}
.map_err(dylint::ColorizedError::new)
}
Expand Down
2 changes: 1 addition & 1 deletion cargo-dylint/tests/nightly_toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn nightly_toolchain() {
update_nightly().unwrap();

#[allow(let_underscore_drop)]
let _ = dylint::driver_builder::get(&dylint::Dylint::default(), "nightly").unwrap();
let _ = dylint::driver_builder::get(&dylint::opts::Dylint::default(), "nightly").unwrap();
}

fn update_nightly() -> Result<()> {
Expand Down
10 changes: 5 additions & 5 deletions dylint/src/driver_builder.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::warn;
use crate::{error::warn, opts};
use anyhow::{anyhow, ensure, Context, Result};
use cargo_metadata::MetadataCommand;
use dylint_internal::{
Expand Down Expand Up @@ -68,7 +68,7 @@ pub fn main() -> Result<()> {
dylint_lib = "question_mark_in_expression",
allow(question_mark_in_expression)
)]
pub fn get(opts: &crate::Dylint, toolchain: &str) -> Result<PathBuf> {
pub fn get(opts: &opts::Dylint, toolchain: &str) -> Result<PathBuf> {
let dylint_drivers = dylint_drivers()?;

let driver_dir = dylint_drivers.join(toolchain);
Expand Down Expand Up @@ -113,7 +113,7 @@ fn dylint_drivers() -> Result<PathBuf> {
}
}

fn is_outdated(opts: &crate::Dylint, toolchain: &str, driver: &Path) -> Result<bool> {
fn is_outdated(opts: &opts::Dylint, toolchain: &str, driver: &Path) -> Result<bool> {
(|| -> Result<bool> {
let mut command = dylint_driver(toolchain, driver)?;
let output = command.args(["-V"]).logged_output(true)?;
Expand All @@ -138,7 +138,7 @@ fn is_outdated(opts: &crate::Dylint, toolchain: &str, driver: &Path) -> Result<b
}

#[cfg_attr(dylint_lib = "supplementary", allow(commented_code))]
fn build(opts: &crate::Dylint, toolchain: &str, driver: &Path) -> Result<()> {
fn build(opts: &opts::Dylint, toolchain: &str, driver: &Path) -> Result<()> {
let tempdir = tempdir().with_context(|| "`tempdir` failed")?;
let package = tempdir.path();

Expand Down Expand Up @@ -231,7 +231,7 @@ mod test {
fn nightly() {
let tempdir = tempdir().unwrap();
build(
&crate::Dylint::default(),
&opts::Dylint::default(),
"nightly",
&tempdir.path().join("dylint-driver"),
)
Expand Down
2 changes: 1 addition & 1 deletion dylint/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ where
pub type ColorizedResult<T> = Result<T, ColorizedError<anyhow::Error>>;

#[allow(clippy::expect_used)]
pub fn warn(opts: &crate::Dylint, message: &str) {
pub fn warn(opts: &crate::opts::Dylint, message: &str) {
if !opts.quiet {
// smoelius: Writing directly to `stderr` prevents capture by `libtest`.
std::io::stderr()
Expand Down
112 changes: 18 additions & 94 deletions dylint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use std::{
collections::BTreeMap,
env::{consts, current_dir},
ffi::OsStr,
fmt::Debug,
fs::{metadata, OpenOptions},
path::{Path, PathBuf, MAIN_SEPARATOR},
};
Expand All @@ -40,6 +39,8 @@ use name_toolchain_map::{LazyToolchainMap, MaybeLibrary};
#[cfg(__library_packages)]
pub(crate) mod library_packages;

pub mod opts;

#[cfg(feature = "package_options")]
mod package_options;

Expand All @@ -51,87 +52,7 @@ static REQUIRED_FORM: Lazy<String> = Lazy::new(|| {
)
});

#[allow(clippy::struct_excessive_bools)]
#[derive(Clone, Debug, Default)]
// smoelius: Please keep the fields `names` and `args` last. Please keep all other fields sorted.
pub struct Dylint {
pub all: bool,

#[deprecated]
pub allow_downgrade: bool,

#[deprecated]
pub bisect: bool,

pub branch: Option<String>,

pub fix: bool,

#[deprecated]
pub force: bool,

pub git: Option<String>,

#[deprecated]
pub isolate: bool,

pub keep_going: bool,

pub lib_paths: Vec<String>,

pub libs: Vec<String>,

#[deprecated]
pub list: bool,

pub manifest_path: Option<String>,

#[deprecated]
pub new_path: Option<String>,

pub no_build: bool,

pub no_deps: bool,

pub no_metadata: bool,

pub packages: Vec<String>,

pub paths: Vec<String>,

pub pattern: Option<String>,

pub pipe_stderr: Option<String>,

pub pipe_stdout: Option<String>,

pub quiet: bool,

pub rev: Option<String>,

#[deprecated]
pub rust_version: Option<String>,

pub tag: Option<String>,

#[deprecated]
pub upgrade_path: Option<String>,

pub workspace: bool,

#[deprecated]
pub names: Vec<String>,

pub args: Vec<String>,
}

impl Dylint {
fn git_or_path(&self) -> bool {
self.git.is_some() || !self.paths.is_empty()
}
}

pub fn run(opts: &Dylint) -> Result<()> {
pub fn run(opts: &opts::Dylint) -> Result<()> {
let opts = {
let mut opts = opts.clone();

Expand Down Expand Up @@ -219,7 +140,10 @@ pub fn run(opts: &Dylint) -> Result<()> {
run_with_name_toolchain_map(&opts, &name_toolchain_map)
}

fn run_with_name_toolchain_map(opts: &Dylint, name_toolchain_map: &NameToolchainMap) -> Result<()> {
fn run_with_name_toolchain_map(
opts: &opts::Dylint,
name_toolchain_map: &NameToolchainMap,
) -> Result<()> {
if opts.libs.is_empty() && opts.lib_paths.is_empty() && opts.names.is_empty() && !opts.all {
if opts.list {
warn_if_empty(opts, name_toolchain_map)?;
Expand Down Expand Up @@ -251,7 +175,7 @@ fn run_with_name_toolchain_map(opts: &Dylint, name_toolchain_map: &NameToolchain
}
}

fn warn_if_empty(opts: &Dylint, name_toolchain_map: &NameToolchainMap) -> Result<bool> {
fn warn_if_empty(opts: &opts::Dylint, name_toolchain_map: &NameToolchainMap) -> Result<bool> {
let name_toolchain_map = name_toolchain_map.get_or_try_init()?;

Ok(if name_toolchain_map.is_empty() {
Expand Down Expand Up @@ -294,7 +218,7 @@ fn list_libs(name_toolchain_map: &NameToolchainMap) -> Result<()> {
dylint_lib = "question_mark_in_expression",
allow(question_mark_in_expression)
)]
fn resolve(opts: &Dylint, name_toolchain_map: &NameToolchainMap) -> Result<ToolchainMap> {
fn resolve(opts: &opts::Dylint, name_toolchain_map: &NameToolchainMap) -> Result<ToolchainMap> {
let mut toolchain_map = ToolchainMap::new();

if opts.all {
Expand Down Expand Up @@ -450,7 +374,7 @@ fn name_as_path(name: &str, as_path_only: bool) -> Result<Option<(String, PathBu
Ok(None)
}

fn list_lints(opts: &Dylint, resolved: &ToolchainMap) -> Result<()> {
fn list_lints(opts: &opts::Dylint, resolved: &ToolchainMap) -> Result<()> {
for (toolchain, paths) in resolved {
for path in paths {
let driver = driver_builder::get(opts, toolchain)?;
Expand Down Expand Up @@ -501,7 +425,7 @@ fn display_location(path: &Path) -> Result<String> {
.to_string())
}

fn check_or_fix(opts: &Dylint, resolved: &ToolchainMap) -> Result<()> {
fn check_or_fix(opts: &opts::Dylint, resolved: &ToolchainMap) -> Result<()> {
let clippy_disable_docs_links = clippy_disable_docs_links()?;

let mut failures = Vec::new();
Expand Down Expand Up @@ -599,7 +523,7 @@ fn check_or_fix(opts: &Dylint, resolved: &ToolchainMap) -> Result<()> {
}
}

fn target_dir(opts: &Dylint, toolchain: &str) -> Result<PathBuf> {
fn target_dir(opts: &opts::Dylint, toolchain: &str) -> Result<PathBuf> {
let mut command = MetadataCommand::new();
if let Some(path) = &opts.manifest_path {
command.manifest_path(path);
Expand Down Expand Up @@ -634,9 +558,9 @@ mod test {
// The easiest solution is to just not run the tests concurrently.
static MUTEX: Mutex<()> = Mutex::new(());

static OPTS: Lazy<Dylint> = Lazy::new(|| Dylint {
static OPTS: Lazy<opts::Dylint> = Lazy::new(|| opts::Dylint {
no_metadata: true,
..Dylint::default()
..opts::Dylint::default()
});

fn name_toolchain_map() -> NameToolchainMap<'static> {
Expand Down Expand Up @@ -681,12 +605,12 @@ mod test {
straggler.keys().collect::<Vec<_>>()
);

let opts = Dylint {
let opts = opts::Dylint {
libs: vec![
"question_mark_in_expression".to_owned(),
"straggler".to_owned(),
],
..Dylint::default()
..opts::Dylint::default()
};

run_with_name_toolchain_map(&opts, &name_toolchain_map).unwrap();
Expand Down Expand Up @@ -727,12 +651,12 @@ mod test {
question_mark_in_expression.keys().collect::<Vec<_>>()
);

let opts = Dylint {
let opts = opts::Dylint {
libs: vec![
"clippy".to_owned(),
"question_mark_in_expression".to_owned(),
],
..Dylint::default()
..opts::Dylint::default()
};

run_with_name_toolchain_map(&opts, &name_toolchain_map).unwrap();
Expand Down
3 changes: 2 additions & 1 deletion dylint/src/library_packages/cargo_cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//!
//! [Marker]: https://github.com/rust-marker/marker

use crate::opts;
use anyhow::{anyhow, bail, ensure, Context, Result};
use cargo_metadata::{Metadata, MetadataCommand};
use dylint_internal::{packaging::isolate, CommandExt};
Expand Down Expand Up @@ -82,7 +83,7 @@ impl PackageId {
}

pub fn dependency_source_id_and_root(
_opts: &crate::Dylint,
_opts: &opts::Dylint,
metadata: &Metadata,
_config: &Config,
details: &DetailedTomlDependency,
Expand Down
4 changes: 2 additions & 2 deletions dylint/src/library_packages/cargo_lib/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::error::warn;
use crate::{error::warn, opts};
use anyhow::{anyhow, bail, ensure, Result};
use cargo::{
core::{Dependency, Features, Package as CargoPackage},
Expand All @@ -15,7 +15,7 @@ mod toml;
pub use self::toml::DetailedTomlDependency;

pub fn dependency_source_id_and_root(
opts: &crate::Dylint,
opts: &opts::Dylint,
metadata: &Metadata,
config: &Config,
details: &DetailedTomlDependency,
Expand Down
Loading

0 comments on commit 83e5eb0

Please sign in to comment.