Skip to content

Commit e5261f0

Browse files
committed
self review
1 parent 9e11645 commit e5261f0

File tree

3 files changed

+19
-37
lines changed

3 files changed

+19
-37
lines changed

cpp-linter/src/clang_tools/clang_format.rs

+14-32
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
common_fs::{get_line_cols_from_offset, FileObj},
1919
};
2020

21-
#[derive(Debug, Clone, Deserialize)]
21+
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
2222
pub struct FormatAdvice {
2323
/// A list of [`Replacement`]s that clang-tidy wants to make.
2424
#[serde(rename(deserialize = "replacement"))]
@@ -38,25 +38,25 @@ impl MakeSuggestions for FormatAdvice {
3838
}
3939

4040
/// A single replacement that clang-format wants to make.
41-
#[derive(Debug, PartialEq, Default, Clone, Copy, Deserialize)]
41+
#[derive(Debug, PartialEq, Eq, Default, Clone, Copy, Deserialize)]
4242
pub struct Replacement {
4343
/// The byte offset where the replacement will start.
4444
#[serde(rename = "@offset")]
45-
pub offset: usize,
45+
pub offset: u32,
4646

4747
/// The line number described by the [`Replacement::offset`].
4848
///
4949
/// This value is not provided by the XML output, but we calculate it after
5050
/// deserialization.
5151
#[serde(default)]
52-
pub line: usize,
52+
pub line: u32,
5353

5454
/// The column number on the line described by the [`Replacement::offset`].
5555
///
5656
/// This value is not provided by the XML output, but we calculate it after
5757
/// deserialization.
5858
#[serde(default)]
59-
pub cols: usize,
59+
pub cols: u32,
6060
}
6161

6262
/// Get a string that summarizes the given `--style`
@@ -163,8 +163,7 @@ pub fn run_clang_format(
163163
if !format_advice.replacements.is_empty() {
164164
let original_contents = fs::read(&file.name).with_context(|| {
165165
format!(
166-
"Failed to cache file's original content before applying clang-tidy changes: {}",
167-
file_name.clone()
166+
"Failed to read file's original content before translating byte offsets: {file_name}",
168167
)
169168
})?;
170169
// get line and column numbers from format_advice.offset
@@ -175,7 +174,7 @@ pub fn run_clang_format(
175174
replacement.line = line_number;
176175
replacement.cols = columns;
177176
for range in &ranges {
178-
if range.contains(&line_number.try_into().unwrap_or(0)) {
177+
if range.contains(&line_number) {
179178
filtered_replacements.push(*replacement);
180179
break;
181180
}
@@ -208,37 +207,20 @@ mod tests {
208207
.to_vec();
209208

210209
let expected = FormatAdvice {
211-
replacements: vec![
212-
Replacement {
213-
offset: 113,
210+
replacements: [113, 147, 161, 165]
211+
.iter()
212+
.map(|offset| Replacement {
213+
offset: *offset,
214214
..Default::default()
215-
},
216-
Replacement {
217-
offset: 147,
218-
..Default::default()
219-
},
220-
Replacement {
221-
offset: 161,
222-
..Default::default()
223-
},
224-
Replacement {
225-
offset: 165,
226-
..Default::default()
227-
},
228-
],
215+
})
216+
.collect(),
229217
patched: None,
230218
};
231219

232220
let xml = String::from_utf8(xml_raw).unwrap();
233221

234222
let document = quick_xml::de::from_str::<FormatAdvice>(&xml).unwrap();
235-
assert_eq!(expected.replacements.len(), document.replacements.len());
236-
for i in 0..expected.replacements.len() {
237-
assert_eq!(
238-
expected.replacements[i].offset,
239-
document.replacements[i].offset
240-
);
241-
}
223+
assert_eq!(expected, document);
242224
}
243225

244226
fn formalize_style(style: &str, expected: &str) {

cpp-linter/src/common_fs/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -227,11 +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: usize) -> (usize, usize) {
231-
let lines = contents[0..offset].split(|byte| byte == &b'\n');
232-
let line_count = lines.clone().count();
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;
233233
// 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);
234+
let column_count = lines.last().map(|cols| cols.len() + 1).unwrap_or(1) as u32;
235235
(line_count, column_count)
236236
}
237237

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl GithubApiClient {
169169
let file = file.lock().unwrap();
170170
if let Some(format_advice) = &file.format_advice {
171171
// assemble a list of line numbers
172-
let mut lines: Vec<usize> = Vec::new();
172+
let mut lines = Vec::new();
173173
for replacement in &format_advice.replacements {
174174
if !lines.contains(&replacement.line) {
175175
lines.push(replacement.line);

0 commit comments

Comments
 (0)