@@ -18,7 +18,7 @@ use crate::{
18
18
common_fs:: { get_line_cols_from_offset, FileObj } ,
19
19
} ;
20
20
21
- #[ derive( Debug , Clone , Deserialize ) ]
21
+ #[ derive( Debug , Clone , Deserialize , PartialEq , Eq ) ]
22
22
pub struct FormatAdvice {
23
23
/// A list of [`Replacement`]s that clang-tidy wants to make.
24
24
#[ serde( rename( deserialize = "replacement" ) ) ]
@@ -38,25 +38,25 @@ impl MakeSuggestions for FormatAdvice {
38
38
}
39
39
40
40
/// 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 ) ]
42
42
pub struct Replacement {
43
43
/// The byte offset where the replacement will start.
44
44
#[ serde( rename = "@offset" ) ]
45
- pub offset : usize ,
45
+ pub offset : u32 ,
46
46
47
47
/// The line number described by the [`Replacement::offset`].
48
48
///
49
49
/// This value is not provided by the XML output, but we calculate it after
50
50
/// deserialization.
51
51
#[ serde( default ) ]
52
- pub line : usize ,
52
+ pub line : u32 ,
53
53
54
54
/// The column number on the line described by the [`Replacement::offset`].
55
55
///
56
56
/// This value is not provided by the XML output, but we calculate it after
57
57
/// deserialization.
58
58
#[ serde( default ) ]
59
- pub cols : usize ,
59
+ pub cols : u32 ,
60
60
}
61
61
62
62
/// Get a string that summarizes the given `--style`
@@ -163,8 +163,7 @@ pub fn run_clang_format(
163
163
if !format_advice. replacements . is_empty ( ) {
164
164
let original_contents = fs:: read ( & file. name ) . with_context ( || {
165
165
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}" ,
168
167
)
169
168
} ) ?;
170
169
// get line and column numbers from format_advice.offset
@@ -175,7 +174,7 @@ pub fn run_clang_format(
175
174
replacement. line = line_number;
176
175
replacement. cols = columns;
177
176
for range in & ranges {
178
- if range. contains ( & line_number. try_into ( ) . unwrap_or ( 0 ) ) {
177
+ if range. contains ( & line_number) {
179
178
filtered_replacements. push ( * replacement) ;
180
179
break ;
181
180
}
@@ -208,37 +207,20 @@ mod tests {
208
207
. to_vec ( ) ;
209
208
210
209
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,
214
214
..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 ( ) ,
229
217
patched : None ,
230
218
} ;
231
219
232
220
let xml = String :: from_utf8 ( xml_raw) . unwrap ( ) ;
233
221
234
222
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) ;
242
224
}
243
225
244
226
fn formalize_style ( style : & str , expected : & str ) {
0 commit comments