Skip to content

Commit d5cc0ca

Browse files
committed
remove column count from Replacement struct
Also ensure given `offset` is not out of bounds when calculating line number from byte offset.
1 parent e5261f0 commit d5cc0ca

File tree

3 files changed

+10
-27
lines changed

3 files changed

+10
-27
lines changed

cpp-linter/src/clang_tools/clang_format.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use serde::Deserialize;
1515
use super::MakeSuggestions;
1616
use crate::{
1717
cli::ClangParams,
18-
common_fs::{get_line_cols_from_offset, FileObj},
18+
common_fs::{get_line_count_from_offset, FileObj},
1919
};
2020

2121
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
@@ -50,13 +50,6 @@ pub struct Replacement {
5050
/// deserialization.
5151
#[serde(default)]
5252
pub line: u32,
53-
54-
/// The column number on the line described by the [`Replacement::offset`].
55-
///
56-
/// This value is not provided by the XML output, but we calculate it after
57-
/// deserialization.
58-
#[serde(default)]
59-
pub cols: u32,
6053
}
6154

6255
/// Get a string that summarizes the given `--style`
@@ -169,10 +162,8 @@ pub fn run_clang_format(
169162
// get line and column numbers from format_advice.offset
170163
let mut filtered_replacements = Vec::new();
171164
for replacement in &mut format_advice.replacements {
172-
let (line_number, columns) =
173-
get_line_cols_from_offset(&original_contents, replacement.offset);
165+
let line_number = get_line_count_from_offset(&original_contents, replacement.offset);
174166
replacement.line = line_number;
175-
replacement.cols = columns;
176167
for range in &ranges {
177168
if range.contains(&line_number) {
178169
filtered_replacements.push(*replacement);

cpp-linter/src/common_fs/mod.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -227,12 +227,11 @@ impl FileObj {
227227
/// boundary exists at the returned column number. However, the `offset` given to this
228228
/// function is expected to originate from diagnostic information provided by
229229
/// clang-format or clang-tidy.
230-
pub fn get_line_cols_from_offset(contents: &[u8], offset: u32) -> (u32, u32) {
231-
let lines = contents[0..offset as usize].split(|byte| byte == &b'\n');
232-
let line_count = lines.clone().count() as u32;
233-
// here we `cols.len() + 1` because columns is not a 0-based count
234-
let column_count = lines.last().map(|cols| cols.len() + 1).unwrap_or(1) as u32;
235-
(line_count, column_count)
230+
pub fn get_line_count_from_offset(contents: &[u8], offset: u32) -> u32 {
231+
let offset = (offset as usize).min(contents.len());
232+
let lines = contents[0..offset].split(|byte| byte == &b'\n');
233+
234+
lines.count() as u32
236235
}
237236

238237
/// This was copied from [cargo source code](https://github.com/rust-lang/cargo/blob/fede83ccf973457de319ba6fa0e36ead454d2e20/src/cargo/util/paths.rs#L61).
@@ -270,7 +269,7 @@ mod test {
270269
use std::path::PathBuf;
271270
use std::{env::current_dir, fs};
272271

273-
use super::{get_line_cols_from_offset, normalize_path, FileObj};
272+
use super::{get_line_count_from_offset, normalize_path, FileObj};
274273
use crate::cli::LinesChangedOnly;
275274

276275
// *********************** tests for normalized paths
@@ -313,10 +312,8 @@ mod test {
313312
#[test]
314313
fn translate_byte_offset() {
315314
let contents = fs::read(PathBuf::from("tests/demo/demo.cpp")).unwrap();
316-
let (lines, cols) = get_line_cols_from_offset(&contents, 144);
317-
println!("lines: {lines}, cols: {cols}");
315+
let lines = get_line_count_from_offset(&contents, 144);
318316
assert_eq!(lines, 13);
319-
assert_eq!(cols, 5);
320317
}
321318

322319
// *********************** tests for FileObj::get_ranges()

cpp-linter/src/rest_api/github/mod.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -314,13 +314,8 @@ mod test {
314314
notes,
315315
patched: None,
316316
});
317-
let replacements = vec![Replacement {
318-
offset: 0,
319-
line: 1,
320-
cols: 1,
321-
}];
322317
file.format_advice = Some(FormatAdvice {
323-
replacements,
318+
replacements: vec![Replacement { offset: 0, line: 1 }],
324319
patched: None,
325320
});
326321
files.push(Arc::new(Mutex::new(file)));

0 commit comments

Comments
 (0)