Skip to content

Commit

Permalink
run clippy pedantic
Browse files Browse the repository at this point in the history
$ cargo +nightly clippy --allow-dirty --fix -- -W clippy::pedantic
  • Loading branch information
sylvestre committed Jan 23, 2024
1 parent fdc35f6 commit d891e10
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 184 deletions.
77 changes: 34 additions & 43 deletions src/context_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ fn make_diff(expected: &[u8], actual: &[u8], context_size: usize) -> Vec<Mismatc
results
}

#[must_use]
pub fn diff(
expected: &[u8],
expected_filename: &str,
actual: &[u8],
actual_filename: &str,
context_size: usize,
) -> Vec<u8> {
let mut output =
format!("*** {}\t\n--- {}\t\n", expected_filename, actual_filename).into_bytes();
let mut output = format!("*** {expected_filename}\t\n--- {actual_filename}\t\n").into_bytes();
let diff_results = make_diff(expected, actual, context_size);
if diff_results.is_empty() {
return Vec::new();
Expand All @@ -284,17 +284,16 @@ pub fn diff(
let exp_start = if end_line_number_expected == line_number_expected {
String::new()
} else {
format!("{},", line_number_expected)
format!("{line_number_expected},")
};
let act_start = if end_line_number_actual == line_number_actual {
String::new()
} else {
format!("{},", line_number_actual)
format!("{line_number_actual},")
};
writeln!(
output,
"***************\n*** {}{} ****",
exp_start, end_line_number_expected
"***************\n*** {exp_start}{end_line_number_expected} ****"
)
.expect("write to Vec is infallible");
if !result.expected_all_context {
Expand Down Expand Up @@ -322,7 +321,7 @@ pub fn diff(
.expect("write to Vec is infallible");
}
}
writeln!(output, "--- {}{} ----", act_start, end_line_number_actual)
writeln!(output, "--- {act_start}{end_line_number_actual} ----")
.expect("write to Vec is infallible");
if !result.actual_all_context {
for line in result.actual {
Expand Down Expand Up @@ -406,29 +405,27 @@ mod tests {
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff(&alef, "a/alef", &bet, &format!("{}/alef", target), 2);
File::create(&format!("{}/ab.diff", target))
diff(&alef, "a/alef", &bet, &format!("{target}/alef"), 2);
File::create(&format!("{target}/ab.diff"))
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{}/alef", target)).unwrap();
let mut fa = File::create(&format!("{target}/alef")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{}/bet", target)).unwrap();
let mut fb = File::create(&format!("{target}/bet")).unwrap();
fb.write_all(&bet[..]).unwrap();
let _ = fa;
let _ = fb;
let output = Command::new("patch")
.arg("-p0")
.arg("--context")
.stdin(File::open(&format!("{}/ab.diff", target)).unwrap())
.stdin(File::open(&format!("{target}/ab.diff")).unwrap())
.output()
.unwrap();
if !output.status.success() {
panic!("{:?}", output);
}
assert!(output.status.success(), "{:?}", output);
//println!("{}", String::from_utf8_lossy(&output.stdout));
//println!("{}", String::from_utf8_lossy(&output.stderr));
let alef = fs::read(&format!("{}/alef", target)).unwrap();
let alef = fs::read(&format!("{target}/alef")).unwrap();
assert_eq!(alef, bet);
}
}
Expand Down Expand Up @@ -481,29 +478,27 @@ mod tests {
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff(&alef, "a/alef_", &bet, &format!("{}/alef_", target), 2);
File::create(&format!("{}/ab_.diff", target))
diff(&alef, "a/alef_", &bet, &format!("{target}/alef_"), 2);
File::create(&format!("{target}/ab_.diff"))
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{}/alef_", target)).unwrap();
let mut fa = File::create(&format!("{target}/alef_")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{}/bet_", target)).unwrap();
let mut fb = File::create(&format!("{target}/bet_")).unwrap();
fb.write_all(&bet[..]).unwrap();
let _ = fa;
let _ = fb;
let output = Command::new("patch")
.arg("-p0")
.arg("--context")
.stdin(File::open(&format!("{}/ab_.diff", target)).unwrap())
.stdin(File::open(&format!("{target}/ab_.diff")).unwrap())
.output()
.unwrap();
if !output.status.success() {
panic!("{:?}", output);
}
assert!(output.status.success(), "{:?}", output);
//println!("{}", String::from_utf8_lossy(&output.stdout));
//println!("{}", String::from_utf8_lossy(&output.stderr));
let alef = fs::read(&format!("{}/alef_", target)).unwrap();
let alef = fs::read(&format!("{target}/alef_")).unwrap();
assert_eq!(alef, bet);
}
}
Expand Down Expand Up @@ -559,29 +554,27 @@ mod tests {
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff(&alef, "a/alefx", &bet, &format!("{}/alefx", target), 2);
File::create(&format!("{}/abx.diff", target))
diff(&alef, "a/alefx", &bet, &format!("{target}/alefx"), 2);
File::create(&format!("{target}/abx.diff"))
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{}/alefx", target)).unwrap();
let mut fa = File::create(&format!("{target}/alefx")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{}/betx", target)).unwrap();
let mut fb = File::create(&format!("{target}/betx")).unwrap();
fb.write_all(&bet[..]).unwrap();
let _ = fa;
let _ = fb;
let output = Command::new("patch")
.arg("-p0")
.arg("--context")
.stdin(File::open(&format!("{}/abx.diff", target)).unwrap())
.stdin(File::open(&format!("{target}/abx.diff")).unwrap())
.output()
.unwrap();
if !output.status.success() {
panic!("{:?}", output);
}
assert!(output.status.success(), "{:?}", output);
//println!("{}", String::from_utf8_lossy(&output.stdout));
//println!("{}", String::from_utf8_lossy(&output.stderr));
let alef = fs::read(&format!("{}/alefx", target)).unwrap();
let alef = fs::read(&format!("{target}/alefx")).unwrap();
assert_eq!(alef, bet);
}
}
Expand Down Expand Up @@ -640,29 +633,27 @@ mod tests {
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff(&alef, "a/alefr", &bet, &format!("{}/alefr", target), 2);
File::create(&format!("{}/abr.diff", target))
diff(&alef, "a/alefr", &bet, &format!("{target}/alefr"), 2);
File::create(&format!("{target}/abr.diff"))
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{}/alefr", target)).unwrap();
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{}/betr", target)).unwrap();
let mut fb = File::create(&format!("{target}/betr")).unwrap();
fb.write_all(&bet[..]).unwrap();
let _ = fa;
let _ = fb;
let output = Command::new("patch")
.arg("-p0")
.arg("--context")
.stdin(File::open(&format!("{}/abr.diff", target)).unwrap())
.stdin(File::open(&format!("{target}/abr.diff")).unwrap())
.output()
.unwrap();
if !output.status.success() {
panic!("{:?}", output);
}
assert!(output.status.success(), "{:?}", output);
//println!("{}", String::from_utf8_lossy(&output.stdout));
//println!("{}", String::from_utf8_lossy(&output.stderr));
let alef = fs::read(&format!("{}/alefr", target)).unwrap();
let alef = fs::read(&format!("{target}/alefr")).unwrap();
assert_eq!(alef, bet);
}
}
Expand Down
38 changes: 15 additions & 23 deletions src/ed_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ mod tests {
use super::*;
pub fn diff_w(expected: &[u8], actual: &[u8], filename: &str) -> Result<Vec<u8>, DiffError> {
let mut output = diff(expected, actual)?;
writeln!(&mut output, "w {}", filename).unwrap();
writeln!(&mut output, "w {filename}").unwrap();
Ok(output)
}

Expand Down Expand Up @@ -204,29 +204,26 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff_w(&alef, &bet, &format!("{}/alef", target)).unwrap();
let diff = diff_w(&alef, &bet, &format!("{target}/alef")).unwrap();
File::create("target/ab.ed")
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{}/alef", target)).unwrap();
let mut fa = File::create(&format!("{target}/alef")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{}/bet", target)).unwrap();
let mut fb = File::create(&format!("{target}/bet")).unwrap();
fb.write_all(&bet[..]).unwrap();
let _ = fa;
let _ = fb;
let output = Command::new("ed")
.arg(&format!("{}/alef", target))
.arg(&format!("{target}/alef"))
.stdin(File::open("target/ab.ed").unwrap())
.output()
.unwrap();
if !output.status.success() {
panic!("{:?}", output);
}
assert!(output.status.success(), "{:?}", output);
//println!("{}", String::from_utf8_lossy(&output.stdout));
//println!("{}", String::from_utf8_lossy(&output.stderr));
let alef = fs::read(&format!("{}/alef", target)).unwrap();
let alef = fs::read(&format!("{target}/alef")).unwrap();
assert_eq!(alef, bet);
}
}
Expand Down Expand Up @@ -285,7 +282,7 @@ mod tests {
.unwrap();
let mut fa = File::create("target/alef_").unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{}/bet_", target)).unwrap();
let mut fb = File::create(&format!("{target}/bet_")).unwrap();
fb.write_all(&bet[..]).unwrap();
let _ = fa;
let _ = fb;
Expand All @@ -294,9 +291,7 @@ mod tests {
.stdin(File::open("target/ab_.ed").unwrap())
.output()
.unwrap();
if !output.status.success() {
panic!("{:?}", output);
}
assert!(output.status.success(), "{:?}", output);
//println!("{}", String::from_utf8_lossy(&output.stdout));
//println!("{}", String::from_utf8_lossy(&output.stderr));
let alef = fs::read("target/alef_").unwrap();
Expand Down Expand Up @@ -357,29 +352,26 @@ mod tests {
}
// This test diff is intentionally reversed.
// We want it to turn the alef into bet.
let diff =
diff_w(&alef, &bet, &format!("{}/alefr", target)).unwrap();
let diff = diff_w(&alef, &bet, &format!("{target}/alefr")).unwrap();
File::create("target/abr.ed")
.unwrap()
.write_all(&diff)
.unwrap();
let mut fa = File::create(&format!("{}/alefr", target)).unwrap();
let mut fa = File::create(&format!("{target}/alefr")).unwrap();
fa.write_all(&alef[..]).unwrap();
let mut fb = File::create(&format!("{}/betr", target)).unwrap();
let mut fb = File::create(&format!("{target}/betr")).unwrap();
fb.write_all(&bet[..]).unwrap();
let _ = fa;
let _ = fb;
let output = Command::new("ed")
.arg(&format!("{}/alefr", target))
.arg(&format!("{target}/alefr"))
.stdin(File::open("target/abr.ed").unwrap())
.output()
.unwrap();
if !output.status.success() {
panic!("{:?}", output);
}
assert!(output.status.success(), "{:?}", output);
//println!("{}", String::from_utf8_lossy(&output.stdout));
//println!("{}", String::from_utf8_lossy(&output.stderr));
let alef = fs::read(&format!("{}/alefr", target)).unwrap();
let alef = fs::read(&format!("{target}/alefr")).unwrap();
assert_eq!(alef, bet);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// For the full copyright and license information, please view the LICENSE-*
// files that was distributed with this source code.

use crate::params::*;
use crate::params::{parse_params, Format, Params};
use std::env;

use std::fs;
Expand All @@ -27,13 +27,13 @@ fn main() -> Result<(), String> {
let from_content = match fs::read(&from) {
Ok(from_content) => from_content,
Err(e) => {
return Err(format!("Failed to read from-file: {}", e));
return Err(format!("Failed to read from-file: {e}"));
}
};
let to_content = match fs::read(&to) {
Ok(to_content) => to_content,
Err(e) => {
return Err(format!("Failed to read from-file: {}", e));
return Err(format!("Failed to read from-file: {e}"));
}
};
// run diff
Expand Down
Loading

0 comments on commit d891e10

Please sign in to comment.