Fix some clippy warnings

This commit is contained in:
Sylvestre Ledru 2020-12-28 23:46:13 +01:00
parent c2dc05eb56
commit bc4c82b132
3 changed files with 32 additions and 26 deletions

View file

@ -8,6 +8,7 @@ extern crate getopts;
extern crate regex; extern crate regex;
use getopts::Matches; use getopts::Matches;
use regex::Regex; use regex::Regex;
use std::cmp::Ordering;
use std::io::{self, BufReader}; use std::io::{self, BufReader};
use std::{ use std::{
fs::{remove_file, File}, fs::{remove_file, File},
@ -87,7 +88,7 @@ where
T: BufRead, T: BufRead,
{ {
let mut input_iter = InputSplitter::new(input.lines().enumerate()); let mut input_iter = InputSplitter::new(input.lines().enumerate());
let mut split_writer = SplitWriter::new(&options)?; let mut split_writer = SplitWriter::new(&options);
let ret = do_csplit(&mut split_writer, patterns, &mut input_iter); let ret = do_csplit(&mut split_writer, patterns, &mut input_iter);
// consume the rest // consume the rest
@ -98,7 +99,7 @@ where
for (_, line) in input_iter { for (_, line) in input_iter {
split_writer.writeln(line?)?; split_writer.writeln(line?)?;
} }
split_writer.finish_split()?; split_writer.finish_split();
} }
// delete files on error by default // delete files on error by default
if ret.is_err() && !options.keep_files { if ret.is_err() && !options.keep_files {
@ -118,6 +119,7 @@ where
// split the file based on patterns // split the file based on patterns
for pattern in patterns.into_iter() { for pattern in patterns.into_iter() {
let pattern_as_str = pattern.to_string(); let pattern_as_str = pattern.to_string();
#[allow(clippy::match_like_matches_macro)]
let is_skip = if let patterns::Pattern::SkipToMatch(_, _, _) = pattern { let is_skip = if let patterns::Pattern::SkipToMatch(_, _, _) = pattern {
true true
} else { } else {
@ -204,14 +206,14 @@ impl<'a> Drop for SplitWriter<'a> {
} }
impl<'a> SplitWriter<'a> { impl<'a> SplitWriter<'a> {
fn new(options: &CsplitOptions) -> io::Result<SplitWriter> { fn new(options: &CsplitOptions) -> SplitWriter {
Ok(SplitWriter { SplitWriter {
options, options,
counter: 0, counter: 0,
current_writer: None, current_writer: None,
size: 0, size: 0,
dev_null: false, dev_null: false,
}) }
} }
/// Creates a new split and returns its filename. /// Creates a new split and returns its filename.
@ -262,7 +264,7 @@ impl<'a> SplitWriter<'a> {
/// # Errors /// # Errors
/// ///
/// Some [`io::Error`] if the split could not be removed in case it should be elided. /// Some [`io::Error`] if the split could not be removed in case it should be elided.
fn finish_split(&mut self) -> io::Result<()> { fn finish_split(&mut self) {
if !self.dev_null { if !self.dev_null {
if self.options.elide_empty_files && self.size == 0 { if self.options.elide_empty_files && self.size == 0 {
self.counter -= 1; self.counter -= 1;
@ -270,7 +272,6 @@ impl<'a> SplitWriter<'a> {
println!("{}", self.size); println!("{}", self.size);
} }
} }
Ok(())
} }
/// Removes all the split files that were created. /// Removes all the split files that were created.
@ -314,23 +315,28 @@ impl<'a> SplitWriter<'a> {
let mut ret = Err(CsplitError::LineOutOfRange(pattern_as_str.to_string())); let mut ret = Err(CsplitError::LineOutOfRange(pattern_as_str.to_string()));
while let Some((ln, line)) = input_iter.next() { while let Some((ln, line)) = input_iter.next() {
let l = line?; let l = line?;
if ln + 1 > n { match n.cmp(&(&ln + 1)) {
if input_iter.add_line_to_buffer(ln, l).is_some() { Ordering::Less => {
panic!("the buffer is big enough to contain 1 line"); if input_iter.add_line_to_buffer(ln, l).is_some() {
panic!("the buffer is big enough to contain 1 line");
}
ret = Ok(());
break;
} }
ret = Ok(()); Ordering::Equal => {
break; if !self.options.suppress_matched
} else if ln + 1 == n { && input_iter.add_line_to_buffer(ln, l).is_some()
if !self.options.suppress_matched && input_iter.add_line_to_buffer(ln, l).is_some() {
{ panic!("the buffer is big enough to contain 1 line");
panic!("the buffer is big enough to contain 1 line"); }
ret = Ok(());
break;
} }
ret = Ok(()); Ordering::Greater => (),
break;
} }
self.writeln(l)?; self.writeln(l)?;
} }
self.finish_split()?; self.finish_split();
ret ret
} }
@ -386,7 +392,7 @@ impl<'a> SplitWriter<'a> {
self.writeln(line?)?; self.writeln(line?)?;
} }
None => { None => {
self.finish_split()?; self.finish_split();
return Err(CsplitError::LineOutOfRange( return Err(CsplitError::LineOutOfRange(
pattern_as_str.to_string(), pattern_as_str.to_string(),
)); ));
@ -394,7 +400,7 @@ impl<'a> SplitWriter<'a> {
}; };
offset -= 1; offset -= 1;
} }
self.finish_split()?; self.finish_split();
return Ok(()); return Ok(());
} }
self.writeln(l)?; self.writeln(l)?;
@ -420,7 +426,7 @@ impl<'a> SplitWriter<'a> {
panic!("should be big enough to hold every lines"); panic!("should be big enough to hold every lines");
} }
} }
self.finish_split()?; self.finish_split();
if input_iter.buffer_len() < offset_usize { if input_iter.buffer_len() < offset_usize {
return Err(CsplitError::LineOutOfRange(pattern_as_str.to_string())); return Err(CsplitError::LineOutOfRange(pattern_as_str.to_string()));
} }
@ -436,7 +442,7 @@ impl<'a> SplitWriter<'a> {
} }
} }
self.finish_split()?; self.finish_split();
Err(CsplitError::MatchNotFound(pattern_as_str.to_string())) Err(CsplitError::MatchNotFound(pattern_as_str.to_string()))
} }
} }

View file

@ -29,7 +29,7 @@ impl SplitName {
n_digits_opt: Option<String>, n_digits_opt: Option<String>,
) -> Result<SplitName, CsplitError> { ) -> Result<SplitName, CsplitError> {
// get the prefix // get the prefix
let prefix = prefix_opt.unwrap_or("xx".to_string()); let prefix = prefix_opt.unwrap_or_else(|| "xx".to_string());
// the width for the split offset // the width for the split offset
let n_digits = match n_digits_opt { let n_digits = match n_digits_opt {
None => 2, None => 2,

View file

@ -323,8 +323,8 @@ fn test_install_copy_file() {
assert!(at.file_exists(file2)); assert!(at.file_exists(file2));
} }
/*#[test] #[test]
#[cfg(target_os = "linux")]*/ #[cfg(target_os = "linux")]
fn test_install_target_file_dev_null() { fn test_install_target_file_dev_null() {
let (at, mut ucmd) = at_and_ucmd!(); let (at, mut ucmd) = at_and_ucmd!();
let file1 = "/dev/null"; let file1 = "/dev/null";