Skip to content

Commit 9376992

Browse files
committed
consolidate a single added line into ranges correctly
1 parent a3dbd01 commit 9376992

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

cpp-linter/src/common_fs.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ impl FileObj {
9090
};
9191
// lines.len() cannot be 0 at this point
9292
let last_index = lines.len() - 1;
93+
if last_index == 0 {
94+
// Single element case: push range and return
95+
ranges.push(RangeInclusive::new(range_start, range_start));
96+
return ranges;
97+
}
9398
for (index, number) in line_iter {
9499
// use let chain to avoid repeated lookup of lines[index - 1].
95100
// should always yield some value since we entered the for loop at index 1.
@@ -281,6 +286,14 @@ mod test {
281286
assert_eq!(ranges, vec![4..=5, 9..=9]);
282287
}
283288

289+
#[test]
290+
fn get_ranges_single_added_line() {
291+
let added_lines = vec![5];
292+
let file_obj = FileObj::from(PathBuf::from("tests/demo/demo.cpp"), added_lines, vec![]);
293+
let ranges = file_obj.get_ranges(&LinesChangedOnly::On);
294+
assert_eq!(ranges, vec![5..=5]);
295+
}
296+
284297
#[test]
285298
fn line_not_in_diff() {
286299
let file_obj = FileObj::new(PathBuf::from("tests/demo/demo.cpp"));

0 commit comments

Comments
 (0)