Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent ICE when formatting an empty-ish macro arm #5833

Merged
merged 1 commit into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern crate thin_vec;
extern crate rustc_driver;

use std::cell::RefCell;
use std::cmp::min;
use std::collections::HashMap;
use std::fmt;
use std::io::{self, Write};
Expand Down Expand Up @@ -385,9 +386,15 @@ fn format_code_block(
.snippet
.rfind('}')
.unwrap_or_else(|| formatted.snippet.len());

// It's possible that `block_len < FN_MAIN_PREFIX.len()`. This can happen if the code block was
// formatted into the empty string, leading to the enclosing `fn main() {\n}` being formatted
// into `fn main() {}`. In this case no unindentation is done.
let block_start = min(FN_MAIN_PREFIX.len(), block_len);

let mut is_indented = true;
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
for (kind, ref line) in LineClasses::new(&formatted.snippet[FN_MAIN_PREFIX.len()..block_len]) {
for (kind, ref line) in LineClasses::new(&formatted.snippet[block_start..block_len]) {
if !is_first {
result.push('\n');
} else {
Expand Down
3 changes: 3 additions & 0 deletions tests/source/issue_5730.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
macro_rules! statement {
() => {;};
}
3 changes: 3 additions & 0 deletions tests/target/issue_5730.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
macro_rules! statement {
() => {};
}