Apply all Clippy lints that don't break the API

This commit is contained in:
nmlgc
2018-07-02 20:09:51 +02:00
parent 282c139cc0
commit de82e46021
4 changed files with 73 additions and 78 deletions
+32 -32
View File
@@ -2,6 +2,7 @@ use sequencematcher::SequenceMatcher;
use std::cmp; use std::cmp;
use utils::{count_leading, str_with_similar_chars}; use utils::{count_leading, str_with_similar_chars};
#[derive(Default)]
pub struct Differ { pub struct Differ {
pub line_junk: Option<fn(&str) -> bool>, pub line_junk: Option<fn(&str) -> bool>,
pub char_junk: Option<fn(&str) -> bool>, pub char_junk: Option<fn(&str) -> bool>,
@@ -53,9 +54,8 @@ impl Differ {
fn dump(&self, tag: &str, sequence: &[&str], start: usize, end: usize) -> Vec<String> { fn dump(&self, tag: &str, sequence: &[&str], start: usize, end: usize) -> Vec<String> {
let mut res = Vec::new(); let mut res = Vec::new();
for i in start..end { for i in start..end {
match sequence.get(i) { if let Some(s) = sequence.get(i) {
Some(s) => res.push(format!("{} {}", tag, s)), res.push(format!("{} {}", tag, s))
None => {}
} }
} }
res res
@@ -73,15 +73,17 @@ impl Differ {
if !(first_start < first_end && second_start < second_end) { if !(first_start < first_end && second_start < second_end) {
return Vec::new(); return Vec::new();
} }
let mut first; let (mut first, second) = if second_end - second_start < first_end - first_start {
let second; (
if second_end - second_start < first_end - first_start { self.dump("+", second_sequence, second_start, second_end),
first = self.dump("+", second_sequence, second_start, second_end); self.dump("-", first_sequence, first_start, first_end),
second = self.dump("-", first_sequence, first_start, first_end); )
} else { } else {
first = self.dump("-", first_sequence, first_start, first_end); (
second = self.dump("+", second_sequence, second_start, second_end); self.dump("-", first_sequence, first_start, first_end),
} self.dump("+", second_sequence, second_start, second_end),
)
};
for s in second { for s in second {
first.push(s); first.push(s);
} }
@@ -100,13 +102,20 @@ impl Differ {
let mut res = Vec::new(); let mut res = Vec::new();
let (mut best_ratio, cutoff) = (0.74, 0.75); let (mut best_ratio, cutoff) = (0.74, 0.75);
let (mut best_i, mut best_j) = (0, 0); let (mut best_i, mut best_j) = (0, 0);
let (mut second_sequence_str, mut first_sequence_str);
let mut eqi: Option<usize> = None; let mut eqi: Option<usize> = None;
let mut eqj: Option<usize> = None; let mut eqj: Option<usize> = None;
for j in second_start..second_end { for (j, second_sequence_str) in second_sequence
second_sequence_str = &second_sequence[j]; .iter()
for i in first_start..first_end { .enumerate()
first_sequence_str = &first_sequence[i]; .take(second_end)
.skip(second_start)
{
for (i, first_sequence_str) in first_sequence
.iter()
.enumerate()
.take(second_end)
.skip(second_start)
{
if first_sequence_str == second_sequence_str { if first_sequence_str == second_sequence_str {
if eqi.is_none() { if eqi.is_none() {
eqi = Some(i); eqi = Some(i);
@@ -254,40 +263,31 @@ impl Differ {
common = cmp::min(common, count_leading(first_tags.split_at(common).0, ' ')); common = cmp::min(common, count_leading(first_tags.split_at(common).0, ' '));
first_tags = first_tags.split_at(common).1.trim_right(); first_tags = first_tags.split_at(common).1.trim_right();
second_tags = second_tags.split_at(common).1.trim_right(); second_tags = second_tags.split_at(common).1.trim_right();
let mut s = String::from(format!("- {}", first_line)); let mut s = format!("- {}", first_line);
res.push(s); res.push(s);
if first_tags != "" { if first_tags != "" {
s = String::from(format!( s = format!("? {}{}\n", str_with_similar_chars('\t', common), first_tags);
"? {}{}\n",
str_with_similar_chars('\t', common),
first_tags
));
res.push(s); res.push(s);
} }
s = String::from(format!("+ {}", second_line)); s = format!("+ {}", second_line);
res.push(s); res.push(s);
if second_tags != "" { if second_tags != "" {
s = String::from(format!( s = format!(
"? {}{}\n", "? {}{}\n",
str_with_similar_chars('\t', common), str_with_similar_chars('\t', common),
second_tags second_tags
)); );
res.push(s); res.push(s);
} }
res res
} }
pub fn restore(delta: &Vec<String>, which: usize) -> Vec<String> { pub fn restore(delta: &[String], which: usize) -> Vec<String> {
if !(which == 1 || which == 2) { if !(which == 1 || which == 2) {
panic!("Second parameter must be 1 or 2"); panic!("Second parameter must be 1 or 2");
} }
let mut res = Vec::new(); let mut res = Vec::new();
let tag; let tag = if which == 1 { "- " } else { "+ " }.to_string();
if which == 1 {
tag = "- ".to_string();
} else {
tag = "+ ".to_string();
}
let prefixes = vec![tag, " ".to_string()]; let prefixes = vec![tag, " ".to_string()];
for line in delta { for line in delta {
for prefix in &prefixes { for prefix in &prefixes {
+2 -2
View File
@@ -122,7 +122,7 @@ pub fn context_diff<T: Sequence>(
for i in opcode.first_start..opcode.first_end { for i in opcode.first_start..opcode.first_end {
res.push(format!( res.push(format!(
"{}{}", "{}{}",
prefix.get(&opcode.tag).unwrap(), &prefix[&opcode.tag],
first_sequence.at_index(i).unwrap() first_sequence.at_index(i).unwrap()
)); ));
} }
@@ -144,7 +144,7 @@ pub fn context_diff<T: Sequence>(
for i in opcode.second_start..opcode.second_end { for i in opcode.second_start..opcode.second_end {
res.push(format!( res.push(format!(
"{}{}", "{}{}",
prefix.get(&opcode.tag).unwrap(), &prefix[&opcode.tag],
second_sequence.at_index(i).unwrap() second_sequence.at_index(i).unwrap()
)); ));
} }
+37 -42
View File
@@ -13,9 +13,9 @@ pub struct Match {
impl Match { impl Match {
fn new(first_start: usize, second_start: usize, size: usize) -> Match { fn new(first_start: usize, second_start: usize, size: usize) -> Match {
Match { Match {
first_start: first_start, first_start,
second_start: second_start, second_start,
size: size, size,
} }
} }
} }
@@ -38,11 +38,11 @@ impl Opcode {
second_end: usize, second_end: usize,
) -> Opcode { ) -> Opcode {
Opcode { Opcode {
tag: tag, tag,
first_start: first_start, first_start,
first_end: first_end, first_end,
second_start: second_start, second_start,
second_end: second_end, second_end,
} }
} }
} }
@@ -129,8 +129,8 @@ pub struct SequenceMatcher<'a, T: 'a + ?Sized + Sequence> {
impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> { impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
pub fn new(first_sequence: &'a T, second_sequence: &'a T) -> SequenceMatcher<'a, T> { pub fn new(first_sequence: &'a T, second_sequence: &'a T) -> SequenceMatcher<'a, T> {
let mut matcher = SequenceMatcher { let mut matcher = SequenceMatcher {
first_sequence: first_sequence, first_sequence,
second_sequence: second_sequence, second_sequence,
matching_blocks: None, matching_blocks: None,
opcodes: None, opcodes: None,
is_junk: None, is_junk: None,
@@ -169,7 +169,7 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
for i in 0..second_sequence.len() { for i in 0..second_sequence.len() {
let mut counter = second_sequence_elements let mut counter = second_sequence_elements
.entry(second_sequence.at_index(i).unwrap()) .entry(second_sequence.at_index(i).unwrap())
.or_insert(Vec::new()); .or_insert_with(Vec::new);
counter.push(i); counter.push(i);
} }
if let Some(junk_func) = self.is_junk { if let Some(junk_func) = self.is_junk {
@@ -204,35 +204,30 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
let mut j2len: HashMap<usize, usize> = HashMap::new(); let mut j2len: HashMap<usize, usize> = HashMap::new();
for i in first_start..first_end { for i in first_start..first_end {
let mut new_j2len: HashMap<usize, usize> = HashMap::new(); let mut new_j2len: HashMap<usize, usize> = HashMap::new();
match second_sequence_elements.get(first_sequence.at_index(i).unwrap()) { if let Some(indexes) = second_sequence_elements.get(first_sequence.at_index(i).unwrap())
Some(indexes) => { {
for j in indexes { for j in indexes {
let j = j.clone(); let j = *j;
if j < second_start { if j < second_start {
continue; continue;
}; };
if j >= second_end { if j >= second_end {
break; break;
}; };
let mut size = 0; let mut size = 0;
if j > 0 { if j > 0 {
match j2len.get(&(j - 1)) { if let Some(k) = j2len.get(&(j - 1)) {
Some(k) => { size = *k;
size = k.clone();
}
None => {}
}
}
size += 1;
new_j2len.insert(j, size);
if size > best_size {
best_i = i + 1 - size;
best_j = j + 1 - size;
best_size = size;
} }
} }
size += 1;
new_j2len.insert(j, size);
if size > best_size {
best_i = i + 1 - size;
best_j = j + 1 - size;
best_size = size;
}
} }
None => {}
} }
j2len = new_j2len; j2len = new_j2len;
} }
@@ -241,9 +236,9 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
&& best_j > second_start && best_j > second_start
&& first_sequence.at_index(best_i - 1) == second_sequence.at_index(best_j - 1) && first_sequence.at_index(best_i - 1) == second_sequence.at_index(best_j - 1)
{ {
best_i = best_i - 1; best_i -= 1;
best_j = best_j - 1; best_j -= 1;
best_size = best_size + 1; best_size += 1;
} }
while best_i + best_size < first_end && best_j + best_size < second_end while best_i + best_size < first_end && best_j + best_size < second_end
&& first_sequence.at_index(best_i + best_size) && first_sequence.at_index(best_i + best_size)
@@ -337,7 +332,7 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
} }
} }
self.opcodes = Some(opcodes); self.opcodes = Some(opcodes);
return self.opcodes.as_ref().unwrap().clone(); self.opcodes.as_ref().unwrap().clone()
} }
pub fn get_grouped_opcodes(&mut self, n: usize) -> Vec<Vec<Opcode>> { pub fn get_grouped_opcodes(&mut self, n: usize) -> Vec<Vec<Opcode>> {
@@ -382,7 +377,7 @@ impl<'a, T: ?Sized + Sequence> SequenceMatcher<'a, T> {
code.second_end, code.second_end,
)); ));
} }
if !group.is_empty() && !(group.len() == 1 && group.first().unwrap().tag == "equal") { if !(group.len() == 1 && group.first().unwrap().tag == "equal") || group.is_empty() {
res.push(group.clone()); res.push(group.clone());
} }
res res
+2 -2
View File
@@ -2,7 +2,7 @@ pub fn calculate_ratio(matches: usize, length: usize) -> f32 {
if length != 0 { if length != 0 {
return 2.0 * matches as f32 / length as f32; return 2.0 * matches as f32 / length as f32;
} }
return 1.0; 1.0
} }
pub fn str_with_similar_chars(c: char, length: usize) -> String { pub fn str_with_similar_chars(c: char, length: usize) -> String {
@@ -19,7 +19,7 @@ pub fn count_leading(line: &str, c: char) -> usize {
while (i < n) && line[i] == c { while (i < n) && line[i] == c {
i += 1; i += 1;
} }
return i; i
} }
pub fn format_range_unified(start: usize, end: usize) -> String { pub fn format_range_unified(start: usize, end: usize) -> String {