Skip to content

Commit 05825a8

Browse files
feat: support raw ref operator (#4046)
1 parent b9138e0 commit 05825a8

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

rustfmt-core/rustfmt-lib/src/expr.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -2039,14 +2039,16 @@ pub(crate) fn prefer_next_line(
20392039

20402040
fn rewrite_expr_addrof(
20412041
context: &RewriteContext<'_>,
2042-
_borrow_kind: ast::BorrowKind,
2042+
borrow_kind: ast::BorrowKind,
20432043
mutability: ast::Mutability,
20442044
expr: &ast::Expr,
20452045
shape: Shape,
20462046
) -> Option<String> {
2047-
let operator_str = match mutability {
2048-
ast::Mutability::Not => "&",
2049-
ast::Mutability::Mut => "&mut ",
2047+
let operator_str = match (mutability, borrow_kind) {
2048+
(ast::Mutability::Not, ast::BorrowKind::Ref) => "&",
2049+
(ast::Mutability::Not, ast::BorrowKind::Raw) => "&raw const ",
2050+
(ast::Mutability::Mut, ast::BorrowKind::Ref) => "&mut ",
2051+
(ast::Mutability::Mut, ast::BorrowKind::Raw) => "&raw mut ",
20502052
};
20512053
rewrite_unary_prefix(context, operator_str, expr, shape)
20522054
}

rustfmt-core/rustfmt-lib/tests/source/expr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ fn returns() {
218218
fn addrof() {
219219
& mut(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
220220
& (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa+bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
221+
222+
// raw reference operator
223+
& raw const a;
224+
& raw mut b;
221225
}
222226

223227
fn casts() {

rustfmt-core/rustfmt-lib/tests/target/expr.rs

+4
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ fn addrof() {
251251
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
252252
&(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
253253
+ bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb);
254+
255+
// raw reference operator
256+
&raw const a;
257+
&raw mut b;
254258
}
255259

256260
fn casts() {

0 commit comments

Comments
 (0)