diff options
author | Dan Davison <dandavison7@gmail.com> | 2019-08-08 09:06:00 -0700 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2019-08-08 09:06:00 -0700 |
commit | 96e8109bc89da08ec86aa488f7401f1c634f3b6c (patch) | |
tree | bebe272d6bb04afd34511223980b43abad15fec3 /src | |
parent | 63b30eef695196301d21a85bf60b927fa83f15e1 (diff) |
Clippy
Diffstat (limited to 'src')
-rw-r--r-- | src/align.rs | 12 | ||||
-rw-r--r-- | src/config.rs | 13 | ||||
-rw-r--r-- | src/delta.rs | 6 | ||||
-rw-r--r-- | src/edits.rs | 6 | ||||
-rw-r--r-- | src/main.rs | 9 | ||||
-rw-r--r-- | src/paint.rs | 49 | ||||
-rw-r--r-- | src/parse.rs | 15 |
7 files changed, 52 insertions, 58 deletions
diff --git a/src/align.rs b/src/align.rs index bd6aa834..6e9b5af4 100644 --- a/src/align.rs +++ b/src/align.rs @@ -106,7 +106,7 @@ impl<'a> Alignment<'a> { let mut ops = VecDeque::with_capacity(max(self.x.len(), self.y.len())); let mut cell = &self.table[self.index(self.x.len(), self.y.len())]; loop { - ops.push_front(cell.operation.clone()); + ops.push_front(cell.operation); if cell.parent == 0 { break; } @@ -169,12 +169,12 @@ impl<'a> Alignment<'a> { fn print(&self) { println!("x: {:?}", self.x); println!("y: {:?}", self.y); - print!("\n"); + println!(); print!(" "); for j in 0..self.dim[1] { print!("{} ", if j > 0 { self.x[j - 1] } else { " " }) } - print!("\n"); + println!(); for i in 0..self.dim[0] { for j in 0..self.dim[1] { @@ -184,9 +184,9 @@ impl<'a> Alignment<'a> { let cell = &self.table[self.index(j, i)]; print!("{} ", self.format_cell(cell)); } - print!("\n"); + println!(); } - print!("\n"); + println!(); } } @@ -197,7 +197,7 @@ where { let mut encoded = Vec::with_capacity(sequence.len()); - if sequence.len() == 0 { + if sequence.is_empty() { return encoded; } diff --git a/src/config.rs b/src/config.rs index 0f8c3d25..75412522 100644 --- a/src/config.rs +++ b/src/config.rs @@ -31,10 +31,13 @@ pub fn get_config<'a>( ) -> Config<'a> { let theme_name = match opt.theme { Some(ref theme) => theme, - None => match opt.light { - true => style::DEFAULT_LIGHT_THEME, - false => style::DEFAULT_DARK_THEME, - }, + None => { + if opt.light { + style::DEFAULT_LIGHT_THEME + } else { + style::DEFAULT_DARK_THEME + } + } }; let is_light_theme = style::LIGHT_THEMES.contains(&theme_name); @@ -92,7 +95,7 @@ pub fn get_config<'a>( Config { theme: &theme_set.themes[theme_name], - theme_name: theme_name, + theme_name, minus_style_modifier, minus_emph_style_modifier, plus_style_modifier, diff --git a/src/delta.rs b/src/delta.rs index a50b511e..ac794085 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -145,7 +145,7 @@ fn handle_file_meta_header_line( cli::SectionStyle::Plain => panic!(), }; let ansi_style = Blue.normal(); - write!(painter.writer, "\n")?; + writeln!(painter.writer)?; draw_fn( painter.writer, &ansi_style.paint(parse::get_file_change_description_from_file_paths( @@ -170,7 +170,7 @@ fn handle_hunk_meta_line( }; let ansi_style = Blue.normal(); let (code_fragment, line_number) = parse::parse_hunk_metadata(&line); - if code_fragment.len() > 0 { + if !code_fragment.is_empty() { let syntax_style_sections = Painter::get_line_syntax_style_sections( code_fragment, &mut painter.highlighter, @@ -254,7 +254,7 @@ fn handle_hunk_line(painter: &mut Painter, line: &str, state: State, config: &Co // highlight correctly. // See https://docs.rs/syntect/3.2.0/syntect/parsing/struct.SyntaxSetBuilder.html#method.add_from_folder fn prepare(line: &str) -> String { - if line.len() > 0 { + if !line.is_empty() { format!(" {}\n", &line[1..]) } else { "\n".to_string() diff --git a/src/edits.rs b/src/edits.rs index d6bcd528..38bec5c9 100644 --- a/src/edits.rs +++ b/src/edits.rs @@ -9,8 +9,8 @@ use crate::align; /// slices are slices of the line, and their concatenation equals the line. Return the input minus /// and plus lines, in annotated form. pub fn infer_edits<'a, EditOperation>( - minus_lines: &'a Vec<String>, - plus_lines: &'a Vec<String>, + minus_lines: &'a [String], + plus_lines: &'a [String], noop_deletion: EditOperation, deletion: EditOperation, noop_insertion: EditOperation, @@ -537,7 +537,7 @@ mod tests { for (edit, s) in annotated_line { print!("({} {}), ", fmt_edit(edit), s.trim_end()); } - print!("\n"); + println!(); } #[allow(dead_code)] diff --git a/src/main.rs b/src/main.rs index 10da054a..1ed36e5e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,17 +61,16 @@ fn main() -> std::io::Result<()> { OutputType::from_mode(PagingMode::QuitIfOneScreen, Some(config.pager)).unwrap(); let mut writer = output_type.handle().unwrap(); - match delta( + if let Err(error) = delta( io::stdin().lock().lines().map(|l| l.unwrap()), &config, &assets, &mut writer, ) { - Err(error) => match error.kind() { + match error.kind() { ErrorKind::BrokenPipe => process::exit(0), _ => eprintln!("{}", error), - }, - _ => (), + } }; Ok(()) } @@ -148,7 +147,7 @@ index 541e930..e23bef1 100644 let mut writer = output_type.handle().unwrap(); delta( - input.split("\n").map(String::from), + input.split('\n').map(String::from), &config, &assets, &mut writer, diff --git a/src/paint.rs b/src/paint.rs index 27f78489..6a88e9c1 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -31,13 +31,13 @@ impl<'a> Painter<'a> { minus_lines: Vec::new(), plus_lines: Vec::new(), output_buffer: String::new(), - writer: writer, syntax: None, highlighter: HighlightLines::new( assets.syntax_set.find_syntax_by_extension("txt").unwrap(), config.theme, ), - config: config, + writer, + config, } } @@ -56,7 +56,7 @@ impl<'a> Painter<'a> { let (minus_line_diff_style_sections, plus_line_diff_style_sections) = Self::get_diff_style_sections(&self.minus_lines, &self.plus_lines, self.config); // TODO: lines and style sections contain identical line text - if self.minus_lines.len() > 0 { + if !self.minus_lines.is_empty() { Painter::paint_lines( &mut self.output_buffer, minus_line_syntax_style_sections, @@ -65,7 +65,7 @@ impl<'a> Painter<'a> { self.config.minus_style_modifier, ); } - if self.plus_lines.len() > 0 { + if !self.plus_lines.is_empty() { Painter::paint_lines( &mut self.output_buffer, plus_line_syntax_style_sections, @@ -114,7 +114,7 @@ impl<'a> Painter<'a> { } _ => (), } - write!(output_buffer, "\n").unwrap(); + writeln!(output_buffer).unwrap(); } } @@ -127,8 +127,8 @@ impl<'a> Painter<'a> { /// Perform syntax highlighting for minus and plus lines in buffer. fn get_syntax_style_sections<'m, 'p>( - minus_lines: &'m Vec<String>, - plus_lines: &'p Vec<String>, + minus_lines: &'m [String], + plus_lines: &'p [String], highlighter: &mut HighlightLines, config: &config::Config, ) -> (Vec<Vec<(Style, &'m str)>>, Vec<Vec<(Style, &'p str)>>) { @@ -168,8 +168,8 @@ impl<'a> Painter<'a> { /// Set background styles to represent diff for minus and plus lines in buffer. fn get_diff_style_sections<'b>( - minus_lines: &'b Vec<String>, - plus_lines: &'b Vec<String>, + minus_lines: &'b [String], + plus_lines: &'b [String], config: &config::Config, ) -> ( Vec<Vec<(StyleModifier, &'b str)>>, @@ -191,7 +191,7 @@ impl<'a> Painter<'a> { pub fn paint_text(text: &str, style: Style, output_buffer: &mut String) -> std::fmt::Result { use std::fmt::Write; - if text.len() == 0 { + if text.is_empty() { return Ok(()); } @@ -218,8 +218,8 @@ mod superimpose_style_sections { use syntect::highlighting::{Style, StyleModifier}; pub fn superimpose_style_sections( - sections_1: &Vec<(Style, &str)>, - sections_2: &Vec<(StyleModifier, &str)>, + sections_1: &[(Style, &str)], + sections_2: &[(StyleModifier, &str)], ) -> Vec<(Style, String)> { coalesce(superimpose( explode(sections_1) @@ -229,7 +229,7 @@ mod superimpose_style_sections { )) } - fn explode<T>(style_sections: &Vec<(T, &str)>) -> Vec<(T, char)> + fn explode<T>(style_sections: &[(T, &str)]) -> Vec<(T, char)> where T: Copy, { @@ -261,21 +261,18 @@ mod superimpose_style_sections { fn coalesce(style_sections: Vec<(Style, char)>) -> Vec<(Style, String)> { let mut coalesced: Vec<(Style, String)> = Vec::new(); let mut style_sections = style_sections.iter(); - match style_sections.next() { - Some((style, c)) => { - let mut current_string = c.to_string(); - let mut current_style = style; - for (style, c) in style_sections { - if style != current_style { - coalesced.push((*current_style, current_string)); - current_string = String::new(); - current_style = style; - } - current_string.push(*c); + if let Some((style, c)) = style_sections.next() { + let mut current_string = c.to_string(); + let mut current_style = style; + for (style, c) in style_sections { + if style != current_style { + coalesced.push((*current_style, current_string)); + current_string = String::new(); + current_style = style; } - coalesced.push((*current_style, current_string)); + current_string.push(*c); } - None => (), + coalesced.push((*current_style, current_string)); } coalesced } diff --git a/src/parse.rs b/src/parse.rs index 7485b9c9..fba0c099 100644 --- a/src/parse.rs +++ b/src/parse.rs @@ -17,13 +17,13 @@ pub fn get_file_extension_from_diff_line(line: &str) -> Option<&str> { pub fn get_file_path_from_file_meta_line(line: &str) -> String { if line.starts_with("rename") { - match line.split(" ").skip(2).next() { + match line.split(' ').nth(2) { Some(path) => path, _ => "", } .to_string() } else { - match line.split(" ").skip(1).next() { + match line.split(' ').nth(1) { Some("/dev/null") => "/dev/null", Some(path) => &path[2..], _ => "", @@ -34,7 +34,7 @@ pub fn get_file_path_from_file_meta_line(line: &str) -> String { pub fn get_file_change_description_from_file_paths(minus_file: &str, plus_file: &str) -> String { match (minus_file, plus_file) { - (minus_file, plus_file) if minus_file == plus_file => format!("{}", minus_file), + (minus_file, plus_file) if minus_file == plus_file => minus_file.to_string(), (minus_file, "/dev/null") => format!("deleted: {}", minus_file), ("/dev/null", plus_file) => format!("added: {}", plus_file), (minus_file, plus_file) => format!("renamed: {} ⟶ {}", minus_file, plus_file), @@ -48,12 +48,7 @@ pub fn parse_hunk_metadata(line: &str) -> (&str, &str) { let mut iter = line.split("@@").skip(1); let line_number = iter .next() - .and_then(|s| { - s.split("+") - .skip(1) - .next() - .and_then(|s| s.split(",").next()) - }) + .and_then(|s| s.split('+').nth(1).and_then(|s| s.split(',').next())) .unwrap_or(""); let code_fragment = iter.next().unwrap_or(""); (code_fragment, line_number) @@ -62,7 +57,7 @@ pub fn parse_hunk_metadata(line: &str) -> (&str, &str) { /// Given input like "diff --git a/src/main.rs b/src/main.rs" /// return ("rs", "rs"). fn get_file_extensions_from_diff_line(line: &str) -> (Option<&str>, Option<&str>) { - let mut iter = line.split(" ").skip(2); + let mut iter = line.split(' ').skip(2); ( iter.next().and_then(|s| get_extension(&s[2..])), iter.next().and_then(|s| get_extension(&s[2..])), |