Skip to content

Commit c777fb5

Browse files
committed
style: Make clippy happy
1 parent bc504bc commit c777fb5

File tree

4 files changed

+142
-145
lines changed

4 files changed

+142
-145
lines changed

src/display_list/from_snippet.rs

+135-138
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ impl<'a> CursorLines<'a> {
1111
}
1212

1313
enum EndLine {
14-
EOF = 0,
15-
CRLF = 1,
16-
LF = 2,
14+
Eof = 0,
15+
Crlf = 1,
16+
Lf = 2,
1717
}
1818

1919
impl<'a> Iterator for CursorLines<'a> {
@@ -28,18 +28,18 @@ impl<'a> Iterator for CursorLines<'a> {
2828
.map(|x| {
2929
let ret = if 0 < x {
3030
if self.0.as_bytes()[x - 1] == b'\r' {
31-
(&self.0[..x - 1], EndLine::LF)
31+
(&self.0[..x - 1], EndLine::Lf)
3232
} else {
33-
(&self.0[..x], EndLine::CRLF)
33+
(&self.0[..x], EndLine::Crlf)
3434
}
3535
} else {
36-
("", EndLine::CRLF)
36+
("", EndLine::Crlf)
3737
};
3838
self.0 = &self.0[x + 1..];
3939
ret
4040
})
4141
.or_else(|| {
42-
let ret = Some((self.0, EndLine::EOF));
42+
let ret = Some((self.0, EndLine::Eof));
4343
self.0 = "";
4444
ret
4545
})
@@ -330,171 +330,168 @@ fn format_body(
330330
.map(|m| m.left(line_end_index - line_start_index))
331331
.unwrap_or_default();
332332
// It would be nice to use filter_drain here once it's stable.
333-
annotations = annotations
334-
.into_iter()
335-
.filter(|annotation| {
336-
let body_idx = idx + annotation_line_count;
337-
let annotation_type = match annotation.annotation_type {
338-
snippet::AnnotationType::Error => DisplayAnnotationType::None,
339-
snippet::AnnotationType::Warning => DisplayAnnotationType::None,
340-
_ => DisplayAnnotationType::from(annotation.annotation_type),
341-
};
342-
match annotation.range {
343-
(start, _) if start > line_end_index => true,
344-
(start, end)
345-
if start >= line_start_index && end <= line_end_index
346-
|| start == line_end_index && end - start <= 1 =>
347-
{
348-
let annotation_start_col = char_widths
349-
.iter()
350-
.take(start - line_start_index)
351-
.sum::<usize>()
352-
- margin_left;
353-
let annotation_end_col = char_widths
354-
.iter()
355-
.take(end - line_start_index)
356-
.sum::<usize>()
357-
- margin_left;
358-
let range = (annotation_start_col, annotation_end_col);
359-
body.insert(
360-
body_idx + 1,
361-
DisplayLine::Source {
362-
lineno: None,
363-
inline_marks: vec![],
364-
line: DisplaySourceLine::Annotation {
365-
annotation: Annotation {
366-
annotation_type,
367-
id: None,
368-
label: format_label(Some(annotation.label), None),
369-
},
370-
range,
371-
annotation_type: DisplayAnnotationType::from(
372-
annotation.annotation_type,
373-
),
374-
annotation_part: DisplayAnnotationPart::Standalone,
375-
},
376-
},
377-
);
378-
annotation_line_count += 1;
379-
false
380-
}
381-
(start, end)
382-
if start >= line_start_index
383-
&& start <= line_end_index
384-
&& end > line_end_index =>
385-
{
386-
if start - line_start_index == 0 {
387-
if let DisplayLine::Source {
388-
ref mut inline_marks,
389-
..
390-
} = body[body_idx]
391-
{
392-
inline_marks.push(DisplayMark {
393-
mark_type: DisplayMarkType::AnnotationStart,
394-
annotation_type: DisplayAnnotationType::from(
395-
annotation.annotation_type,
396-
),
397-
});
398-
}
399-
} else {
400-
let annotation_start_col = char_widths
401-
.iter()
402-
.take(start - line_start_index)
403-
.sum::<usize>();
404-
let range = (annotation_start_col, annotation_start_col + 1);
405-
body.insert(
406-
body_idx + 1,
407-
DisplayLine::Source {
408-
lineno: None,
409-
inline_marks: vec![],
410-
line: DisplaySourceLine::Annotation {
411-
annotation: Annotation {
412-
annotation_type: DisplayAnnotationType::None,
413-
id: None,
414-
label: vec![],
415-
},
416-
range,
417-
annotation_type: DisplayAnnotationType::from(
418-
annotation.annotation_type,
419-
),
420-
annotation_part: DisplayAnnotationPart::MultilineStart,
421-
},
333+
annotations.retain(|annotation| {
334+
let body_idx = idx + annotation_line_count;
335+
let annotation_type = match annotation.annotation_type {
336+
snippet::AnnotationType::Error => DisplayAnnotationType::None,
337+
snippet::AnnotationType::Warning => DisplayAnnotationType::None,
338+
_ => DisplayAnnotationType::from(annotation.annotation_type),
339+
};
340+
match annotation.range {
341+
(start, _) if start > line_end_index => true,
342+
(start, end)
343+
if start >= line_start_index && end <= line_end_index
344+
|| start == line_end_index && end - start <= 1 =>
345+
{
346+
let annotation_start_col = char_widths
347+
.iter()
348+
.take(start - line_start_index)
349+
.sum::<usize>()
350+
- margin_left;
351+
let annotation_end_col = char_widths
352+
.iter()
353+
.take(end - line_start_index)
354+
.sum::<usize>()
355+
- margin_left;
356+
let range = (annotation_start_col, annotation_end_col);
357+
body.insert(
358+
body_idx + 1,
359+
DisplayLine::Source {
360+
lineno: None,
361+
inline_marks: vec![],
362+
line: DisplaySourceLine::Annotation {
363+
annotation: Annotation {
364+
annotation_type,
365+
id: None,
366+
label: format_label(Some(annotation.label), None),
422367
},
423-
);
424-
annotation_line_count += 1;
425-
}
426-
true
427-
}
428-
(start, end) if start < line_start_index && end > line_end_index => {
429-
if let DisplayLine::Source {
430-
ref mut inline_marks,
431-
..
432-
} = body[body_idx]
433-
{
434-
inline_marks.push(DisplayMark {
435-
mark_type: DisplayMarkType::AnnotationThrough,
368+
range,
436369
annotation_type: DisplayAnnotationType::from(
437370
annotation.annotation_type,
438371
),
439-
});
440-
}
441-
true
442-
}
443-
(start, end)
444-
if start < line_start_index
445-
&& end >= line_start_index
446-
&& end <= line_end_index =>
447-
{
372+
annotation_part: DisplayAnnotationPart::Standalone,
373+
},
374+
},
375+
);
376+
annotation_line_count += 1;
377+
false
378+
}
379+
(start, end)
380+
if start >= line_start_index
381+
&& start <= line_end_index
382+
&& end > line_end_index =>
383+
{
384+
if start - line_start_index == 0 {
448385
if let DisplayLine::Source {
449386
ref mut inline_marks,
450387
..
451388
} = body[body_idx]
452389
{
453390
inline_marks.push(DisplayMark {
454-
mark_type: DisplayMarkType::AnnotationThrough,
391+
mark_type: DisplayMarkType::AnnotationStart,
455392
annotation_type: DisplayAnnotationType::from(
456393
annotation.annotation_type,
457394
),
458395
});
459396
}
460-
461-
let end_mark = char_widths
397+
} else {
398+
let annotation_start_col = char_widths
462399
.iter()
463-
.take(end - line_start_index)
464-
.sum::<usize>()
465-
.saturating_sub(1);
466-
let range = (end_mark - margin_left, (end_mark + 1) - margin_left);
400+
.take(start - line_start_index)
401+
.sum::<usize>();
402+
let range = (annotation_start_col, annotation_start_col + 1);
467403
body.insert(
468404
body_idx + 1,
469405
DisplayLine::Source {
470406
lineno: None,
471-
inline_marks: vec![DisplayMark {
472-
mark_type: DisplayMarkType::AnnotationThrough,
473-
annotation_type: DisplayAnnotationType::from(
474-
annotation.annotation_type,
475-
),
476-
}],
407+
inline_marks: vec![],
477408
line: DisplaySourceLine::Annotation {
478409
annotation: Annotation {
479-
annotation_type,
410+
annotation_type: DisplayAnnotationType::None,
480411
id: None,
481-
label: format_label(Some(annotation.label), None),
412+
label: vec![],
482413
},
483414
range,
484415
annotation_type: DisplayAnnotationType::from(
485416
annotation.annotation_type,
486417
),
487-
annotation_part: DisplayAnnotationPart::MultilineEnd,
418+
annotation_part: DisplayAnnotationPart::MultilineStart,
488419
},
489420
},
490421
);
491422
annotation_line_count += 1;
492-
false
493423
}
494-
_ => true,
424+
true
425+
}
426+
(start, end) if start < line_start_index && end > line_end_index => {
427+
if let DisplayLine::Source {
428+
ref mut inline_marks,
429+
..
430+
} = body[body_idx]
431+
{
432+
inline_marks.push(DisplayMark {
433+
mark_type: DisplayMarkType::AnnotationThrough,
434+
annotation_type: DisplayAnnotationType::from(
435+
annotation.annotation_type,
436+
),
437+
});
438+
}
439+
true
495440
}
496-
})
497-
.collect();
441+
(start, end)
442+
if start < line_start_index
443+
&& end >= line_start_index
444+
&& end <= line_end_index =>
445+
{
446+
if let DisplayLine::Source {
447+
ref mut inline_marks,
448+
..
449+
} = body[body_idx]
450+
{
451+
inline_marks.push(DisplayMark {
452+
mark_type: DisplayMarkType::AnnotationThrough,
453+
annotation_type: DisplayAnnotationType::from(
454+
annotation.annotation_type,
455+
),
456+
});
457+
}
458+
459+
let end_mark = char_widths
460+
.iter()
461+
.take(end - line_start_index)
462+
.sum::<usize>()
463+
.saturating_sub(1);
464+
let range = (end_mark - margin_left, (end_mark + 1) - margin_left);
465+
body.insert(
466+
body_idx + 1,
467+
DisplayLine::Source {
468+
lineno: None,
469+
inline_marks: vec![DisplayMark {
470+
mark_type: DisplayMarkType::AnnotationThrough,
471+
annotation_type: DisplayAnnotationType::from(
472+
annotation.annotation_type,
473+
),
474+
}],
475+
line: DisplaySourceLine::Annotation {
476+
annotation: Annotation {
477+
annotation_type,
478+
id: None,
479+
label: format_label(Some(annotation.label), None),
480+
},
481+
range,
482+
annotation_type: DisplayAnnotationType::from(
483+
annotation.annotation_type,
484+
),
485+
annotation_part: DisplayAnnotationPart::MultilineEnd,
486+
},
487+
},
488+
);
489+
annotation_line_count += 1;
490+
false
491+
}
492+
_ => true,
493+
}
494+
});
498495
}
499496

500497
if slice.fold {

tests/dl_from_snippet.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ fn test_format_title() {
3737
fn test_format_slice() {
3838
let line_1 = "This is line 1";
3939
let line_2 = "This is line 2";
40-
let source = vec![line_1, line_2].join("\n");
40+
let source = [line_1, line_2].join("\n");
4141
let input = snippet::Snippet {
4242
title: None,
4343
footer: vec![],
@@ -173,7 +173,7 @@ fn test_format_slices_continuation() {
173173
fn test_format_slice_annotation_standalone() {
174174
let line_1 = "This is line 1";
175175
let line_2 = "This is line 2";
176-
let source = vec![line_1, line_2].join("\n");
176+
let source = [line_1, line_2].join("\n");
177177
// In line 2
178178
let range = (22, 24);
179179
let input = snippet::Snippet {

tests/fixtures_test.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn read_file(path: &str) -> Result<String, io::Error> {
1313
Ok(s.trim_end().to_string())
1414
}
1515

16-
fn read_fixture<'de>(src: &'de str) -> Result<Snippet<'de>, Box<dyn Error>> {
16+
fn read_fixture(src: &str) -> Result<Snippet<'_>, Box<dyn Error>> {
1717
Ok(toml::from_str(src).map(|a: SnippetDef| a.into())?)
1818
}
1919

@@ -25,7 +25,7 @@ fn test_fixtures() {
2525
let path_in = p.to_str().expect("Can't print path");
2626
let path_out = path_in.replace(".toml", ".txt");
2727

28-
let src = read_file(&path_in).expect("Failed to read file");
28+
let src = read_file(path_in).expect("Failed to read file");
2929
let snippet = read_fixture(&src).expect("Failed to read file");
3030
let expected_out = read_file(&path_out).expect("Failed to read file");
3131

tests/snippet/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ pub struct SnippetDef<'a> {
2323
pub slices: Vec<Slice<'a>>,
2424
}
2525

26-
impl<'a> Into<Snippet<'a>> for SnippetDef<'a> {
27-
fn into(self) -> Snippet<'a> {
26+
impl<'a> From<SnippetDef<'a>> for Snippet<'a> {
27+
fn from(val: SnippetDef<'a>) -> Self {
2828
let SnippetDef {
2929
title,
3030
footer,
3131
opt,
3232
slices,
33-
} = self;
33+
} = val;
3434
Snippet {
3535
title,
3636
footer,

0 commit comments

Comments
 (0)