Avoid `res` locals in favour of more meaningful names

pull/579/head^2
Wilfred Hughes 2023-11-28 13:27:27 +07:00
parent 2adb2f8531
commit cac80e992a
13 changed files with 103 additions and 103 deletions

@ -491,7 +491,7 @@ pub(crate) fn set_neighbours<'s, 'b>(
}
// There are only seven pushes in this function, so that's sufficient.
let mut res: Vec<(Edge, &Vertex)> = Vec::with_capacity(7);
let mut neighbours: Vec<(Edge, &Vertex)> = Vec::with_capacity(7);
if let (Some(lhs_syntax), Some(rhs_syntax)) = (&v.lhs_syntax, &v.rhs_syntax) {
if lhs_syntax == rhs_syntax {
@ -510,7 +510,7 @@ pub(crate) fn set_neighbours<'s, 'b>(
&v.parents,
);
res.push((
neighbours.push((
UnchangedNode {
depth_difference,
probably_punctuation,
@ -569,7 +569,7 @@ pub(crate) fn set_neighbours<'s, 'b>(
&parents_next,
);
res.push((
neighbours.push((
EnterUnchangedDelimiter { depth_difference },
allocate_if_new(
Vertex {
@ -620,7 +620,7 @@ pub(crate) fn set_neighbours<'s, 'b>(
v.rhs_parent_id,
&v.parents,
);
res.push((
neighbours.push((
edge,
allocate_if_new(
Vertex {
@ -653,7 +653,7 @@ pub(crate) fn set_neighbours<'s, 'b>(
&v.parents,
);
res.push((
neighbours.push((
NovelAtomLHS {},
allocate_if_new(
Vertex {
@ -685,7 +685,7 @@ pub(crate) fn set_neighbours<'s, 'b>(
&parents_next,
);
res.push((
neighbours.push((
EnterNovelDelimiterLHS {},
allocate_if_new(
Vertex {
@ -718,7 +718,7 @@ pub(crate) fn set_neighbours<'s, 'b>(
&v.parents,
);
res.push((
neighbours.push((
NovelAtomRHS {},
allocate_if_new(
Vertex {
@ -749,7 +749,7 @@ pub(crate) fn set_neighbours<'s, 'b>(
&parents_next,
);
res.push((
neighbours.push((
EnterNovelDelimiterRHS {},
allocate_if_new(
Vertex {
@ -769,11 +769,11 @@ pub(crate) fn set_neighbours<'s, 'b>(
}
}
assert!(
!res.is_empty(),
!neighbours.is_empty(),
"Must always find some next steps if node is not the end"
);
v.neighbours.replace(Some(res));
v.neighbours.replace(Some(neighbours));
}
pub(crate) fn populate_change_map<'s, 'b>(

@ -41,13 +41,13 @@ impl<T> Stack<T> {
// O(n)
pub(crate) fn size(&self) -> usize {
let mut res = 0;
let mut count = 0;
let mut node = &self.head;
while let Some(next) = node {
res += 1;
count += 1;
node = &next.next;
}
res
count
}
pub(crate) fn is_empty(&self) -> bool {

@ -20,14 +20,14 @@ pub(crate) fn mark_unchanged<'a>(
) -> Vec<(Vec<&'a Syntax<'a>>, Vec<&'a Syntax<'a>>)> {
let (_, lhs_nodes, rhs_nodes) = shrink_unchanged_at_ends(lhs_nodes, rhs_nodes, change_map);
let mut res = vec![];
let mut nodes_to_diff = vec![];
for (lhs_nodes, rhs_nodes) in split_mostly_unchanged_toplevel(&lhs_nodes, &rhs_nodes) {
let (_, lhs_nodes, rhs_nodes) =
shrink_unchanged_at_ends(&lhs_nodes, &rhs_nodes, change_map);
res.extend(split_unchanged(&lhs_nodes, &rhs_nodes, change_map));
nodes_to_diff.extend(split_unchanged(&lhs_nodes, &rhs_nodes, change_map));
}
res
nodes_to_diff
}
#[derive(Debug)]

@ -128,9 +128,9 @@ fn all_lines(mps: &[MatchedPos]) -> Vec<LineNumber> {
for mp in mps {
lines.insert(mp.pos.line);
}
let mut res: Vec<LineNumber> = lines.into_iter().collect();
res.sort_unstable();
res
let mut line_nums: Vec<LineNumber> = lines.into_iter().collect();
line_nums.sort_unstable();
line_nums
}
fn matched_lines_from_unchanged(
@ -418,7 +418,7 @@ fn before_with_opposites(
}
fn pad_before(ln: LineNumber, num_context_lines: usize) -> Vec<LineNumber> {
let mut res = vec![];
let mut line_nums = vec![];
let mut current = ln;
// Use one more line than num_context_lines so we merge
@ -429,15 +429,15 @@ fn pad_before(ln: LineNumber, num_context_lines: usize) -> Vec<LineNumber> {
}
current = (current.0 - 1).into();
res.push(current);
line_nums.push(current);
}
res.reverse();
res
line_nums.reverse();
line_nums
}
fn pad_after(ln: LineNumber, max_line: LineNumber, num_context_lines: usize) -> Vec<LineNumber> {
let mut res = vec![];
let mut line_nums = vec![];
let mut current = ln;
// Use one more line than num_context_lines so we merge
@ -448,10 +448,10 @@ fn pad_after(ln: LineNumber, max_line: LineNumber, num_context_lines: usize) ->
}
current = (current.0 + 1).into();
res.push(current);
line_nums.push(current);
}
res
line_nums
}
pub(crate) fn flip_tuple<Tx: Copy, Ty: Copy>(pair: (Tx, Ty)) -> (Ty, Tx) {

@ -138,7 +138,7 @@ pub(crate) fn merge_adjacent(
max_rhs_src_line: LineNumber,
num_context_lines: usize,
) -> Vec<Hunk> {
let mut res: Vec<Hunk> = vec![];
let mut merged_hunks: Vec<Hunk> = vec![];
let mut prev_hunk: Option<Hunk> = None;
let mut prev_lhs_lines: HashSet<LineNumber> = HashSet::new();
@ -171,7 +171,7 @@ pub(crate) fn merge_adjacent(
if lhs_lines.is_disjoint(&prev_lhs_lines) && rhs_lines.is_disjoint(&prev_rhs_lines)
{
// No overlaps, start a new hunk.
res.push(hunk_so_far.clone());
merged_hunks.push(hunk_so_far.clone());
prev_hunk = Some(hunk.clone());
prev_lhs_lines = lhs_lines;
@ -193,10 +193,10 @@ pub(crate) fn merge_adjacent(
}
if let Some(current_hunk) = prev_hunk {
res.push(current_hunk);
merged_hunks.push(current_hunk);
}
res
merged_hunks
}
fn lines_are_close(
@ -227,7 +227,7 @@ fn lines_are_close(
fn enforce_increasing(
lines: &[(Option<LineNumber>, Option<LineNumber>)],
) -> Vec<(Option<LineNumber>, Option<LineNumber>)> {
let mut res = vec![];
let mut ordered_lines = vec![];
let mut max_lhs_line: Option<LineNumber> = None;
let mut max_rhs_line: Option<LineNumber> = None;
@ -264,11 +264,11 @@ fn enforce_increasing(
}
if lhs_line.is_some() || rhs_line.is_some() {
res.push((lhs_line, rhs_line));
ordered_lines.push((lhs_line, rhs_line));
}
}
res
ordered_lines
}
fn find_novel_lines(

@ -76,7 +76,7 @@ fn display_single_column(
) -> Vec<String> {
let column_width = format_line_num((src_lines.len() as u32).into()).len();
let mut result = Vec::with_capacity(src_lines.len());
let mut formatted_lines = Vec::with_capacity(src_lines.len());
let mut header_line = String::new();
header_line.push_str(&style::header(
@ -88,7 +88,7 @@ fn display_single_column(
display_options,
));
header_line.push('\n');
result.push(header_line);
formatted_lines.push(header_line);
let mut style = Style::new();
if display_options.use_color {
@ -103,10 +103,10 @@ fn display_single_column(
.to_string(),
);
formatted_line.push_str(line);
result.push(formatted_line);
formatted_lines.push(formatted_line);
}
result
formatted_lines
}
fn display_line_nums(

@ -88,7 +88,7 @@ fn width_respecting_tabs(s: &str, tab_width: usize) -> usize {
/// split_string_by_width("fooba", 3) // vec![("foo", 0), ("ba", 1)]
/// ```
fn split_string_by_width(s: &str, max_width: usize, tab_width: usize) -> Vec<(&str, usize)> {
let mut res: Vec<(&str, usize)> = vec![];
let mut parts: Vec<(&str, usize)> = vec![];
let mut s = s;
while width_respecting_tabs(s, tab_width) > max_width {
@ -103,14 +103,14 @@ fn split_string_by_width(s: &str, max_width: usize, tab_width: usize) -> Vec<(&s
} else {
0
};
res.push((part, padding));
parts.push((part, padding));
}
if res.is_empty() || !s.is_empty() {
res.push((s, max_width - width_respecting_tabs(s, tab_width)));
if parts.is_empty() || !s.is_empty() {
parts.push((s, max_width - width_respecting_tabs(s, tab_width)));
}
res
parts
}
/// Return a copy of `src` with all the tab characters replaced by
@ -147,13 +147,13 @@ pub(crate) fn split_and_apply(
.map(|(part, pad)| {
let part = replace_tabs(part, tab_width);
let mut res = String::with_capacity(part.len() + pad);
res.push_str(&part);
let mut parts = String::with_capacity(part.len() + pad);
parts.push_str(&part);
if matches!(side, Side::Left) {
res.push_str(&" ".repeat(pad));
parts.push_str(&" ".repeat(pad));
}
res
parts
})
.collect();
}
@ -230,7 +230,7 @@ pub(crate) fn split_and_apply(
/// specified.
fn apply_line(line: &str, styles: &[(SingleLineSpan, Style)]) -> String {
let line_bytes = byte_len(line);
let mut res = String::with_capacity(line.len());
let mut styled_line = String::with_capacity(line.len());
let mut i = 0;
for (span, style) in styles {
let start_col = span.start_col as usize;
@ -244,21 +244,21 @@ fn apply_line(line: &str, styles: &[(SingleLineSpan, Style)]) -> String {
// Unstyled text before the next span.
if i < start_col {
res.push_str(substring_by_byte(line, i, start_col));
styled_line.push_str(substring_by_byte(line, i, start_col));
}
// Apply style to the substring in this span.
let span_s = substring_by_byte(line, start_col, min(line_bytes, end_col));
res.push_str(&span_s.style(*style).to_string());
styled_line.push_str(&span_s.style(*style).to_string());
i = end_col;
}
// Unstyled text after the last span.
if i < line_bytes {
let span_s = substring_by_byte(line, i, line_bytes);
res.push_str(span_s);
styled_line.push_str(span_s);
}
res
styled_line
}
fn group_by_line(
@ -283,7 +283,7 @@ fn group_by_line(
fn style_lines(lines: &[&str], styles: &[(SingleLineSpan, Style)]) -> Vec<String> {
let mut ranges_by_line = group_by_line(styles);
let mut res = Vec::with_capacity(lines.len());
let mut styled_lines = Vec::with_capacity(lines.len());
for (i, line) in lines.iter().enumerate() {
let mut styled_line = String::with_capacity(line.len());
let ranges = ranges_by_line
@ -292,9 +292,9 @@ fn style_lines(lines: &[&str], styles: &[(SingleLineSpan, Style)]) -> Vec<String
styled_line.push_str(&apply_line(line, &ranges));
styled_line.push('\n');
res.push(styled_line);
styled_lines.push(styled_line);
}
res
styled_lines
}
pub(crate) fn novel_style(style: Style, side: Side, background: BackgroundColor) -> Style {

@ -253,7 +253,7 @@ pub(crate) fn relative_paths_in_either(lhs_dir: &Path, rhs_dir: &Path) -> Vec<Pa
let rhs_paths = relative_file_paths_in_dir(rhs_dir);
let mut seen = FxHashSet::default();
let mut res: Vec<PathBuf> = vec![];
let mut paths: Vec<PathBuf> = vec![];
let mut i = 0;
let mut j = 0;
@ -264,7 +264,7 @@ pub(crate) fn relative_paths_in_either(lhs_dir: &Path, rhs_dir: &Path) -> Vec<Pa
if !seen.contains(lhs_path) {
// It should be impossible to get duplicates, but
// be defensive.
res.push(lhs_path.clone());
paths.push(lhs_path.clone());
seen.insert(lhs_path);
}
@ -277,8 +277,8 @@ pub(crate) fn relative_paths_in_either(lhs_dir: &Path, rhs_dir: &Path) -> Vec<Pa
} else if seen.contains(rhs_path) {
j += 1;
} else {
res.push(lhs_path.clone());
res.push(rhs_path.clone());
paths.push(lhs_path.clone());
paths.push(rhs_path.clone());
seen.insert(lhs_path);
seen.insert(rhs_path);
@ -291,10 +291,10 @@ pub(crate) fn relative_paths_in_either(lhs_dir: &Path, rhs_dir: &Path) -> Vec<Pa
}
}
res.extend(lhs_paths.into_iter().skip(i));
res.extend(rhs_paths.into_iter().skip(j));
paths.extend(lhs_paths.into_iter().skip(i));
paths.extend(rhs_paths.into_iter().skip(j));
res
paths
}
#[cfg(test)]

@ -16,17 +16,17 @@ fn split_lines_keep_newline(s: &str) -> Vec<&str> {
}
let mut offset = 0;
let mut res = vec![];
let mut lines = vec![];
for newline_match in NEWLINE_RE.find_iter(s) {
res.push(s[offset..newline_match.end()].into());
lines.push(s[offset..newline_match.end()].into());
offset = newline_match.end();
}
if offset < s.len() {
res.push(s[offset..].into());
lines.push(s[offset..].into());
}
res
lines
}
#[derive(Debug)]
@ -115,7 +115,7 @@ pub(crate) fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec<MatchedPos>
let mut lhs_offset = 0;
let mut rhs_offset = 0;
let mut res = vec![];
let mut mps = vec![];
for (kind, lhs_lines, rhs_lines) in changed_parts(lhs_src, rhs_src) {
match kind {
TextChangeKind::Unchanged => {
@ -125,7 +125,7 @@ pub(crate) fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec<MatchedPos>
let rhs_pos =
rhs_lp.from_region(rhs_offset, rhs_offset + line_len_in_bytes(rhs_line));
res.push(MatchedPos {
mps.push(MatchedPos {
kind: MatchKind::UnchangedToken {
highlight: TokenKind::Atom(AtomKind::Normal),
self_pos: lhs_pos.clone(),
@ -150,7 +150,7 @@ pub(crate) fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec<MatchedPos>
myers_diff::DiffResult::Left(lhs_word) => {
let lhs_pos =
lhs_lp.from_region(lhs_offset, lhs_offset + lhs_word.len());
res.push(MatchedPos {
mps.push(MatchedPos {
kind: MatchKind::NovelWord {
highlight: TokenKind::Atom(AtomKind::Normal),
},
@ -166,7 +166,7 @@ pub(crate) fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec<MatchedPos>
let rhs_pos =
rhs_lp.from_region(rhs_offset, rhs_offset + rhs_word.len());
res.push(MatchedPos {
mps.push(MatchedPos {
kind: MatchKind::NovelLinePart {
highlight: TokenKind::Atom(AtomKind::Normal),
self_pos: lhs_pos[0],
@ -188,7 +188,7 @@ pub(crate) fn change_positions(lhs_src: &str, rhs_src: &str) -> Vec<MatchedPos>
}
}
res
mps
}
#[cfg(test)]

@ -449,7 +449,7 @@ fn build_display_path(lhs_path: &FileArgument, rhs_path: &FileArgument) -> Strin
}
fn parse_overrides_or_die(raw_overrides: &[String]) -> Vec<(LanguageOverride, Vec<glob::Pattern>)> {
let mut res: Vec<(LanguageOverride, Vec<glob::Pattern>)> = vec![];
let mut overrides: Vec<(LanguageOverride, Vec<glob::Pattern>)> = vec![];
let mut invalid_syntax = false;
for raw_override in raw_overrides {
@ -457,7 +457,7 @@ fn parse_overrides_or_die(raw_overrides: &[String]) -> Vec<(LanguageOverride, Ve
match glob::Pattern::new(glob_str) {
Ok(pattern) => {
if let Some(language_override) = language_override_from_name(lang_name) {
res.push((language_override, vec![pattern]));
overrides.push((language_override, vec![pattern]));
} else {
eprintln!("No such language '{}'", lang_name);
eprintln!("See --list-languages for the names of all languages available. Language overrides are case insensitive.");
@ -481,7 +481,7 @@ fn parse_overrides_or_die(raw_overrides: &[String]) -> Vec<(LanguageOverride, Ve
std::process::exit(EXIT_BAD_ARGUMENTS);
}
res.into_iter()
overrides.into_iter()
.coalesce(
|(prev_lang, mut prev_globs), (current_lang, current_globs)| {
if prev_lang == current_lang {

@ -349,12 +349,12 @@ pub(crate) fn comment_positions<'a>(nodes: &[&'a Syntax<'a>]) -> Vec<SingleLineS
}
}
let mut res = vec![];
let mut positions = vec![];
for node in nodes {
walk_comment_positions(node, &mut res);
walk_comment_positions(node, &mut positions);
}
res
positions
}
/// Initialise all the fields in `SyntaxInfo`.
@ -667,13 +667,13 @@ fn split_atom_words(
let mut offset = 0;
let mut opposite_offset = 0;
let mut res = vec![];
let mut mps = vec![];
for diff_res in word_diffs {
match diff_res {
myers_diff::DiffResult::Left(word) => {
// This word is novel to this side.
if !is_all_whitespace(word) {
res.push(MatchedPos {
mps.push(MatchedPos {
kind: MatchKind::NovelWord {
highlight: TokenKind::Atom(kind),
},
@ -699,7 +699,7 @@ fn split_atom_words(
opposite_offset + opposite_word.len(),
);
res.push(MatchedPos {
mps.push(MatchedPos {
kind: MatchKind::NovelLinePart {
highlight: TokenKind::Atom(kind),
self_pos: word_pos,
@ -717,7 +717,7 @@ fn split_atom_words(
}
}
res
mps
}
/// Are there sufficient common words that we should only highlight
@ -828,9 +828,9 @@ impl MatchedPos {
};
// Create a MatchedPos for every line that `pos` covers.
let mut res = vec![];
let mut mps = vec![];
for line_pos in &pos {
res.push(Self {
mps.push(Self {
kind: kind.clone(),
pos: *line_pos,
});
@ -839,16 +839,16 @@ impl MatchedPos {
// MatchedPos on the LHS and RHS. This allows us
// to consider unchanged MatchedPos values
// pairwise.
if res.len() == opposite_pos_len {
if mps.len() == opposite_pos_len {
break;
}
}
res
mps
}
Novel => {
let kind = MatchKind::Novel { highlight };
// Create a MatchedPos for every line that `pos` covers.
let mut res = vec![];
let mut mps = vec![];
for line_pos in &pos {
// Don't create a MatchedPos for entirely empty positions. This
// occurs when we have lists with empty open/close
@ -857,13 +857,13 @@ impl MatchedPos {
continue;
}
res.push(Self {
mps.push(Self {
kind: kind.clone(),
pos: *line_pos,
});
}
res
mps
}
}
}

@ -1455,10 +1455,10 @@ fn all_syntaxes_from_cursor<'a>(
subtrees: &DftHashMap<usize, (tree_sitter::Tree, TreeSitterConfig, HighlightedNodeIds)>,
ignore_comments: bool,
) -> Vec<&'a Syntax<'a>> {
let mut result: Vec<&Syntax> = vec![];
let mut nodes: Vec<&Syntax> = vec![];
loop {
result.extend(syntax_from_cursor(
nodes.extend(syntax_from_cursor(
arena,
src,
nl_pos,
@ -1475,7 +1475,7 @@ fn all_syntaxes_from_cursor<'a>(
}
}
result
nodes
}
/// Convert the tree-sitter node at `cursor` to a difftastic syntax

@ -7,7 +7,7 @@
/// so they are separate implementations rather than passing a bool to
/// customise number handling.
pub(crate) fn split_words(s: &str) -> Vec<&str> {
let mut res = vec![];
let mut words = vec![];
let mut word_start: Option<usize> = None;
for (idx, c) in s.char_indices() {
match word_start {
@ -16,8 +16,8 @@ pub(crate) fn split_words(s: &str) -> Vec<&str> {
// Just carry on in this word.
} else {
// Push the previous word, then this non-word character.
res.push(&s[start..idx]);
res.push(&s[idx..idx + c.len_utf8()]);
words.push(&s[start..idx]);
words.push(&s[idx..idx + c.len_utf8()]);
word_start = None;
}
}
@ -25,16 +25,16 @@ pub(crate) fn split_words(s: &str) -> Vec<&str> {
if c.is_alphanumeric() || c == '-' || c == '_' {
word_start = Some(idx);
} else {
res.push(&s[idx..idx + c.len_utf8()]);
words.push(&s[idx..idx + c.len_utf8()]);
}
}
}
}
if let Some(start) = word_start {
res.push(&s[start..]);
words.push(&s[start..]);
}
res
words
}
/// Split `s` into a vec of things that look like words and individual
@ -42,7 +42,7 @@ pub(crate) fn split_words(s: &str) -> Vec<&str> {
///
/// "foo..bar23" -> vec!["foo", ".", ".", "bar23"]
pub(crate) fn split_words_and_numbers(s: &str) -> Vec<&str> {
let mut res = vec![];
let mut words = vec![];
let mut word_start: Option<(usize, char)> = None;
for (idx, c) in s.char_indices() {
match word_start {
@ -54,13 +54,13 @@ pub(crate) fn split_words_and_numbers(s: &str) -> Vec<&str> {
// Just carry on in this word.
} else {
// Finish previous word, start a new one.
res.push(&s[start..idx]);
words.push(&s[start..idx]);
word_start = Some((idx, c));
}
} else {
// Push the previous word, then this non-word character.
res.push(&s[start..idx]);
res.push(&s[idx..idx + c.len_utf8()]);
words.push(&s[start..idx]);
words.push(&s[idx..idx + c.len_utf8()]);
word_start = None;
}
}
@ -68,16 +68,16 @@ pub(crate) fn split_words_and_numbers(s: &str) -> Vec<&str> {
if c.is_alphanumeric() || c == '-' || c == '_' {
word_start = Some((idx, c));
} else {
res.push(&s[idx..idx + c.len_utf8()]);
words.push(&s[idx..idx + c.len_utf8()]);
}
}
}
}
if let Some((start, _)) = word_start {
res.push(&s[start..]);
words.push(&s[start..]);
}
res
words
}
#[cfg(test)]