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

Compare code block line indentation with config whitespace #4166

Merged
merged 1 commit into from
May 10, 2020
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
4 changes: 2 additions & 2 deletions rustfmt-core/rustfmt-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
.rfind('}')
.unwrap_or_else(|| formatted.snippet.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]) {
if !is_first {
result.push('\n');
Expand All @@ -364,9 +365,8 @@ fn format_code_block(code_snippet: &str, config: &Config) -> Option<FormattedSni
// are too long, or we have failed to format code block. We will be
// conservative and just return `None` in this case.
return None;
} else if line.len() > config.tab_spaces() {
} else if line.len() > indent_str.len() {
// Make sure that the line has leading whitespaces.
let indent_str = Indent::from_width(config, config.tab_spaces()).to_string(config);
if line.starts_with(indent_str.as_ref()) {
let offset = if config.hard_tabs() {
1
Expand Down
18 changes: 18 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue-4152.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// rustfmt-hard_tabs: true

macro_rules! bit {
($bool:expr) => {
if $bool {
1;
1
} else {
0;
0
}
};
}
macro_rules! add_one {
($vec:expr) => {{
$vec.push(1);
}};
}