Skip to content

Commit fe9709c

Browse files
committed
1 parent 281bf03 commit fe9709c

File tree

2 files changed

+45
-28
lines changed

2 files changed

+45
-28
lines changed

src/lib.rs

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -396,37 +396,40 @@ fn format_code_block(
396396
.unwrap_or_else(|| formatted.snippet.len());
397397
let mut is_indented = true;
398398
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
399-
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
400-
if !is_first {
401-
result.push('\n');
402-
} else {
403-
is_first = false;
404-
}
405-
let trimmed_line = if !is_indented {
406-
line
407-
} else if line.len() > config.max_width() {
408-
// If there are lines that are larger than max width, we cannot tell
409-
// whether we have succeeded but have some comments or strings that
410-
// are too long, or we have failed to format code block. We will be
411-
// conservative and just return `None` in this case.
412-
return None;
413-
} else if line.len() > indent_str.len() {
414-
// Make sure that the line has leading whitespaces.
415-
if line.starts_with(indent_str.as_ref()) {
416-
let offset = if config.hard_tabs() {
417-
1
399+
400+
if let Some(to_format) = formatted.snippet.get(FN_MAIN_PREFIX.len()..block_len) {
401+
for (kind, ref line) in LineClasses::new(to_format) {
402+
if !is_first {
403+
result.push('\n');
404+
} else {
405+
is_first = false;
406+
}
407+
let trimmed_line = if !is_indented {
408+
line
409+
} else if line.len() > config.max_width() {
410+
// If there are lines that are larger than max width, we cannot tell
411+
// whether we have succeeded but have some comments or strings that
412+
// are too long, or we have failed to format code block. We will be
413+
// conservative and just return `None` in this case.
414+
return None;
415+
} else if line.len() > indent_str.len() {
416+
// Make sure that the line has leading whitespaces.
417+
if line.starts_with(indent_str.as_ref()) {
418+
let offset = if config.hard_tabs() {
419+
1
420+
} else {
421+
config.tab_spaces()
422+
};
423+
&line[offset..]
418424
} else {
419-
config.tab_spaces()
420-
};
421-
&line[offset..]
425+
line
426+
}
422427
} else {
423428
line
424-
}
425-
} else {
426-
line
427-
};
428-
result.push_str(trimmed_line);
429-
is_indented = indent_next_line(kind, line, config);
429+
};
430+
result.push_str(trimmed_line);
431+
is_indented = indent_next_line(kind, line, config);
432+
}
430433
}
431434
Some(FormattedSnippet {
432435
snippet: result,

tests/target/issue-5234.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-format_code_in_doc_comments: true
2+
3+
/// ```
4+
/// ```
5+
fn a() {}
6+
7+
/// ```
8+
/// ```
9+
fn a() {}
10+
11+
/// ```rust
12+
/// k1 == k2 ⇒ hash(k1) == hash(k2)
13+
/// ```
14+
pub struct a;

0 commit comments

Comments
 (0)