Lints and clippys

This commit is contained in:
Félix Saparelli 2019-10-27 23:47:35 +13:00
parent 6b028cb649
commit f56e838fb9
No known key found for this signature in database
GPG key ID: 25940898BB90EA51
8 changed files with 47 additions and 52 deletions

View file

@ -199,7 +199,7 @@ where
let cmd: Vec<String> = values_t!(args.values_of("command"), String)?; let cmd: Vec<String> = values_t!(args.values_of("command"), String)?;
let paths = values_t!(args.values_of("path"), String) let paths = values_t!(args.values_of("path"), String)
.unwrap_or(vec![".".into()]) .unwrap_or_else(|_| vec![".".into()])
.iter() .iter()
.map(|string_path| string_path.into()) .map(|string_path| string_path.into())
.collect(); .collect();
@ -212,7 +212,7 @@ where
args.value_of("signal").map(str::to_string) args.value_of("signal").map(str::to_string)
}; };
let mut filters = values_t!(args.values_of("filter"), String).unwrap_or(Vec::new()); let mut filters = values_t!(args.values_of("filter"), String).unwrap_or_else(|_| Vec::new());
if let Some(extensions) = args.values_of("extensions") { if let Some(extensions) = args.values_of("extensions") {
for exts in extensions { for exts in extensions {
@ -240,7 +240,7 @@ where
if args.occurrences_of("no-default-ignore") == 0 { if args.occurrences_of("no-default-ignore") == 0 {
ignores.extend(default_ignores) ignores.extend(default_ignores)
}; };
ignores.extend(values_t!(args.values_of("ignore"), String).unwrap_or(Vec::new())); ignores.extend(values_t!(args.values_of("ignore"), String).unwrap_or_else(|_| Vec::new()));
let poll_interval = if args.occurrences_of("poll") > 0 { let poll_interval = if args.occurrences_of("poll") > 0 {
value_t!(args.value_of("poll"), u32).unwrap_or_else(|e| e.exit()) value_t!(args.value_of("poll"), u32).unwrap_or_else(|e| e.exit())
@ -267,14 +267,14 @@ where
} }
Ok(Args { Ok(Args {
cmd: cmd, cmd,
paths: paths, paths,
filters: filters, filters,
ignores: ignores, ignores,
signal: signal, signal,
clear_screen: args.is_present("clear"), clear_screen: args.is_present("clear"),
restart: args.is_present("restart"), restart: args.is_present("restart"),
debounce: debounce, debounce,
debug: args.is_present("verbose"), debug: args.is_present("verbose"),
run_initially: !args.is_present("postpone"), run_initially: !args.is_present("postpone"),
no_shell: args.is_present("no-shell"), no_shell: args.is_present("no-shell"),
@ -282,6 +282,6 @@ where
no_ignore: args.is_present("no-ignore"), no_ignore: args.is_present("no-ignore"),
once: args.is_present("once"), once: args.is_present("once"),
poll: args.occurrences_of("poll") > 0, poll: args.occurrences_of("poll") > 0,
poll_interval: poll_interval, poll_interval,
}) })
} }

View file

@ -84,7 +84,7 @@ pub fn load(paths: &[PathBuf]) -> Gitignore {
impl Gitignore { impl Gitignore {
fn new(files: Vec<GitignoreFile>) -> Gitignore { fn new(files: Vec<GitignoreFile>) -> Gitignore {
Gitignore { files: files } Gitignore { files }
} }
pub fn is_excluded(&self, path: &Path) -> bool { pub fn is_excluded(&self, path: &Path) -> bool {
@ -129,13 +129,13 @@ impl GitignoreFile {
let parsed_patterns = GitignoreFile::parse(strs); let parsed_patterns = GitignoreFile::parse(strs);
for p in parsed_patterns { for p in parsed_patterns {
let mut pat = String::from(p.pattern.clone()); let mut pat = p.pattern.clone();
if !p.anchored && !pat.starts_with("**/") { if !p.anchored && !pat.starts_with("**/") {
pat = "**/".to_string() + &pat; pat = "**/".to_string() + &pat;
} }
if !pat.ends_with("/**") { if !pat.ends_with("/**") {
pat = pat + "/**"; pat += "/**";
} }
let glob = GlobBuilder::new(&pat).literal_separator(true).build()?; let glob = GlobBuilder::new(&pat).literal_separator(true).build()?;
@ -146,7 +146,7 @@ impl GitignoreFile {
Ok(GitignoreFile { Ok(GitignoreFile {
set: builder.build()?, set: builder.build()?,
patterns: patterns, patterns,
root: root.to_owned(), root: root.to_owned(),
}) })
} }
@ -158,14 +158,14 @@ impl GitignoreFile {
fn matches(&self, path: &Path) -> MatchResult { fn matches(&self, path: &Path) -> MatchResult {
let stripped = path.strip_prefix(&self.root); let stripped = path.strip_prefix(&self.root);
if !stripped.is_ok() { if stripped.is_err() {
return MatchResult::None; return MatchResult::None;
} }
let matches = self.set.matches(stripped.unwrap()); let matches = self.set.matches(stripped.unwrap());
for &i in matches.iter().rev() { if let Some(i) = matches.iter().rev().next() {
let pattern = &self.patterns[i]; let pattern = &self.patterns[*i];
return match pattern.pattern_type { return match pattern.pattern_type {
PatternType::Whitelist => MatchResult::Whitelist, PatternType::Whitelist => MatchResult::Whitelist,
PatternType::Ignore => MatchResult::Ignore, PatternType::Ignore => MatchResult::Ignore,
@ -217,8 +217,8 @@ impl Pattern {
Pattern { Pattern {
pattern: normalized, pattern: normalized,
pattern_type: pattern_type, pattern_type,
anchored: anchored, anchored,
} }
} }
} }

View file

@ -77,7 +77,7 @@ pub fn load(paths: &[PathBuf]) -> Ignore {
impl Ignore { impl Ignore {
fn new(files: Vec<IgnoreFile>) -> Ignore { fn new(files: Vec<IgnoreFile>) -> Ignore {
Ignore { files: files } Ignore { files }
} }
pub fn is_excluded(&self, path: &Path) -> bool { pub fn is_excluded(&self, path: &Path) -> bool {
@ -122,13 +122,13 @@ impl IgnoreFile {
let parsed_patterns = IgnoreFile::parse(strs); let parsed_patterns = IgnoreFile::parse(strs);
for p in parsed_patterns { for p in parsed_patterns {
let mut pat = String::from(p.pattern.clone()); let mut pat = p.pattern.clone();
if !p.anchored && !pat.starts_with("**/") { if !p.anchored && !pat.starts_with("**/") {
pat = "**/".to_string() + &pat; pat = "**/".to_string() + &pat;
} }
if !pat.ends_with("/**") { if !pat.ends_with("/**") {
pat = pat + "/**"; pat += "/**";
} }
let glob = GlobBuilder::new(&pat).literal_separator(true).build()?; let glob = GlobBuilder::new(&pat).literal_separator(true).build()?;
@ -139,7 +139,7 @@ impl IgnoreFile {
Ok(IgnoreFile { Ok(IgnoreFile {
set: builder.build()?, set: builder.build()?,
patterns: patterns, patterns,
root: root.to_owned(), root: root.to_owned(),
}) })
} }
@ -151,14 +151,14 @@ impl IgnoreFile {
fn matches(&self, path: &Path) -> MatchResult { fn matches(&self, path: &Path) -> MatchResult {
let stripped = path.strip_prefix(&self.root); let stripped = path.strip_prefix(&self.root);
if !stripped.is_ok() { if stripped.is_err() {
return MatchResult::None; return MatchResult::None;
} }
let matches = self.set.matches(stripped.unwrap()); let matches = self.set.matches(stripped.unwrap());
for &i in matches.iter().rev() { if let Some(i) = matches.iter().rev().next() {
let pattern = &self.patterns[i]; let pattern = &self.patterns[*i];
return match pattern.pattern_type { return match pattern.pattern_type {
PatternType::Whitelist => MatchResult::Whitelist, PatternType::Whitelist => MatchResult::Whitelist,
PatternType::Ignore => MatchResult::Ignore, PatternType::Ignore => MatchResult::Ignore,
@ -210,8 +210,8 @@ impl Pattern {
Pattern { Pattern {
pattern: normalized, pattern: normalized,
pattern_type: pattern_type, pattern_type,
anchored: anchored, anchored,
} }
} }
} }

View file

@ -30,7 +30,7 @@ impl NotificationFilter {
let mut ignore_set_builder = GlobSetBuilder::new(); let mut ignore_set_builder = GlobSetBuilder::new();
for i in ignores { for i in ignores {
let mut ignore_path = Path::new(i).to_path_buf(); let mut ignore_path = Path::new(i).to_path_buf();
if ignore_path.is_relative() && !i.starts_with("*") { if ignore_path.is_relative() && !i.starts_with('*') {
ignore_path = Path::new("**").join(&ignore_path); ignore_path = Path::new("**").join(&ignore_path);
} }
let pattern = ignore_path.to_str().unwrap(); let pattern = ignore_path.to_str().unwrap();
@ -42,8 +42,8 @@ impl NotificationFilter {
filters: filter_set_builder.build()?, filters: filter_set_builder.build()?,
filter_count: filters.len(), filter_count: filters.len(),
ignores: ignore_set_builder.build()?, ignores: ignore_set_builder.build()?,
gitignore_files: gitignore_files, gitignore_files,
ignore_files: ignore_files, ignore_files,
}) })
} }

View file

@ -13,8 +13,8 @@ impl PathOp {
pub fn new(path: &Path, op: Option<op::Op>, cookie: Option<u32>) -> PathOp { pub fn new(path: &Path, op: Option<op::Op>, cookie: Option<u32>) -> PathOp {
PathOp { PathOp {
path: path.to_path_buf(), path: path.to_path_buf(),
op: op, op,
cookie: cookie, cookie,
} }
} }

View file

@ -3,7 +3,7 @@ use pathop::PathOp;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::path::PathBuf; use std::path::PathBuf;
pub fn spawn(cmd: &Vec<String>, updated_paths: &[PathOp], no_shell: bool) -> Result<Process> { pub fn spawn(cmd: &[String], updated_paths: &[PathOp], no_shell: bool) -> Result<Process> {
self::imp::Process::new(cmd, updated_paths, no_shell).map_err(|e| e.into()) self::imp::Process::new(cmd, updated_paths, no_shell).map_err(|e| e.into())
} }
@ -79,10 +79,9 @@ mod imp {
} }
} }
#[allow(unknown_lints)] #[allow(clippy::mutex_atomic)]
#[allow(mutex_atomic)]
impl Process { impl Process {
pub fn new(cmd: &Vec<String>, updated_paths: &[PathOp], no_shell: bool) -> Result<Process> { pub fn new(cmd: &[String], updated_paths: &[PathOp], no_shell: bool) -> Result<Process> {
use nix::unistd::*; use nix::unistd::*;
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
@ -110,8 +109,8 @@ mod imp {
command.env(name, val); command.env(name, val);
} }
unsafe { command.pre_exec(|| setsid().map_err(from_nix_error).map(|_| ())); }
command command
.before_exec(|| setsid().map_err(from_nix_error).map(|_| ()))
.spawn() .spawn()
.and_then(|p| { .and_then(|p| {
Ok(Process { Ok(Process {
@ -130,7 +129,6 @@ mod imp {
loop { loop {
match waitpid(Pid::from_raw(-self.pgid), Some(WaitPidFlag::WNOHANG)) { match waitpid(Pid::from_raw(-self.pgid), Some(WaitPidFlag::WNOHANG)) {
Ok(WaitStatus::Exited(_, _)) | Ok(WaitStatus::Signaled(_, _, _)) => { Ok(WaitStatus::Exited(_, _)) | Ok(WaitStatus::Signaled(_, _, _)) => {
finished = finished && true
} }
Ok(_) => { Ok(_) => {
finished = false; finished = false;
@ -284,8 +282,8 @@ mod imp {
resume_threads(handle); resume_threads(handle);
Ok(Process { Ok(Process {
job: job, job,
completion_port: completion_port, completion_port,
}) })
}) })
} }
@ -373,9 +371,9 @@ mod imp {
/// RENAMED -> `notify::ops::RENAME` /// RENAMED -> `notify::ops::RENAME`
fn collect_path_env_vars(pathops: &[PathOp]) -> Vec<(String, String)> { fn collect_path_env_vars(pathops: &[PathOp]) -> Vec<(String, String)> {
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
const ENV_SEP: &'static str = ":"; const ENV_SEP: &str = ":";
#[cfg(not(target_family = "unix"))] #[cfg(not(target_family = "unix"))]
const ENV_SEP: &'static str = ";"; const ENV_SEP: &str = ";";
let mut by_op = HashMap::new(); // Paths as `String`s collected by `notify::op` let mut by_op = HashMap::new(); // Paths as `String`s collected by `notify::op`
let mut all_pathbufs = HashSet::new(); // All unique `PathBuf`s let mut all_pathbufs = HashSet::new(); // All unique `PathBuf`s
@ -417,7 +415,7 @@ fn collect_path_env_vars(pathops: &[PathOp]) -> Vec<(String, String)> {
let paths = if let Some(ref common_path) = common_path { let paths = if let Some(ref common_path) = common_path {
paths paths
.iter() .iter()
.map(|path_str| path_str.trim_left_matches(common_path).to_string()) .map(|path_str| path_str.trim_start_matches(common_path).to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
} else { } else {
paths paths

View file

@ -97,9 +97,9 @@ where
let filter = NotificationFilter::new(&args.filters, &args.ignores, gitignore, ignore)?; let filter = NotificationFilter::new(&args.filters, &args.ignores, gitignore, ignore)?;
let (tx, rx) = channel(); let (tx, rx) = channel();
let poll = args.poll.clone(); let poll = args.poll;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
let poll_interval = args.poll_interval.clone(); let poll_interval = args.poll_interval;
let watcher = Watcher::new(tx.clone(), &paths, args.poll, args.poll_interval).or_else(|err| { let watcher = Watcher::new(tx.clone(), &paths, args.poll, args.poll_interval).or_else(|err| {
if poll { if poll {
return Err(err); return Err(err);
@ -129,11 +129,9 @@ where
} }
// Call handler initially, if necessary // Call handler initially, if necessary
if args.run_initially { if args.run_initially && !handler.on_manual()? {
if !handler.on_manual()? {
return Ok(()); return Ok(());
} }
}
loop { loop {
debug!("Waiting for filesystem activity"); debug!("Waiting for filesystem activity");

View file

@ -1,7 +1,7 @@
use std::sync::Mutex; use std::sync::Mutex;
lazy_static! { lazy_static! {
static ref CLEANUP: Mutex<Option<Box<Fn(self::Signal) + Send>>> = Mutex::new(None); static ref CLEANUP: Mutex<Option<Box<dyn Fn(self::Signal) + Send>>> = Mutex::new(None);
} }
#[cfg(unix)] #[cfg(unix)]
@ -75,7 +75,6 @@ pub fn install_handler<F>(handler: F)
where where
F: Fn(self::Signal) + 'static + Send + Sync, F: Fn(self::Signal) + 'static + Send + Sync,
{ {
use nix::libc::c_int;
use nix::sys::signal::*; use nix::sys::signal::*;
use std::thread; use std::thread;