Skip to content

Commit

Permalink
feat: support raw ref operator (#4046)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebcartwright authored Feb 14, 2020
1 parent b9138e0 commit 05825a8
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
10 changes: 6 additions & 4 deletions rustfmt-core/rustfmt-lib/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2039,14 +2039,16 @@ pub(crate) fn prefer_next_line(

fn rewrite_expr_addrof(
context: &RewriteContext<'_>,
_borrow_kind: ast::BorrowKind,
borrow_kind: ast::BorrowKind,
mutability: ast::Mutability,
expr: &ast::Expr,
shape: Shape,
) -> Option<String> {
let operator_str = match mutability {
ast::Mutability::Not => "&",
ast::Mutability::Mut => "&mut ",
let operator_str = match (mutability, borrow_kind) {
(ast::Mutability::Not, ast::BorrowKind::Ref) => "&",
(ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ",
(ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ",
(ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ",
};
rewrite_unary_prefix(context, operator_str, expr, shape)
}
Expand Down
4 changes: 4 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/source/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ fn returns() {
fn addrof() {
& mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
& (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);

// raw reference operator
& raw const a;
& raw mut b;
}

fn casts() {
Expand Down
4 changes: 4 additions & 0 deletions rustfmt-core/rustfmt-lib/tests/target/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,10 @@ fn addrof() {
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
&(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);

// raw reference operator
&raw const a;
&raw mut b;
}

fn casts() {
Expand Down

0 comments on commit 05825a8

Please sign in to comment.