From bef83eef3802c8671fca9559bf828370539fa8e7 Mon Sep 17 00:00:00 2001 From: Ferran Basora Date: Thu, 23 Apr 2020 19:56:45 +0000 Subject: [PATCH] Fix clippy suggestions --- src/alphabets.rs | 6 +++--- src/main.rs | 6 +++--- src/state.rs | 10 +++++----- src/swapper.rs | 43 +++++++++++++++++++++---------------------- src/view.rs | 41 +++++++++++++++++++---------------------- 5 files changed, 51 insertions(+), 55 deletions(-) diff --git a/src/alphabets.rs b/src/alphabets.rs index 4387b8c..5709e61 100644 --- a/src/alphabets.rs +++ b/src/alphabets.rs @@ -31,7 +31,7 @@ pub struct Alphabet<'a> { impl<'a> Alphabet<'a> { fn new(letters: &'a str) -> Alphabet { - Alphabet { letters: letters } + Alphabet { letters } } pub fn hints(&self, matches: usize) -> Vec { @@ -44,7 +44,7 @@ impl<'a> Alphabet<'a> { if expansion.len() + expanded.len() >= matches { break; } - if expansion.len() == 0 { + if expansion.is_empty() { break; } @@ -61,7 +61,7 @@ impl<'a> Alphabet<'a> { expansion = expansion .iter() .take(matches - expanded.len()) - .map(|s| s.clone()) + .cloned() .collect(); expansion.append(&mut expanded); expansion diff --git a/src/main.rs b/src/main.rs index f1b56d2..eecc4fa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,7 @@ use clap::crate_version; use std::io::{self, Read}; fn app_args<'a>() -> clap::ArgMatches<'a> { - return App::new("thumbs") + App::new("thumbs") .version(crate_version!()) .about("A lightning fast version copy/pasting like vimium/vimperator") .arg( @@ -103,7 +103,7 @@ fn app_args<'a>() -> clap::ArgMatches<'a> { .long("contrast") .short("c"), ) - .get_matches(); + .get_matches() } fn main() { @@ -136,7 +136,7 @@ fn main() { handle.read_to_string(&mut output).unwrap(); - let lines = output.split("\n").collect::>(); + let lines = output.split('\n').collect::>(); let mut state = state::State::new(&lines, alphabet, ®exp); diff --git a/src/state.rs b/src/state.rs index dacbda9..f351c6e 100644 --- a/src/state.rs +++ b/src/state.rs @@ -65,9 +65,9 @@ pub struct State<'a> { impl<'a> State<'a> { pub fn new(lines: &'a Vec<&'a str>, alphabet: &'a str, regexp: &'a Vec<&'a str>) -> State<'a> { State { - lines: lines, - alphabet: alphabet, - regexp: regexp, + lines, + alphabet, + regexp, } } @@ -131,7 +131,7 @@ impl<'a> State<'a> { } chunk = chunk.get(matching.end()..).expect("Unknown chunk"); - offset = offset + matching.end() as i32; + offset += matching.end() as i32; } else { panic!("No matching?"); } @@ -175,7 +175,7 @@ impl<'a> State<'a> { matches.reverse(); } - return matches; + matches } } diff --git a/src/swapper.rs b/src/swapper.rs index ebbdc01..4bfa274 100644 --- a/src/swapper.rs +++ b/src/swapper.rs @@ -69,17 +69,17 @@ impl<'a> Swapper<'a> { let signal = format!("thumbs-finished-{}", since_the_epoch.as_secs()); Swapper { - executor: executor, - dir: dir, - command: command, - upcase_command: upcase_command, + executor, + dir, + command, + upcase_command, active_pane_id: None, active_pane_height: None, active_pane_scroll_position: None, active_pane_in_copy_mode: None, thumbs_pane_id: None, content: None, - signal: signal, + signal, } } @@ -95,33 +95,31 @@ impl<'a> Swapper<'a> { .executor .execute(active_command.iter().map(|arg| arg.to_string()).collect()); - let lines: Vec<&str> = output.split("\n").collect(); + let lines: Vec<&str> = output.split('\n').collect(); let chunks: Vec> = lines .into_iter() - .map(|line| line.split(":").collect()) + .map(|line| line.split(':').collect()) .collect(); let active_pane = chunks .iter() - .find(|&chunks| *chunks.iter().nth(4).unwrap() == "active") + .find(|&chunks| *chunks.get(4).unwrap() == "active") .expect("Unable to find active pane"); - let pane_id = active_pane.iter().nth(0).unwrap(); - let pane_in_copy_mode = active_pane.iter().nth(1).unwrap().to_string(); + let pane_id = active_pane.get(0).unwrap(); + let pane_in_copy_mode = active_pane.get(1).unwrap().to_string(); self.active_pane_id = Some(pane_id.to_string()); self.active_pane_in_copy_mode = Some(pane_in_copy_mode); if self.active_pane_in_copy_mode.clone().unwrap() == "1" { let pane_height = active_pane - .iter() - .nth(2) + .get(2) .unwrap() .parse() .expect("Unable to retrieve pane height"); let pane_scroll_position = active_pane - .iter() - .nth(3) + .get(3) .unwrap() .parse() .expect("Unable to retrieve pane scroll"); @@ -135,7 +133,7 @@ impl<'a> Swapper<'a> { let options_command = vec!["tmux", "show", "-g"]; let params: Vec = options_command.iter().map(|arg| arg.to_string()).collect(); let options = self.executor.execute(params); - let lines: Vec<&str> = options.split("\n").collect(); + let lines: Vec<&str> = options.split('\n').collect(); let pattern = Regex::new(r#"@thumbs-([\w\-0-9]+) "(.*)""#).unwrap(); @@ -149,7 +147,7 @@ impl<'a> Swapper<'a> { let boolean_params = vec!["reverse", "unique", "contrast"]; if boolean_params.iter().any(|&x| x == name) { - return vec![format!("--{}", name).to_string()]; + return vec![format!("--{}", name)]; } let string_params = vec![ @@ -164,13 +162,13 @@ impl<'a> Swapper<'a> { if string_params.iter().any(|&x| x == name) { return vec![ - format!("--{}", name).to_string(), - format!("'{}'", value).to_string(), + format!("--{}", name), + format!("'{}'", value), ]; } if name.starts_with("regexp") { - return vec!["--regexp".to_string(), format!("'{}'", value).to_string()]; + return vec!["--regexp".to_string(), format!("'{}'", value)]; } vec![] @@ -298,7 +296,7 @@ mod tests { fn new(outputs: Vec) -> TestShell { TestShell { executed: None, - outputs: outputs, + outputs, } } } @@ -356,8 +354,9 @@ mod tests { assert_eq!(executor.last_executed().unwrap(), expectation); } } + fn app_args<'a>() -> clap::ArgMatches<'a> { - return App::new("tmux-thumbs") + App::new("tmux-thumbs") .version(crate_version!()) .about("A lightning fast version of tmux-fingers, copy/pasting tmux like vimium/vimperator") .arg( @@ -378,7 +377,7 @@ fn app_args<'a>() -> clap::ArgMatches<'a> { .long("upcase-command") .default_value("tmux set-buffer {} && tmux paste-buffer"), ) - .get_matches(); + .get_matches() } fn main() -> std::io::Result<()> { diff --git a/src/view.rs b/src/view.rs index b115df0..9ff1902 100644 --- a/src/view.rs +++ b/src/view.rs @@ -36,42 +36,40 @@ impl<'a> View<'a> { hint_background_color: Color, ) -> View<'a> { View { - state: state, + state, skip: 0, - multi: multi, - reverse: reverse, - unique: unique, - contrast: contrast, - position: position, - select_foreground_color: select_foreground_color, - select_background_color: select_background_color, - foreground_color: foreground_color, - background_color: background_color, - hint_foreground_color: hint_foreground_color, - hint_background_color: hint_background_color, + multi, + reverse, + unique, + contrast, + position, + select_foreground_color, + select_background_color, + foreground_color, + background_color, + hint_foreground_color, + hint_background_color, } } pub fn prev(&mut self) { if self.skip > 0 { - self.skip = self.skip - 1; + self.skip -= 1; } } pub fn next(&mut self, maximum: usize) { if self.skip < maximum { - self.skip = self.skip + 1; + self.skip += 1; } } fn make_hint_text(&self, hint: &str) -> String { - let text = if self.contrast { - format!("[{}]", hint).to_string() + if self.contrast { + format!("[{}]", hint) } else { hint.to_string() - }; - - text + } } pub fn present(&mut self) -> Vec<(String, bool)> { @@ -88,8 +86,7 @@ impl<'a> View<'a> { .iter() .filter_map(|m| m.hint.clone()) .max_by(|x, y| x.len().cmp(&y.len())) - .unwrap() - .clone(); + .unwrap(); let mut selected; let mut chosen = vec![]; @@ -102,7 +99,7 @@ impl<'a> View<'a> { for (index, line) in self.state.lines.iter().enumerate() { let clean = line.trim_end_matches(|c: char| c.is_whitespace()); - if clean.len() > 0 { + if !clean.is_empty() { let text = self.make_hint_text(line); rustbox.print(