Skip to content

Commit

Permalink
Pretty-print parenthesis around binary in postfix match
Browse files Browse the repository at this point in the history
Signed-off-by: Sasha Pourcelot <sasha.pourcelot@protonmail.com>
  • Loading branch information
scrabsha committed Apr 22, 2024
1 parent 7f2fc33 commit 027d6f0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
11 changes: 11 additions & 0 deletions compiler/rustc_ast_pretty/src/pprust/state/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,18 @@ impl<'a> State<'a> {
self.space();
}
MatchKind::Postfix => {
let need_par = matches!(expr.kind, ast::ExprKind::Binary(_, _, _));

if need_par {
// avoid printing `a + b.match {}`, print
// `(a + b).match {}` instead.
self.popen();
}
self.print_expr_as_cond(expr);
if need_par {
self.pclose();
}

self.word_nbsp(".match");
}
}
Expand Down
14 changes: 14 additions & 0 deletions tests/pretty/postfix-match/issue-124206.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![feature(prelude_import)]
#![no_std]
#![feature(postfix_match)]
#[prelude_import]
use ::std::prelude::rust_2015::*;
#[macro_use]
extern crate std;

//@ pretty-mode:expanded
//@ pp-exact:issue-124206.pp

macro_rules! repro { ($e:expr) => { $e.match { _ => {} } }; }

pub fn main() { ({ 1 } + 1).match { _ => {} }; }
16 changes: 16 additions & 0 deletions tests/pretty/postfix-match/issue-124206.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#![feature(postfix_match)]

//@ pretty-mode:expanded
//@ pp-exact:issue-124206.pp

macro_rules! repro {
($e:expr) => {
$e.match {
_ => {}
}
};
}

pub fn main() {
repro!({ 1 } + 1);
}
File renamed without changes.

0 comments on commit 027d6f0

Please sign in to comment.