Skip to content

Commit

Permalink
Refactor: fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mstallmo committed Dec 29, 2018
1 parent 9675464 commit 4870b00
Show file tree
Hide file tree
Showing 14 changed files with 43 additions and 42 deletions.
1 change: 1 addition & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
too-many-arguments-threshold = 8
13 changes: 5 additions & 8 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum OutputFragment {
/// given sender.
fn read_and_send<R, F>(
mut reader: R,
sender: mpsc::Sender<OutputFragment>,
sender: &mpsc::Sender<OutputFragment>,
mut map: F,
) -> io::Result<()>
where
Expand Down Expand Up @@ -86,10 +86,7 @@ where
.rev()
.find(|(_, ch)| *ch == b'\n')
{
let next_in_progress: Vec<u8> = self.in_progress[last_newline + 1..]
.iter()
.cloned()
.collect();
let next_in_progress: Vec<u8> = self.in_progress[last_newline + 1..].to_vec();
let mut these_lines = mem::replace(&mut self.in_progress, next_in_progress);
these_lines.truncate(last_newline + 1);
let these_lines = String::from_utf8(these_lines)?;
Expand Down Expand Up @@ -139,9 +136,9 @@ pub fn run(
// waiting on the child process.

let stdout_handle =
thread::spawn(move || read_and_send(stdout, stdout_send, OutputFragment::Stdout));
thread::spawn(move || read_and_send(stdout, &stdout_send, OutputFragment::Stdout));
let stderr_handle =
thread::spawn(move || read_and_send(stderr, stderr_send, OutputFragment::Stderr));
thread::spawn(move || read_and_send(stderr, &stderr_send, OutputFragment::Stderr));

let mut stdout = OutputAccumulator::new(|line| {
info!(logger, "{} (stdout): {}", command_name, line);
Expand All @@ -168,7 +165,7 @@ pub fn run(

let exit = child.wait()?;
if exit.success() {
return Ok(stdout);
Ok(stdout)
} else {
drop((stdout, stderr));
bail!("failed to execute `{}`: exited with {}", command_name, exit)
Expand Down
4 changes: 2 additions & 2 deletions src/command/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl Build {

/// Execute this `Build` command.
pub fn run(&mut self, log: &Logger) -> Result<(), Error> {
let process_steps = Build::get_process_steps(&self.mode);
let process_steps = Build::get_process_steps(self.mode);

let mut step_counter = Step::new(process_steps.len());

Expand Down Expand Up @@ -208,7 +208,7 @@ impl Build {
Ok(())
}

fn get_process_steps(mode: &BuildMode) -> Vec<(&'static str, BuildStep)> {
fn get_process_steps(mode: BuildMode) -> Vec<(&'static str, BuildStep)> {
macro_rules! steps {
($($name:ident),+) => {
{
Expand Down
8 changes: 4 additions & 4 deletions src/command/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use PBAR;

pub fn login(
registry: Option<String>,
scope: Option<String>,
scope: &Option<String>,
always_auth: bool,
auth_type: Option<String>,
auth_type: &Option<String>,
log: &Logger,
) -> result::Result<(), failure::Error> {
let registry = registry.unwrap_or(npm::DEFAULT_NPM_REGISTRY.to_string());
let registry = registry.unwrap_or_else(|| npm::DEFAULT_NPM_REGISTRY.to_string());

info!(&log, "Logging in to npm...");
info!(
Expand All @@ -25,6 +25,6 @@ pub fn login(
npm::npm_login(log, &registry, &scope, always_auth, &auth_type)?;
info!(&log, "Logged you in!");

PBAR.message(&format!("👋 logged you in!"));
PBAR.message(&"👋 logged you in!".to_string());
Ok(())
}
11 changes: 4 additions & 7 deletions src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub enum Command {
pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error> {
// Run the correct command based off input and store the result of it so that we can clear
// the progress bar then return it
let status = match command {
match command {
Command::Build(build_opts) => {
info!(&log, "Running build command...");
Build::try_from_opts(build_opts).and_then(|mut b| b.run(&log))
Expand All @@ -107,7 +107,7 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
} => {
info!(&log, "Running publish command...");
info!(&log, "Path: {:?}", &path);
publish(target, path, access, &log)
publish(&target, path, access, &log)
}
Command::Login {
registry,
Expand All @@ -124,14 +124,11 @@ pub fn run_wasm_pack(command: Command, log: &Logger) -> result::Result<(), Error
&always_auth,
&auth_type
);
login(registry, scope, always_auth, auth_type, &log)
login(registry, &scope, always_auth, &auth_type, &log)
}
Command::Test(test_opts) => {
info!(&log, "Running test command...");
Test::try_from_opts(test_opts).and_then(|t| t.run(&log))
}
};

// Return the actual status of the program to the main function
status
}
}
2 changes: 1 addition & 1 deletion src/command/publish/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use PBAR;
/// Creates a tarball from a 'pkg' directory
/// and publishes it to the NPM registry
pub fn publish(
_target: String,
_target: &str,
path: Option<PathBuf>,
access: Option<Access>,
log: &Logger,
Expand Down
2 changes: 1 addition & 1 deletion src/command/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use PBAR;
/// If an explicit path is given, then use it, otherwise assume the current
/// directory is the crate path.
pub fn set_crate_path(path: Option<PathBuf>) -> Result<PathBuf, failure::Error> {
Ok(path.unwrap_or(PathBuf::from(".")))
Ok(path.unwrap_or_else(|| PathBuf::from(".")))
}

/// Construct our `pkg` directory in the crate.
Expand Down
2 changes: 1 addition & 1 deletion src/installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ fn confirm_can_overwrite(dst: &Path) -> Result<(), failure::Error> {
.read_line(&mut line)
.with_context(|_| "failed to read stdin")?;

if line.starts_with("y") || line.starts_with("Y") {
if line.starts_with('y') || line.starts_with('Y') {
return Ok(());
}

Expand Down
2 changes: 2 additions & 0 deletions src/lockfile.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Reading Cargo.lock lock file.

#![allow(clippy::new_ret_no_self)]

use std::fs;
use std::path::PathBuf;

Expand Down
6 changes: 1 addition & 5 deletions src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ fn log_file_path(cmd: &Command) -> PathBuf {
let path = match cmd {
Command::Build(build_opts) => &build_opts.path,
Command::Pack { path } => path,
Command::Publish {
target: _,
path,
access: _,
} => path,
Command::Publish { path, .. } => path,
Command::Test(test_opts) => &test_opts.path,
Command::Login { .. } => &None,
};
Expand Down
14 changes: 8 additions & 6 deletions src/manifest/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Reading and writing Cargo.toml and package.json manifests.

#![allow(clippy::new_ret_no_self, clippy::needless_pass_by_value)]

mod npm;

use std::fs;
Expand All @@ -20,7 +22,7 @@ use strsim::levenshtein;
use toml;
use PBAR;

const WASM_PACK_METADATA_KEY: &'static str = "package.metadata.wasm-pack";
const WASM_PACK_METADATA_KEY: &str = "package.metadata.wasm-pack";

/// Store for metadata learned about a crate
pub struct CrateData {
Expand Down Expand Up @@ -145,7 +147,7 @@ impl CargoWasmPackProfile {
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(Self::default_dev());
profile.update_with_defaults(&Self::default_dev());
Ok(profile)
}

Expand All @@ -154,7 +156,7 @@ impl CargoWasmPackProfile {
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(Self::default_release());
profile.update_with_defaults(&Self::default_release());
Ok(profile)
}

Expand All @@ -163,11 +165,11 @@ impl CargoWasmPackProfile {
D: serde::Deserializer<'de>,
{
let mut profile = <Option<Self>>::deserialize(deserializer)?.unwrap_or_default();
profile.update_with_defaults(Self::default_profiling());
profile.update_with_defaults(&Self::default_profiling());
Ok(profile)
}

fn update_with_defaults(&mut self, defaults: Self) {
fn update_with_defaults(&mut self, defaults: &Self) {
macro_rules! d {
( $( $path:ident ).* ) => {
self. $( $path ).* .get_or_insert(defaults. $( $path ).* .unwrap());
Expand Down Expand Up @@ -248,7 +250,7 @@ impl CrateData {
for e in errors[..errors.len() - 1].iter().rev() {
err = err.context(e.to_string()).into();
}
return err;
err
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use slog::Logger;
use std::process::{Command, Stdio};

/// The default npm registry used when we aren't working with a custom registry.
pub const DEFAULT_NPM_REGISTRY: &'static str = "https://registry.npmjs.org/";
pub const DEFAULT_NPM_REGISTRY: &str = "https://registry.npmjs.org/";

/// Run the `npm pack` command.
pub fn npm_pack(log: &Logger, path: &str) -> Result<(), failure::Error> {
Expand All @@ -24,7 +24,7 @@ pub fn npm_publish(log: &Logger, path: &str, access: Option<Access>) -> Result<(
Some(a) => cmd
.current_dir(path)
.arg("publish")
.arg(&format!("{}", a.to_string()))
.arg(&a.to_string())
.stdin(Stdio::inherit())
.stdout(Stdio::inherit()),
None => cmd
Expand All @@ -41,7 +41,7 @@ pub fn npm_publish(log: &Logger, path: &str, access: Option<Access>) -> Result<(
/// Run the `npm login` command.
pub fn npm_login(
log: &Logger,
registry: &String,
registry: &str,
scope: &Option<String>,
always_auth: bool,
auth_type: &Option<String>,
Expand All @@ -54,7 +54,7 @@ pub fn npm_login(
args.push_str(&format!(" --scope={}", scope));
}

if always_auth == true {
if always_auth {
args.push_str(" --always_auth");
}

Expand Down
8 changes: 7 additions & 1 deletion src/progressbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl ProgressOutput {
}

/// Add an error message.
pub fn error(&self, message: String) {
pub fn error(&self, message: &str) {
let err = format!(
"{} {}: {}",
emoji::ERROR,
Expand Down Expand Up @@ -135,3 +135,9 @@ impl Drop for ProgressOutput {
self.done();
}
}

impl Default for ProgressOutput {
fn default() -> Self {
Self::new()
}
}
4 changes: 2 additions & 2 deletions src/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub fn copy_from_crate(path: &Path, out_dir: &Path, step: &Step) -> Result<(), f
PBAR.step(step, &msg);
let crate_readme_path = path.join("README.md");
let new_readme_path = out_dir.join("README.md");
if let Err(_) = fs::copy(&crate_readme_path, &new_readme_path) {
if fs::copy(&crate_readme_path, &new_readme_path).is_err() {
PBAR.warn("origin crate has no README");
};
}
Ok(())
}

0 comments on commit 4870b00

Please sign in to comment.