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

fix newline width calc in combine w/ comments #4123

Merged
merged 1 commit into from
Apr 16, 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
2 changes: 1 addition & 1 deletion rustfmt-core/rustfmt-lib/src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ pub(crate) fn combine_strs_with_missing_comments(
let missing_comment = rewrite_missing_comment(span, shape, context)?;

if missing_comment.is_empty() {
if allow_extend && prev_str.len() + first_sep.len() + next_str.len() <= shape.width {
if allow_extend && one_line_width <= shape.width {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is indeed a fix and that we don't want to use the full length of next_str. Otherwise next_str can start to exceed the shape width pretty quickly.

For example:

fn foo() {
    match glyph {
        found @ Glyph {
            data: Gl::Com { .. },
            foo: bar,
        } => (),
        _ => (),
    };
}

Remains unchanged by rustfmt, but adding a few more items to the struct (or longer names) will increase the total next_str length and fail the conditional check resulting in a newline insertion.

fn foo() {
    match glyph {
        found @ Glyph {
            data: Gl::Com { .. },
            foo: bar,
            baz: qux,
        } => (),
        _ => (),
    };
}

would be reformatted to the below without this change

fn foo() {
    match glyph {
        found
        @
        Glyph {
            data: Gl::Com { .. },
            foo: bar,
            baz: qux,
        } => (),
        _ => (),
    };
}

result.push_str(first_sep);
} else if !prev_str.is_empty() {
result.push_str(&indent.to_string_with_newline(config))
Expand Down
21 changes: 21 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue_4031.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
fn foo() {
with_woff2_glyf_table("tests/fonts/woff2/SFNT-TTF-Composite.woff2", |glyf| {
let actual = glyf
.records
.iter()
.map(|glyph| match glyph {
GlyfRecord::Parsed(
found @ Glyph {
data: GlyphData::Composite { .. },
..
},
) => Some(found),
_ => None,
})
.find(|candidate| candidate.is_some())
.unwrap()
.unwrap();

assert_eq!(*actual, expected)
});
}
55 changes: 55 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/issue_4110.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
fn bindings() {
let err = match (place_desc, explanation) {
(
Some(ref name),
BorrowExplanation::MustBeValidFor {
category:
category @ (ConstraintCategory::Return
| ConstraintCategory::CallArgument
| ConstraintCategory::OpaqueType),
from_closure: false,
ref region_name,
span,
..
},
) if borrow_spans.for_generator() | borrow_spans.for_closure() => self
.report_escaping_closure_capture(
borrow_spans,
borrow_span,
region_name,
category,
span,
&format!("`{}`", name),
),
(
ref name,
BorrowExplanation::MustBeValidFor {
category: ConstraintCategory::Assignment,
from_closure: false,
region_name:
RegionName {
source: RegionNameSource::AnonRegionFromUpvar(upvar_span, ref upvar_name),
..
},
span,
..
},
) => self.report_escaping_data(borrow_span, name, upvar_span, upvar_name, span),
(Some(name), explanation) => self.report_local_value_does_not_live_long_enough(
location,
&name,
&borrow,
drop_span,
borrow_spans,
explanation,
),
(None, explanation) => self.report_temporary_value_does_not_live_long_enough(
location,
&borrow,
drop_span,
borrow_spans,
proper_span,
explanation,
),
};
}