Skip to content

Commit cb792fd

Browse files
committed
fix some clippy warnings
1 parent 2217f3d commit cb792fd

File tree

8 files changed

+57
-77
lines changed

8 files changed

+57
-77
lines changed

text/diff.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,11 @@ fn check_difference(args: Args) -> io::Result<DiffExitStatus> {
136136
let path2_is_file = fs::metadata(&path2)?.is_file();
137137

138138
if path1_is_file && path2_is_file {
139-
return FileDiff::file_diff(path1, path2, &format_options, None);
139+
FileDiff::file_diff(path1, path2, &format_options, None)
140140
} else if !path1_is_file && !path2_is_file {
141-
return DirDiff::dir_diff(path1, path2, &format_options, args.recurse);
141+
DirDiff::dir_diff(path1, path2, &format_options, args.recurse)
142142
} else {
143-
return FileDiff::file_dir_diff(path1, path2, &format_options);
143+
FileDiff::file_dir_diff(path1, path2, &format_options)
144144
}
145145
}
146146

text/diff_util/change.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,3 @@ impl Change {
4242
}
4343
}
4444
}
45-
46-
impl PartialEq for Change {
47-
fn eq(&self, other: &Self) -> bool {
48-
match (self, other) {
49-
(Self::None, Self::None) => true,
50-
(Self::Unchanged(_), Self::Unchanged(_)) => true,
51-
(Self::Insert(_), Self::Insert(_)) => true,
52-
(Self::Delete(_), Self::Delete(_)) => true,
53-
(Self::Substitute(_), Self::Substitute(_)) => true,
54-
_ => false,
55-
}
56-
}
57-
}

text/diff_util/constants.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
pub const EXIT_STATUS_NO_DIFFERENCE: u8 = 0;
44
pub const EXIT_STATUS_DIFFERENCE: u8 = 1;
55
pub const EXIT_STATUS_TROUBLE: u8 = 2;
6-
pub const NO_NEW_LINE_AT_END_OF_FILE: &'static str = "\\ No newline at end of file";
7-
pub const COULD_NOT_UNWRAP_FILENAME: &'static str = "Could not unwrap filename!";
6+
pub const NO_NEW_LINE_AT_END_OF_FILE: &str = "\\ No newline at end of file";
7+
pub const COULD_NOT_UNWRAP_FILENAME: &str = "Could not unwrap filename!";
88
pub const UTF8_NOT_ALLOWED_BYTES: [u8; 26] = [
99
0, 1, 2, 3, 4, 5, 6, 11, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 28, 29, 30, 31,
1010
127,

text/diff_util/dir_diff.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'a> DirDiff<'a> {
3838
let mut dir2: DirData = DirData::load(path2)?;
3939

4040
let mut dir_diff = DirDiff::new(&mut dir1, &mut dir2, format_options, recursive);
41-
return dir_diff.analyze();
41+
dir_diff.analyze()
4242
}
4343

4444
fn analyze(&mut self) -> io::Result<DiffExitStatus> {
@@ -48,16 +48,11 @@ impl<'a> DirDiff<'a> {
4848
let is_file = dir_data
4949
.files()
5050
.get_key_value(file_name)
51-
.expect(
52-
format!(
53-
"Could not find file in {}",
51+
.unwrap_or_else(|| panic!("Could not find file in {}",
5452
dir_data
55-
.path()
56-
.to_str()
57-
.unwrap_or(COULD_NOT_UNWRAP_FILENAME)
58-
)
59-
.as_str(),
60-
)
53+
.path()
54+
.to_str()
55+
.unwrap_or(COULD_NOT_UNWRAP_FILENAME)))
6156
.1
6257
.file_type()?
6358
.is_file();
@@ -217,6 +212,6 @@ impl<'a> DirDiff<'a> {
217212
}
218213
}
219214

220-
return Ok(exit_status);
215+
Ok(exit_status)
221216
}
222217
}

text/diff_util/file_data.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'a> FileData<'a> {
3939
}
4040

4141
pub fn line(&self, index: usize) -> &str {
42-
&self.lines[index]
42+
self.lines[index]
4343
}
4444

4545
pub fn modified(&self) -> SystemTime {
@@ -53,7 +53,7 @@ impl<'a> FileData<'a> {
5353
}
5454
}
5555

56-
return COULD_NOT_UNWRAP_FILENAME;
56+
COULD_NOT_UNWRAP_FILENAME
5757
}
5858

5959
pub fn path(&self) -> &str {

text/diff_util/file_diff.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ impl<'a> FileDiff<'a> {
5252
show_if_different: Option<String>,
5353
) -> io::Result<DiffExitStatus> {
5454
if is_binary(&path1)? || is_binary(&path2)? {
55-
return Self::binary_file_diff(&path1, &path2);
55+
Self::binary_file_diff(&path1, &path2)
5656
} else {
5757
let content1 = read_to_string(&path1)?.into_bytes();
5858
let linereader1 = LineReader::new(&content1);
@@ -79,8 +79,8 @@ impl<'a> FileDiff<'a> {
7979
let num_lines1 = diff.file1.lines().len();
8080
let num_lines2 = diff.file2.lines().len();
8181
FileDiff::histogram_lcs(
82-
&diff.file1,
83-
&diff.file2,
82+
diff.file1,
83+
diff.file2,
8484
0,
8585
num_lines1,
8686
0,
@@ -97,7 +97,7 @@ impl<'a> FileDiff<'a> {
9797
}
9898
}
9999

100-
return diff.print();
100+
diff.print()
101101
}
102102
}
103103

@@ -110,24 +110,24 @@ impl<'a> FileDiff<'a> {
110110

111111
if path1_file_type.is_file() {
112112
let path1_file = path1.clone();
113-
let path1_file = path1_file.file_name().expect(&COULD_NOT_UNWRAP_FILENAME);
113+
let path1_file = path1_file.file_name().expect(COULD_NOT_UNWRAP_FILENAME);
114114
let path2 = path2.join(path1_file);
115115

116116
if !check_existance(&path2)? {
117117
return Ok(DiffExitStatus::Trouble);
118118
}
119119

120-
return FileDiff::file_diff(path1, path2, format_options, None);
120+
FileDiff::file_diff(path1, path2, format_options, None)
121121
} else {
122122
let path2_file = path2.clone();
123-
let path2_file = path2_file.file_name().expect(&COULD_NOT_UNWRAP_FILENAME);
123+
let path2_file = path2_file.file_name().expect(COULD_NOT_UNWRAP_FILENAME);
124124
let path1 = path1.join(path2_file);
125125

126126
if !check_existance(&path1)? {
127127
return Ok(DiffExitStatus::Trouble);
128128
}
129129

130-
return FileDiff::file_diff(path1, path2, format_options, None);
130+
FileDiff::file_diff(path1, path2, format_options, None)
131131
}
132132
}
133133

@@ -173,22 +173,22 @@ impl<'a> FileDiff<'a> {
173173
for hunk_index in 0..hunks_count {
174174
let hunk = self.hunks.hunk_at_mut(hunk_index);
175175
match self.format_options.output_format {
176-
OutputFormat::Debug => hunk.print_debug(&self.file1, &self.file2),
176+
OutputFormat::Debug => hunk.print_debug(self.file1, self.file2),
177177
OutputFormat::Default => {
178-
hunk.print_default(&self.file1, &self.file2, hunk_index == hunks_count - 1)
178+
hunk.print_default(self.file1, self.file2, hunk_index == hunks_count - 1)
179179
}
180180
OutputFormat::EditScript => hunk.print_edit_script(
181-
&self.file1,
182-
&self.file2,
181+
self.file1,
182+
self.file2,
183183
hunk_index == hunks_count - 1,
184184
),
185185
OutputFormat::Context(_) => {
186186
eprintln!("OutputFormat::Context should be handled in other place");
187187
return Ok(DiffExitStatus::Trouble);
188188
}
189189
OutputFormat::ForwardEditScript => hunk.print_forward_edit_script(
190-
&self.file1,
191-
&self.file2,
190+
self.file1,
191+
self.file2,
192192
hunk_index == hunks_count - 1,
193193
),
194194
OutputFormat::Unified(_) => {
@@ -200,9 +200,9 @@ impl<'a> FileDiff<'a> {
200200
}
201201

202202
if self.are_different() {
203-
return Ok(DiffExitStatus::Different);
203+
Ok(DiffExitStatus::Different)
204204
} else {
205-
return Ok(DiffExitStatus::NotDifferent);
205+
Ok(DiffExitStatus::NotDifferent)
206206
}
207207
}
208208

@@ -269,9 +269,7 @@ impl<'a> FileDiff<'a> {
269269
.map(|(k, _v)| *k);
270270

271271
match key {
272-
None => {
273-
return;
274-
}
272+
None => {},
275273
Some(k) => {
276274
let rec = hist.get(k).unwrap();
277275
let x1_new = rec[1] as usize;
@@ -308,11 +306,11 @@ impl<'a> FileDiff<'a> {
308306
fn print_context(&mut self, context: usize) {
309307
println!(
310308
"*** {}",
311-
Self::get_header(self.file1, &self.format_options.label1())
309+
Self::get_header(self.file1, self.format_options.label1())
312310
);
313311
println!(
314312
"--- {}",
315-
Self::get_header(self.file2, &self.format_options.label2())
313+
Self::get_header(self.file2, self.format_options.label2())
316314
);
317315

318316
for hunk in self.hunks.hunks() {
@@ -384,11 +382,11 @@ impl<'a> FileDiff<'a> {
384382
fn print_unified(&mut self, unified: usize) -> Result<(), std::fmt::Error> {
385383
println!(
386384
"--- {}",
387-
Self::get_header(self.file1, &self.format_options.label1())
385+
Self::get_header(self.file1, self.format_options.label1())
388386
);
389387
println!(
390388
"+++ {}",
391-
Self::get_header(self.file2, &self.format_options.label2())
389+
Self::get_header(self.file2, self.format_options.label2())
392390
);
393391

394392
let mut diff_disp = DiffDisplay::new();
@@ -448,13 +446,13 @@ impl<'a> FileDiff<'a> {
448446

449447
pub fn get_header(file: &FileData, label: &Option<String>) -> String {
450448
if let Some(label) = label {
451-
return format!("{}", label);
449+
label.to_string()
452450
} else {
453-
return format!(
451+
format!(
454452
"{}\t{}",
455453
file.path(),
456454
system_time_to_rfc2822(file.modified())
457-
);
455+
)
458456
}
459457
}
460458
}

text/diff_util/functions.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use chrono::{DateTime, Local};
22
use std::{
33
fs::File,
44
io::{self, Read},
5-
path::PathBuf,
5+
path::{Path, PathBuf},
66
time::SystemTime,
77
};
88

@@ -18,8 +18,8 @@ pub fn is_binary(file_path: &PathBuf) -> io::Result<bool> {
1818
let mut buffer = [0; 1024];
1919

2020
if let Ok(count) = file.read(&mut buffer) {
21-
for i in 0..count {
22-
if UTF8_NOT_ALLOWED_BYTES.contains(&buffer[i]) {
21+
for buf_item in buffer.iter().take(count) {
22+
if UTF8_NOT_ALLOWED_BYTES.contains(buf_item) {
2323
return Ok(true);
2424
}
2525
}
@@ -28,8 +28,8 @@ pub fn is_binary(file_path: &PathBuf) -> io::Result<bool> {
2828
Ok(false)
2929
}
3030

31-
pub fn check_existance(path_buf: &PathBuf) -> io::Result<bool> {
32-
if path_buf.exists() == false {
31+
pub fn check_existance(path_buf: &Path) -> io::Result<bool> {
32+
if !path_buf.exists() {
3333
println!(
3434
"diff: {}: No such file or directory",
3535
path_buf.to_str().unwrap_or(COULD_NOT_UNWRAP_FILENAME)

text/diff_util/hunks.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl Hunk {
8787
println!("< {}", file1.line(i));
8888
}
8989

90-
if is_last && file1.ends_with_newline() == false {
90+
if is_last && !file1.ends_with_newline() {
9191
println!("{}", NO_NEW_LINE_AT_END_OF_FILE);
9292
}
9393
}
@@ -188,15 +188,15 @@ impl Hunk {
188188
}
189189
}
190190

191-
if is_last && file1.ends_with_newline() == false {
191+
if is_last && !file1.ends_with_newline() {
192192
println!(
193193
"diff: {}:{}\n",
194194
file1.name(),
195195
&NO_NEW_LINE_AT_END_OF_FILE[1..]
196196
);
197197
}
198198

199-
if is_last && file2.ends_with_newline() == false {
199+
if is_last && !file2.ends_with_newline() {
200200
println!(
201201
"diff: {}:{}\n",
202202
file2.name(),
@@ -234,15 +234,15 @@ impl Hunk {
234234
}
235235
}
236236

237-
if is_last && file1.ends_with_newline() == false {
237+
if is_last && !file1.ends_with_newline() {
238238
println!(
239239
"diff: {}:{}\n",
240240
file1.name(),
241241
&NO_NEW_LINE_AT_END_OF_FILE[1..]
242242
);
243243
}
244244

245-
if is_last && file2.ends_with_newline() == false {
245+
if is_last && !file2.ends_with_newline() {
246246
println!(
247247
"diff: {}:{}\n",
248248
file2.name(),
@@ -259,7 +259,7 @@ pub struct Hunks {
259259
impl Hunks {
260260
pub fn new() -> Self {
261261
Self {
262-
hunks: vec![Hunk::new(); 0],
262+
hunks: { Hunk::new(); vec![] as Vec<Hunk>},
263263
}
264264
}
265265

@@ -277,40 +277,40 @@ impl Hunks {
277277

278278
pub fn create_hunks_from_lcs(
279279
&mut self,
280-
lcs_indices: &Vec<i32>,
280+
lcs_indices: &[i32],
281281
num_lines1: usize,
282282
num_lines2: usize,
283283
) {
284284
let mut hunk_start1 = 0;
285285
let mut hunk_end1: usize;
286286
let mut hunk_start2 = 0;
287287
let mut hunk_end2: usize;
288-
let mut prev_val = -2 as i32;
289-
for i in 0..lcs_indices.len() {
290-
if (lcs_indices[i] == -1) && (prev_val != -1) {
288+
let mut prev_val = -2_i32;
289+
for (i, lcs_index) in lcs_indices.iter().enumerate() {
290+
if (lcs_index == &-1) && (prev_val != -1) {
291291
// We reach a new deletion/substitution block
292292
hunk_start1 = i;
293293
hunk_start2 = if prev_val == -2 {
294294
0
295295
} else {
296296
(prev_val + 1) as usize
297297
};
298-
} else if (i != 0) && (prev_val != -1) && (lcs_indices[i] != -1) && (lcs_indices[i] != prev_val + 1) {
298+
} else if (i != 0) && (prev_val != -1) && (lcs_index != &-1) && (lcs_index != &(prev_val + 1)) {
299299
// there was an insertion (but no deletion)
300300
// no -1 values but a bump in the values, eg [136, 145]
301301
hunk_start1 = i;
302302
hunk_start2 = (prev_val + 1) as usize;
303303
hunk_end1 = i;
304-
hunk_end2 = lcs_indices[i] as usize;
304+
hunk_end2 = *lcs_index as usize;
305305
self.add_hunk(hunk_start1, hunk_end1, hunk_start2, hunk_end2);
306306
}
307-
if (lcs_indices[i] != -1) && (prev_val == -1) {
307+
if (lcs_index != &-1) && (prev_val == -1) {
308308
// we reach the end of deletion/substitution block
309309
hunk_end1 = i;
310-
hunk_end2 = lcs_indices[i] as usize;
310+
hunk_end2 = *lcs_index as usize;
311311
self.add_hunk(hunk_start1, hunk_end1, hunk_start2, hunk_end2);
312312
}
313-
prev_val = lcs_indices[i];
313+
prev_val = *lcs_index;
314314
}
315315

316316
// final hunk: we might have only -1 at the end

0 commit comments

Comments
 (0)