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: between span derivation on cast exprs #4536

Merged
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 src/formatting/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ pub(crate) fn format_expr(
/* Retrieving the comments before and after cast */
let prefix_span = mk_sp(
subexpr.span.hi(),
context.snippet_provider.span_before(expr.span, "as"),
context.snippet_provider.span_before_last(expr.span, "as") - BytePos(1),
Copy link
Member Author

Choose a reason for hiding this comment

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

Think there's a separate bug in this util function that only backs the position off by one, but feels like it should be the length of the needle. Our needle is the as keyword, so moving it back by one to avoid snagging the a in the keyword

);
let suffix_span = mk_sp(
context.snippet_provider.span_after(expr.span, "as"),
context.snippet_provider.span_after_last(expr.span, "as"),
ty.span.lo(),
);
let infix_prefix_comments = rewrite_missing_comment(prefix_span, shape, context)?;
Expand Down
4 changes: 4 additions & 0 deletions tests/source/issue_4534.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
fn main() { let z = (x as f64 / y as f64).floor() as usize; }


fn main() { let z = (x /* x */ as /*y */ f64 / y /* z */ as /* a */ f64).floor() /* b */ as /* c */ usize; }
7 changes: 7 additions & 0 deletions tests/target/issue_4534.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let z = (x as f64 / y as f64).floor() as usize;
}

fn main() {
let z = (x /* x */ as /*y */ f64 / y /* z */ as /* a */ f64).floor() /* b */ as /* c */ usize;
}