Skip to content

Commit 646316c

Browse files
committed
use inclusive range and consider pure insertions
1 parent 1ead7f8 commit 646316c

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

cpp-linter/src/clang_tools/clang_format.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
use std::{
55
fs,
6-
ops::Range,
6+
ops::RangeInclusive,
77
path::PathBuf,
88
process::Command,
99
sync::{Arc, Mutex, MutexGuard},
@@ -19,7 +19,7 @@ use crate::{cli::ClangParams, common_fs::FileObj, error::ClangCaptureError};
1919
#[derive(Debug, Clone, PartialEq, Eq, Default)]
2020
pub struct FormatAdvice {
2121
/// A list of [`Replacement`]s that clang-tidy wants to make.
22-
pub replacements: Vec<Range<u32>>,
22+
pub replacements: Vec<RangeInclusive<u32>>,
2323

2424
pub patched: PathBuf,
2525
}
@@ -142,15 +142,19 @@ pub fn run_clang_format(
142142
replacements: diff
143143
.hunks()
144144
.filter_map(|hunk| {
145+
let replacement = if hunk.is_pure_insertion() {
146+
RangeInclusive::new(hunk.after.start, hunk.after.end.saturating_sub(1))
147+
} else {
148+
RangeInclusive::new(hunk.before.start, hunk.before.end.saturating_sub(1))
149+
};
145150
if ranges.is_empty() {
146-
Some(hunk.before)
151+
Some(replacement)
147152
} else {
148153
// only include replacements that fall within the specified line ranges
149154
if ranges.iter().any(|range| {
150-
range.contains(&hunk.before.start)
151-
&& range.contains(&hunk.before.end.saturating_sub(1))
155+
range.contains(replacement.start()) && range.contains(replacement.end())
152156
}) {
153-
Some(hunk.before)
157+
Some(replacement)
154158
} else {
155159
None
156160
}

cpp-linter/src/rest_client/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ impl RestClient {
223223
// assemble a list of line numbers
224224
let mut lines = Vec::new();
225225
for replacement in &format_advice.replacements {
226-
for i in replacement.start..replacement.end {
226+
for i in replacement.clone() {
227227
if !lines.contains(&i) {
228228
lines.push(i);
229229
}
@@ -360,7 +360,7 @@ fn make_format_comment(
360360
let line_count = format_advice
361361
.replacements
362362
.iter()
363-
.fold(0, |acc, r| acc + r.len());
363+
.fold(0, |acc, r| acc + r.clone().count());
364364
let note = format!(
365365
"- {} ({line_count} line{})\n",
366366
file.name.to_string_lossy().replace('\\', "/"),
@@ -488,7 +488,7 @@ mod test {
488488
});
489489
file.format_advice = Some(FormatAdvice {
490490
#[allow(clippy::single_range_in_vec_init)]
491-
replacements: vec![1..2],
491+
replacements: vec![1..=2],
492492
patched: PathBuf::new(),
493493
});
494494
files.push(Arc::new(Mutex::new(file)));

0 commit comments

Comments
 (0)