From 2e37f508279e172b33aae7c433fe486bf584ff1a Mon Sep 17 00:00:00 2001 From: Caleb Cartwright Date: Wed, 18 Nov 2020 20:36:38 -0600 Subject: [PATCH] fix: between span derviation on cast exprs --- src/formatting/expr.rs | 4 ++-- tests/source/issue_4534.rs | 4 ++++ tests/target/issue_4534.rs | 7 +++++++ 3 files changed, 13 insertions(+), 2 deletions(-) create mode 100644 tests/source/issue_4534.rs create mode 100644 tests/target/issue_4534.rs diff --git a/src/formatting/expr.rs b/src/formatting/expr.rs index 61937aeee3f..c57fe37f587 100644 --- a/src/formatting/expr.rs +++ b/src/formatting/expr.rs @@ -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), ); 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)?; diff --git a/tests/source/issue_4534.rs b/tests/source/issue_4534.rs new file mode 100644 index 00000000000..1c194b40663 --- /dev/null +++ b/tests/source/issue_4534.rs @@ -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; } \ No newline at end of file diff --git a/tests/target/issue_4534.rs b/tests/target/issue_4534.rs new file mode 100644 index 00000000000..4a326e5e517 --- /dev/null +++ b/tests/target/issue_4534.rs @@ -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; +}