fix clippy warnings (#139)

This commit is contained in:
Jonas Chevalier 2021-12-11 22:43:02 +01:00 committed by GitHub
parent 874d65223c
commit e7def7c29e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 36 additions and 27 deletions

View File

@ -6,13 +6,13 @@
config = { };
overlays = [ ];
}
, rustPackages ? nixpkgs.rustPackages
}:
let
cargoToml = with builtins; (fromTOML (readFile ./Cargo.toml));
in
{
# What is used when invoking `nix run github:numtide/treefmt`
treefmt = nixpkgs.pkgs.rustPlatform.buildRustPackage {
treefmt = rustPackages.rustPlatform.buildRustPackage {
inherit (cargoToml.package) name version;
src = nixpkgs.lib.cleanSource ./.;
@ -20,6 +20,7 @@ in
# Those are being used in tests
nativeBuildInputs = with nixpkgs; [
# Build tools
rustPackages.clippy
rust-analyzer
# Code formatters
@ -45,7 +46,14 @@ in
meta.description = "one CLI to format the code tree";
};
in
{
inherit treefmt;
# A collection of packages for the project
docs = nixpkgs.callPackage ./docs { };
# Flake attributes
defaultPackage = treefmt;
devShell = treefmt;
}

View File

@ -8,15 +8,16 @@
outputs = { self, nixpkgs, flake-utils }@inputs:
flake-utils.lib.eachSystem [ "x86_64-linux" "x86_64-darwin" ] (system:
let
pkgs = import ./. {
inherit system inputs;
pkgs = import self {
inherit system;
inputs = null;
nixpkgs = nixpkgs.legacyPackages.${system};
};
in
{
defaultPackage = pkgs.treefmt;
packages = pkgs;
devShell = pkgs.treefmt;
defaultPackage = pkgs.defaultPackage;
legacyPackages = pkgs;
devShell = pkgs.devShell;
}
);
}

View File

@ -8,7 +8,7 @@ use std::path::{Path, PathBuf};
pub fn format_cmd(
tree_root: &Option<PathBuf>,
work_dir: &Path,
config_file: &PathBuf,
config_file: &Path,
paths: &[PathBuf],
clear_cache: bool,
fail_on_change: bool,
@ -25,7 +25,7 @@ pub fn format_cmd(
// Default the tree root to the folder that contains the config file
let tree_root = tree_root.clone().unwrap_or_else(|| {
// unwrap: since the config_file is a file, there MUST be a parent folder.
config_file.clone().parent().unwrap().to_path_buf()
config_file.parent().unwrap().to_path_buf()
});
// Default to the tree root if no paths have been given
@ -51,9 +51,9 @@ pub fn format_cmd(
// Finally run the main formatter logic from the engine.
run_treefmt(
&tree_root,
&work_dir,
work_dir,
&cache_dir,
&config_file,
config_file,
&paths,
clear_cache,
fail_on_change,

View File

@ -22,7 +22,7 @@ pub fn format_stdin_cmd(
};
// Search for the treefmt.toml from there.
let config_file = match config::lookup(&work_dir) {
let config_file = match config::lookup(work_dir) {
Some(path) => path,
None => {
return Err(anyhow!(
@ -62,7 +62,7 @@ pub fn format_stdin_cmd(
);
// Finally run the main formatter logic from the engine.
run_treefmt_stdin(&tree_root, &work_dir, &cache_dir, &config_file, &path)?;
run_treefmt_stdin(&tree_root, work_dir, &cache_dir, &config_file, &path)?;
Ok(())
}

View File

@ -118,7 +118,7 @@ pub fn run_cli(cli: &Cli) -> anyhow::Result<()> {
format_cmd(
&cli.tree_root,
&cli.work_dir,
&cli.config_file
cli.config_file
.as_ref()
.expect("presence asserted in ::cli_from_args"),
&cli.paths,

View File

@ -71,7 +71,7 @@ pub fn run_treefmt(
}
// Load the treefmt.toml file
let project_config = config::from_path(&treefmt_toml)?;
let project_config = config::from_path(treefmt_toml)?;
let global_excludes = project_config
.global
@ -85,7 +85,7 @@ pub fn run_treefmt(
BTreeMap::new(),
|mut sum, (name, mut fmt_config)| {
fmt_config.excludes.extend_from_slice(&global_excludes);
match Formatter::from_config(&tree_root, &name, &fmt_config) {
match Formatter::from_config(tree_root, &name, &fmt_config) {
Ok(fmt_matcher) => {
sum.insert(fmt_matcher.name.clone(), fmt_matcher);
}
@ -102,7 +102,7 @@ pub fn run_treefmt(
// Start with an empty cache
CacheManifest::default()
} else {
CacheManifest::load(&cache_dir, &treefmt_toml)
CacheManifest::load(cache_dir, treefmt_toml)
};
timed_debug("load cache");
// Insert the new formatter configs
@ -181,7 +181,7 @@ pub fn run_treefmt(
.map(|(formatter_name, path_mtime)| {
let paths: Vec<PathBuf> = path_mtime.keys().cloned().collect();
// unwrap: the key exists since matches was built from that previous collection
let formatter = formatters.get(&formatter_name).unwrap();
let formatter = formatters.get(formatter_name).unwrap();
// Don't run the formatter if there are no paths to format!
if paths.is_empty() {
@ -191,7 +191,7 @@ pub fn run_treefmt(
// Run the formatter
for path_chunks in paths.chunks(1024) {
formatter.clone().fmt(&path_chunks)?;
formatter.clone().fmt(path_chunks)?;
}
// Get the new mtimes and compare them to the original ones
@ -340,7 +340,7 @@ pub fn run_treefmt_stdin(
};
// Load the treefmt.toml file
let project_config = config::from_path(&treefmt_toml)?;
let project_config = config::from_path(treefmt_toml)?;
let global_excludes = project_config
.global
@ -352,7 +352,7 @@ pub fn run_treefmt_stdin(
BTreeMap::new(),
|mut sum, (name, mut fmt_config)| {
fmt_config.excludes.extend_from_slice(&global_excludes);
match Formatter::from_config(&tree_root, &name, &fmt_config) {
match Formatter::from_config(tree_root, &name, &fmt_config) {
Ok(fmt_matcher) => {
sum.insert(fmt_matcher.name.clone(), fmt_matcher);
}

View File

@ -127,7 +127,7 @@ impl CacheManifest {
// Now discard all the paths who don't have an associated formatter
for name in old_formatters.keys() {
self.matches.remove(&name);
self.matches.remove(name);
}
}

View File

@ -153,9 +153,9 @@ impl Formatter {
pub fn from_config(tree_root: &Path, name: &str, cfg: &FmtConfig) -> Result<Self> {
let name = FormatterName(name.to_string());
// Expand the work_dir to an absolute path, using the project root as a reference.
let work_dir = expand_path(&cfg.work_dir, &tree_root);
let work_dir = expand_path(&cfg.work_dir, tree_root);
// Resolve the path to the binary
let command = expand_exe(&cfg.command, &tree_root)?;
let command = expand_exe(&cfg.command, tree_root)?;
debug!("Found {} at {}", cfg.command, command.display());
assert!(command.is_absolute());
@ -188,7 +188,7 @@ impl fmt::Display for Formatter {
fn patterns_to_glob_set(tree_root: &Path, patterns: &[String]) -> Result<GlobSet> {
let mut sum = GlobSetBuilder::new();
for pattern in patterns {
let pattern = expand_if_path(pattern.to_string(), &tree_root);
let pattern = expand_if_path(pattern.to_string(), tree_root);
let glob = GlobBuilder::new(&pattern).build()?;
sum.add(glob);
}

View File

@ -41,7 +41,7 @@ pub fn get_meta_mtime(metadata: &Metadata) -> Mtime {
}
/// Resolve the command into an absolute path.
fn expand_exe(command: &String, reference: &Path) -> Result<PathBuf> {
fn expand_exe(command: &str, reference: &Path) -> Result<PathBuf> {
Ok(which_in(command, env::var_os("PATH"), reference)?.clean())
}