rustc_ast_pretty: remove ref patterns

This commit is contained in:
Maybe Waffle 2022-11-29 08:08:26 +00:00
parent 07b86a94c7
commit 56ae9a1746
3 changed files with 214 additions and 216 deletions

View File

@ -11,7 +11,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
use rustc_ast::util::classify;
use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle};
use rustc_ast::util::parser;
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, BlockCheckMode, Mutability, PatKind};
use rustc_ast::{self as ast, AttrArgs, AttrArgsEq, BlockCheckMode, PatKind};
use rustc_ast::{attr, BindingAnnotation, ByRef, DelimArgs, RangeEnd, RangeSyntax, Term};
use rustc_ast::{GenericArg, GenericBound, SelfKind, TraitBoundModifier};
use rustc_ast::{InlineAsmOperand, InlineAsmRegOrRegClass};
@ -64,6 +64,7 @@ impl<'a> Comments<'a> {
Comments { sm, comments, current: 0 }
}
// This shouldn't probably clone lmao
pub fn next(&self) -> Option<Comment> {
self.comments.get(self.current).cloned()
}
@ -268,10 +269,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
fn maybe_print_comment(&mut self, pos: BytePos) -> bool {
let mut has_comment = false;
while let Some(ref cmnt) = self.next_comment() {
while let Some(cmnt) = self.next_comment() {
if cmnt.pos < pos {
has_comment = true;
self.print_comment(cmnt);
self.print_comment(&cmnt);
} else {
break;
}
@ -366,8 +367,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
if self.next_comment().is_none() {
self.hardbreak();
}
while let Some(ref cmnt) = self.next_comment() {
self.print_comment(cmnt)
while let Some(cmnt) = self.next_comment() {
self.print_comment(&cmnt)
}
}
@ -446,8 +447,8 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.hardbreak_if_not_bol();
}
self.maybe_print_comment(attr.span.lo());
match attr.kind {
ast::AttrKind::Normal(ref normal) => {
match &attr.kind {
ast::AttrKind::Normal(normal) => {
match attr.style {
ast::AttrStyle::Inner => self.word("#!["),
ast::AttrStyle::Outer => self.word("#["),
@ -456,7 +457,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
self.word("]");
}
ast::AttrKind::DocComment(comment_kind, data) => {
self.word(doc_comment_to_string(comment_kind, attr.style, data));
self.word(doc_comment_to_string(*comment_kind, attr.style, *data));
self.hardbreak()
}
}
@ -497,22 +498,22 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) {
match item {
ast::NestedMetaItem::MetaItem(ref mi) => self.print_meta_item(mi),
ast::NestedMetaItem::Lit(ref lit) => self.print_meta_item_lit(lit),
ast::NestedMetaItem::MetaItem(mi) => self.print_meta_item(mi),
ast::NestedMetaItem::Lit(lit) => self.print_meta_item_lit(lit),
}
}
fn print_meta_item(&mut self, item: &ast::MetaItem) {
self.ibox(INDENT_UNIT);
match item.kind {
match &item.kind {
ast::MetaItemKind::Word => self.print_path(&item.path, false, 0),
ast::MetaItemKind::NameValue(ref value) => {
ast::MetaItemKind::NameValue(value) => {
self.print_path(&item.path, false, 0);
self.space();
self.word_space("=");
self.print_meta_item_lit(value);
}
ast::MetaItemKind::List(ref items) => {
ast::MetaItemKind::List(items) => {
self.print_path(&item.path, false, 0);
self.popen();
self.commasep(Consistent, &items, |s, i| s.print_meta_list_item(i));
@ -657,7 +658,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
fn print_path_segment(&mut self, segment: &ast::PathSegment, colons_before_params: bool) {
if segment.ident.name != kw::PathRoot {
self.print_ident(segment.ident);
if let Some(ref args) = segment.args {
if let Some(args) = &segment.args {
self.print_generic_args(args, colons_before_params);
}
}
@ -712,19 +713,19 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
}
fn nonterminal_to_string(&self, nt: &Nonterminal) -> String {
match *nt {
token::NtExpr(ref e) => self.expr_to_string(e),
token::NtMeta(ref e) => self.attr_item_to_string(e),
token::NtTy(ref e) => self.ty_to_string(e),
token::NtPath(ref e) => self.path_to_string(e),
token::NtItem(ref e) => self.item_to_string(e),
token::NtBlock(ref e) => self.block_to_string(e),
token::NtStmt(ref e) => self.stmt_to_string(e),
token::NtPat(ref e) => self.pat_to_string(e),
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(e, is_raw).to_string(),
match nt {
token::NtExpr(e) => self.expr_to_string(e),
token::NtMeta(e) => self.attr_item_to_string(e),
token::NtTy(e) => self.ty_to_string(e),
token::NtPath(e) => self.path_to_string(e),
token::NtItem(e) => self.item_to_string(e),
token::NtBlock(e) => self.block_to_string(e),
token::NtStmt(e) => self.stmt_to_string(e),
token::NtPat(e) => self.pat_to_string(e),
token::NtIdent(e, is_raw) => IdentPrinter::for_ast_ident(*e, *is_raw).to_string(),
token::NtLifetime(e) => e.to_string(),
token::NtLiteral(ref e) => self.expr_to_string(e),
token::NtVis(ref e) => self.vis_to_string(e),
token::NtLiteral(e) => self.expr_to_string(e),
token::NtVis(e) => self.vis_to_string(e),
}
}
@ -917,8 +918,8 @@ impl<'a> PrintState<'a> for State<'a> {
self.word("::")
}
match *args {
ast::GenericArgs::AngleBracketed(ref data) => {
match args {
ast::GenericArgs::AngleBracketed(data) => {
self.word("<");
self.commasep(Inconsistent, &data.args, |s, arg| match arg {
ast::AngleBracketedArg::Arg(a) => s.print_generic_arg(a),
@ -927,7 +928,7 @@ impl<'a> PrintState<'a> for State<'a> {
self.word(">")
}
ast::GenericArgs::Parenthesized(ref data) => {
ast::GenericArgs::Parenthesized(data) => {
self.word("(");
self.commasep(Inconsistent, &data.inputs, |s, ty| s.print_type(ty));
self.word(")");
@ -1011,17 +1012,17 @@ impl<'a> State<'a> {
pub fn print_type(&mut self, ty: &ast::Ty) {
self.maybe_print_comment(ty.span.lo());
self.ibox(0);
match ty.kind {
ast::TyKind::Slice(ref ty) => {
match &ty.kind {
ast::TyKind::Slice(ty) => {
self.word("[");
self.print_type(ty);
self.word("]");
}
ast::TyKind::Ptr(ref mt) => {
ast::TyKind::Ptr(mt) => {
self.word("*");
self.print_mt(mt, true);
}
ast::TyKind::Rptr(ref lifetime, ref mt) => {
ast::TyKind::Rptr(lifetime, mt) => {
self.word("&");
self.print_opt_lifetime(lifetime);
self.print_mt(mt, false);
@ -1029,7 +1030,7 @@ impl<'a> State<'a> {
ast::TyKind::Never => {
self.word("!");
}
ast::TyKind::Tup(ref elts) => {
ast::TyKind::Tup(elts) => {
self.popen();
self.commasep(Inconsistent, &elts, |s, ty| s.print_type(ty));
if elts.len() == 1 {
@ -1037,36 +1038,36 @@ impl<'a> State<'a> {
}
self.pclose();
}
ast::TyKind::Paren(ref typ) => {
ast::TyKind::Paren(typ) => {
self.popen();
self.print_type(typ);
self.pclose();
}
ast::TyKind::BareFn(ref f) => {
ast::TyKind::BareFn(f) => {
self.print_ty_fn(f.ext, f.unsafety, &f.decl, None, &f.generic_params);
}
ast::TyKind::Path(None, ref path) => {
ast::TyKind::Path(None, path) => {
self.print_path(path, false, 0);
}
ast::TyKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, false),
ast::TyKind::TraitObject(ref bounds, syntax) => {
if syntax == ast::TraitObjectSyntax::Dyn {
ast::TyKind::Path(Some(qself), path) => self.print_qpath(path, qself, false),
ast::TyKind::TraitObject(bounds, syntax) => {
if *syntax == ast::TraitObjectSyntax::Dyn {
self.word_nbsp("dyn");
}
self.print_type_bounds(bounds);
}
ast::TyKind::ImplTrait(_, ref bounds) => {
ast::TyKind::ImplTrait(_, bounds) => {
self.word_nbsp("impl");
self.print_type_bounds(bounds);
}
ast::TyKind::Array(ref ty, ref length) => {
ast::TyKind::Array(ty, length) => {
self.word("[");
self.print_type(ty);
self.word("; ");
self.print_expr(&length.value);
self.word("]");
}
ast::TyKind::Typeof(ref e) => {
ast::TyKind::Typeof(e) => {
self.word("typeof(");
self.print_expr(&e.value);
self.word(")");
@ -1082,7 +1083,7 @@ impl<'a> State<'a> {
ast::TyKind::ImplicitSelf => {
self.word("Self");
}
ast::TyKind::MacCall(ref m) => {
ast::TyKind::MacCall(m) => {
self.print_mac(m);
}
ast::TyKind::CVarArgs => {
@ -1111,8 +1112,8 @@ impl<'a> State<'a> {
pub(crate) fn print_stmt(&mut self, st: &ast::Stmt) {
self.maybe_print_comment(st.span.lo());
match st.kind {
ast::StmtKind::Local(ref loc) => {
match &st.kind {
ast::StmtKind::Local(loc) => {
self.print_outer_attributes(&loc.attrs);
self.space_if_not_bol();
self.ibox(INDENT_UNIT);
@ -1135,15 +1136,15 @@ impl<'a> State<'a> {
self.word(";");
self.end(); // `let` ibox
}
ast::StmtKind::Item(ref item) => self.print_item(item),
ast::StmtKind::Expr(ref expr) => {
ast::StmtKind::Item(item) => self.print_item(item),
ast::StmtKind::Expr(expr) => {
self.space_if_not_bol();
self.print_expr_outer_attr_style(expr, false);
if classify::expr_requires_semi_to_be_stmt(expr) {
self.word(";");
}
}
ast::StmtKind::Semi(ref expr) => {
ast::StmtKind::Semi(expr) => {
self.space_if_not_bol();
self.print_expr_outer_attr_style(expr, false);
self.word(";");
@ -1152,7 +1153,7 @@ impl<'a> State<'a> {
self.space_if_not_bol();
self.word(";");
}
ast::StmtKind::MacCall(ref mac) => {
ast::StmtKind::MacCall(mac) => {
self.space_if_not_bol();
self.print_outer_attributes(&mac.attrs);
self.print_mac(&mac.mac);
@ -1193,8 +1194,8 @@ impl<'a> State<'a> {
let has_attrs = self.print_inner_attributes(attrs);
for (i, st) in blk.stmts.iter().enumerate() {
match st.kind {
ast::StmtKind::Expr(ref expr) if i == blk.stmts.len() - 1 => {
match &st.kind {
ast::StmtKind::Expr(expr) if i == blk.stmts.len() - 1 => {
self.maybe_print_comment(st.span.lo());
self.space_if_not_bol();
self.print_expr_outer_attr_style(expr, false);
@ -1362,7 +1363,7 @@ impl<'a> State<'a> {
pub(crate) fn print_local_decl(&mut self, loc: &ast::Local) {
self.print_pat(&loc.pat);
if let Some(ref ty) = loc.ty {
if let Some(ty) = &loc.ty {
self.word_space(":");
self.print_type(ty);
}
@ -1386,7 +1387,7 @@ impl<'a> State<'a> {
for item_segment in &path.segments[qself.position..] {
self.word("::");
self.print_ident(item_segment.ident);
if let Some(ref args) = item_segment.args {
if let Some(args) = &item_segment.args {
self.print_generic_args(args, colons_before_params)
}
}
@ -1397,23 +1398,23 @@ impl<'a> State<'a> {
self.ann.pre(self, AnnNode::Pat(pat));
/* Pat isn't normalized, but the beauty of it
is that it doesn't matter */
match pat.kind {
match &pat.kind {
PatKind::Wild => self.word("_"),
PatKind::Ident(BindingAnnotation(by_ref, mutbl), ident, ref sub) => {
if by_ref == ByRef::Yes {
PatKind::Ident(BindingAnnotation(by_ref, mutbl), ident, sub) => {
if *by_ref == ByRef::Yes {
self.word_nbsp("ref");
}
if mutbl == Mutability::Mut {
if mutbl.is_mut() {
self.word_nbsp("mut");
}
self.print_ident(ident);
if let Some(ref p) = *sub {
self.print_ident(*ident);
if let Some(p) = sub {
self.space();
self.word_space("@");
self.print_pat(p);
}
}
PatKind::TupleStruct(ref qself, ref path, ref elts) => {
PatKind::TupleStruct(qself, path, elts) => {
if let Some(qself) = qself {
self.print_qpath(path, qself, true);
} else {
@ -1423,16 +1424,16 @@ impl<'a> State<'a> {
self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p));
self.pclose();
}
PatKind::Or(ref pats) => {
PatKind::Or(pats) => {
self.strsep("|", true, Inconsistent, &pats, |s, p| s.print_pat(p));
}
PatKind::Path(None, ref path) => {
PatKind::Path(None, path) => {
self.print_path(path, true, 0);
}
PatKind::Path(Some(ref qself), ref path) => {
PatKind::Path(Some(qself), path) => {
self.print_qpath(path, qself, false);
}
PatKind::Struct(ref qself, ref path, ref fields, etc) => {
PatKind::Struct(qself, path, fields, etc) => {
if let Some(qself) = qself {
self.print_qpath(path, qself, true);
} else {
@ -1458,7 +1459,7 @@ impl<'a> State<'a> {
},
|f| f.pat.span,
);
if etc {
if *etc {
if !fields.is_empty() {
self.word_space(",");
}
@ -1469,7 +1470,7 @@ impl<'a> State<'a> {
}
self.word("}");
}
PatKind::Tuple(ref elts) => {
PatKind::Tuple(elts) => {
self.popen();
self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p));
if elts.len() == 1 {
@ -1477,13 +1478,13 @@ impl<'a> State<'a> {
}
self.pclose();
}
PatKind::Box(ref inner) => {
PatKind::Box(inner) => {
self.word("box ");
self.print_pat(inner);
}
PatKind::Ref(ref inner, mutbl) => {
PatKind::Ref(inner, mutbl) => {
self.word("&");
if mutbl == Mutability::Mut {
if mutbl.is_mut() {
self.word("mut ");
}
if let PatKind::Ident(ast::BindingAnnotation::MUT, ..) = inner.kind {
@ -1494,12 +1495,12 @@ impl<'a> State<'a> {
self.print_pat(inner);
}
}
PatKind::Lit(ref e) => self.print_expr(&**e),
PatKind::Range(ref begin, ref end, Spanned { node: ref end_kind, .. }) => {
PatKind::Lit(e) => self.print_expr(&**e),
PatKind::Range(begin, end, Spanned { node: end_kind, .. }) => {
if let Some(e) = begin {
self.print_expr(e);
}
match *end_kind {
match end_kind {
RangeEnd::Included(RangeSyntax::DotDotDot) => self.word("..."),
RangeEnd::Included(RangeSyntax::DotDotEq) => self.word("..="),
RangeEnd::Excluded => self.word(".."),
@ -1508,36 +1509,36 @@ impl<'a> State<'a> {
self.print_expr(e);
}
}
PatKind::Slice(ref elts) => {
PatKind::Slice(elts) => {
self.word("[");
self.commasep(Inconsistent, &elts, |s, p| s.print_pat(p));
self.word("]");
}
PatKind::Rest => self.word(".."),
PatKind::Paren(ref inner) => {
PatKind::Paren(inner) => {
self.popen();
self.print_pat(inner);
self.pclose();
}
PatKind::MacCall(ref m) => self.print_mac(m),
PatKind::MacCall(m) => self.print_mac(m),
}
self.ann.post(self, AnnNode::Pat(pat))
}
fn print_explicit_self(&mut self, explicit_self: &ast::ExplicitSelf) {
match explicit_self.node {
match &explicit_self.node {
SelfKind::Value(m) => {
self.print_mutability(m, false);
self.print_mutability(*m, false);
self.word("self")
}
SelfKind::Region(ref lt, m) => {
SelfKind::Region(lt, m) => {
self.word("&");
self.print_opt_lifetime(lt);
self.print_mutability(m, false);
self.print_mutability(*m, false);
self.word("self")
}
SelfKind::Explicit(ref typ, m) => {
self.print_mutability(m, false);
SelfKind::Explicit(typ, m) => {
self.print_mutability(*m, false);
self.word("self");
self.word_space(":");
self.print_type(typ)
@ -1599,7 +1600,7 @@ impl<'a> State<'a> {
self.commasep(Inconsistent, &generic_params, |s, param| {
s.print_outer_attributes_inline(&param.attrs);
match param.kind {
match &param.kind {
ast::GenericParamKind::Lifetime => {
let lt = ast::Lifetime { id: param.id, ident: param.ident };
s.print_lifetime(lt);
@ -1608,19 +1609,19 @@ impl<'a> State<'a> {
s.print_lifetime_bounds(&param.bounds)
}
}
ast::GenericParamKind::Type { ref default } => {
ast::GenericParamKind::Type { default } => {
s.print_ident(param.ident);
if !param.bounds.is_empty() {
s.word_nbsp(":");
s.print_type_bounds(&param.bounds);
}
if let Some(ref default) = default {
if let Some(default) = default {
s.space();
s.word_space("=");
s.print_type(default)
}
}
ast::GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
ast::GenericParamKind::Const { ty, default, .. } => {
s.word_space("const");
s.print_ident(param.ident);
s.space();
@ -1630,7 +1631,7 @@ impl<'a> State<'a> {
s.word_nbsp(":");
s.print_type_bounds(&param.bounds);
}
if let Some(ref default) = default {
if let Some(default) = default {
s.space();
s.word_space("=");
s.print_expr(&default.value);

View File

@ -8,9 +8,9 @@ use rustc_ast::{self as ast, BlockCheckMode};
impl<'a> State<'a> {
fn print_else(&mut self, els: Option<&ast::Expr>) {
if let Some(_else) = els {
match _else.kind {
match &_else.kind {
// Another `else if` block.
ast::ExprKind::If(ref i, ref then, ref e) => {
ast::ExprKind::If(i, then, e) => {
self.cbox(INDENT_UNIT - 1);
self.ibox(0);
self.word(" else if ");
@ -20,7 +20,7 @@ impl<'a> State<'a> {
self.print_else(e.as_deref())
}
// Final `else` block.
ast::ExprKind::Block(ref b, _) => {
ast::ExprKind::Block(b, _) => {
self.cbox(INDENT_UNIT - 1);
self.ibox(0);
self.word(" else ");
@ -202,7 +202,7 @@ impl<'a> State<'a> {
self.print_expr_maybe_paren(receiver, parser::PREC_POSTFIX);
self.word(".");
self.print_ident(segment.ident);
if let Some(ref args) = segment.args {
if let Some(args) = &segment.args {
self.print_generic_args(args, true);
}
self.print_call_post(base_args)
@ -284,73 +284,66 @@ impl<'a> State<'a> {
self.ibox(INDENT_UNIT);
self.ann.pre(self, AnnNode::Expr(expr));
match expr.kind {
ast::ExprKind::Box(ref expr) => {
match &expr.kind {
ast::ExprKind::Box(expr) => {
self.word_space("box");
self.print_expr_maybe_paren(expr, parser::PREC_PREFIX);
}
ast::ExprKind::Array(ref exprs) => {
ast::ExprKind::Array(exprs) => {
self.print_expr_vec(exprs);
}
ast::ExprKind::ConstBlock(ref anon_const) => {
ast::ExprKind::ConstBlock(anon_const) => {
self.print_expr_anon_const(anon_const, attrs);
}
ast::ExprKind::Repeat(ref element, ref count) => {
ast::ExprKind::Repeat(element, count) => {
self.print_expr_repeat(element, count);
}
ast::ExprKind::Struct(ref se) => {
ast::ExprKind::Struct(se) => {
self.print_expr_struct(&se.qself, &se.path, &se.fields, &se.rest);
}
ast::ExprKind::Tup(ref exprs) => {
ast::ExprKind::Tup(exprs) => {
self.print_expr_tup(exprs);
}
ast::ExprKind::Call(ref func, ref args) => {
ast::ExprKind::Call(func, args) => {
self.print_expr_call(func, &args);
}
ast::ExprKind::MethodCall(box ast::MethodCall {
ref seg,
ref receiver,
ref args,
..
}) => {
ast::ExprKind::MethodCall(box ast::MethodCall { seg, receiver, args, .. }) => {
self.print_expr_method_call(seg, &receiver, &args);
}
ast::ExprKind::Binary(op, ref lhs, ref rhs) => {
self.print_expr_binary(op, lhs, rhs);
ast::ExprKind::Binary(op, lhs, rhs) => {
self.print_expr_binary(*op, lhs, rhs);
}
ast::ExprKind::Unary(op, ref expr) => {
self.print_expr_unary(op, expr);
ast::ExprKind::Unary(op, expr) => {
self.print_expr_unary(*op, expr);
}
ast::ExprKind::AddrOf(k, m, ref expr) => {
self.print_expr_addr_of(k, m, expr);
ast::ExprKind::AddrOf(k, m, expr) => {
self.print_expr_addr_of(*k, *m, expr);
}
ast::ExprKind::Lit(token_lit) => {
self.print_token_literal(token_lit, expr.span);
self.print_token_literal(*token_lit, expr.span);
}
ast::ExprKind::IncludedBytes(ref bytes) => {
ast::ExprKind::IncludedBytes(bytes) => {
let lit = ast::LitKind::ByteStr(bytes.clone()).to_token_lit();
self.print_token_literal(lit, expr.span)
}
ast::ExprKind::Cast(ref expr, ref ty) => {
ast::ExprKind::Cast(expr, ty) => {
let prec = AssocOp::As.precedence() as i8;
self.print_expr_maybe_paren(expr, prec);
self.space();
self.word_space("as");
self.print_type(ty);
}
ast::ExprKind::Type(ref expr, ref ty) => {
ast::ExprKind::Type(expr, ty) => {
let prec = AssocOp::Colon.precedence() as i8;
self.print_expr_maybe_paren(expr, prec);
self.word_space(":");
self.print_type(ty);
}
ast::ExprKind::Let(ref pat, ref scrutinee, _) => {
ast::ExprKind::Let(pat, scrutinee, _) => {
self.print_let(pat, scrutinee);
}
ast::ExprKind::If(ref test, ref blk, ref elseopt) => {
self.print_if(test, blk, elseopt.as_deref())
}
ast::ExprKind::While(ref test, ref blk, opt_label) => {
ast::ExprKind::If(test, blk, elseopt) => self.print_if(test, blk, elseopt.as_deref()),
ast::ExprKind::While(test, blk, opt_label) => {
if let Some(label) = opt_label {
self.print_ident(label.ident);
self.word_space(":");
@ -362,7 +355,7 @@ impl<'a> State<'a> {
self.space();
self.print_block_with_attrs(blk, attrs);
}
ast::ExprKind::ForLoop(ref pat, ref iter, ref blk, opt_label) => {
ast::ExprKind::ForLoop(pat, iter, blk, opt_label) => {
if let Some(label) = opt_label {
self.print_ident(label.ident);
self.word_space(":");
@ -377,7 +370,7 @@ impl<'a> State<'a> {
self.space();
self.print_block_with_attrs(blk, attrs);
}
ast::ExprKind::Loop(ref blk, opt_label, _) => {
ast::ExprKind::Loop(blk, opt_label, _) => {
if let Some(label) = opt_label {
self.print_ident(label.ident);
self.word_space(":");
@ -387,7 +380,7 @@ impl<'a> State<'a> {
self.word_nbsp("loop");
self.print_block_with_attrs(blk, attrs);
}
ast::ExprKind::Match(ref expr, ref arms) => {
ast::ExprKind::Match(expr, arms) => {
self.cbox(0);
self.ibox(0);
self.word_nbsp("match");
@ -402,18 +395,18 @@ impl<'a> State<'a> {
self.bclose(expr.span, empty);
}
ast::ExprKind::Closure(box ast::Closure {
ref binder,
binder,
capture_clause,
asyncness,
movability,
ref fn_decl,
ref body,
fn_decl,
body,
fn_decl_span: _,
}) => {
self.print_closure_binder(binder);
self.print_movability(movability);
self.print_asyncness(asyncness);
self.print_capture_clause(capture_clause);
self.print_movability(*movability);
self.print_asyncness(*asyncness);
self.print_capture_clause(*capture_clause);
self.print_fn_params_and_ret(fn_decl, true);
self.space();
@ -425,7 +418,7 @@ impl<'a> State<'a> {
// empty box to satisfy the close.
self.ibox(0);
}
ast::ExprKind::Block(ref blk, opt_label) => {
ast::ExprKind::Block(blk, opt_label) => {
if let Some(label) = opt_label {
self.print_ident(label.ident);
self.word_space(":");
@ -436,26 +429,26 @@ impl<'a> State<'a> {
self.ibox(0);
self.print_block_with_attrs(blk, attrs);
}
ast::ExprKind::Async(capture_clause, _, ref blk) => {
ast::ExprKind::Async(capture_clause, _, blk) => {
self.word_nbsp("async");
self.print_capture_clause(capture_clause);
self.print_capture_clause(*capture_clause);
// cbox/ibox in analogy to the `ExprKind::Block` arm above
self.cbox(0);
self.ibox(0);
self.print_block_with_attrs(blk, attrs);
}
ast::ExprKind::Await(ref expr) => {
ast::ExprKind::Await(expr) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
self.word(".await");
}
ast::ExprKind::Assign(ref lhs, ref rhs, _) => {
ast::ExprKind::Assign(lhs, rhs, _) => {
let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_maybe_paren(lhs, prec + 1);
self.space();
self.word_space("=");
self.print_expr_maybe_paren(rhs, prec);
}
ast::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
ast::ExprKind::AssignOp(op, lhs, rhs) => {
let prec = AssocOp::Assign.precedence() as i8;
self.print_expr_maybe_paren(lhs, prec + 1);
self.space();
@ -463,45 +456,44 @@ impl<'a> State<'a> {
self.word_space("=");
self.print_expr_maybe_paren(rhs, prec);
}
ast::ExprKind::Field(ref expr, ident) => {
ast::ExprKind::Field(expr, ident) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
self.word(".");
self.print_ident(ident);
self.print_ident(*ident);
}
ast::ExprKind::Index(ref expr, ref index) => {
ast::ExprKind::Index(expr, index) => {
self.print_expr_maybe_paren(expr, parser::PREC_POSTFIX);
self.word("[");
self.print_expr(index);
self.word("]");
}
ast::ExprKind::Range(ref start, ref end, limits) => {
ast::ExprKind::Range(start, end, limits) => {
// Special case for `Range`. `AssocOp` claims that `Range` has higher precedence
// than `Assign`, but `x .. x = x` gives a parse error instead of `x .. (x = x)`.
// Here we use a fake precedence value so that any child with lower precedence than
// a "normal" binop gets parenthesized. (`LOr` is the lowest-precedence binop.)
let fake_prec = AssocOp::LOr.precedence() as i8;
if let Some(ref e) = *start {
if let Some(e) = start {
self.print_expr_maybe_paren(e, fake_prec);
}
if limits == ast::RangeLimits::HalfOpen {
self.word("..");
} else {
self.word("..=");
match limits {
ast::RangeLimits::HalfOpen => self.word(".."),
ast::RangeLimits::Closed => self.word("..="),
}
if let Some(ref e) = *end {
if let Some(e) = end {
self.print_expr_maybe_paren(e, fake_prec);
}
}
ast::ExprKind::Underscore => self.word("_"),
ast::ExprKind::Path(None, ref path) => self.print_path(path, true, 0),
ast::ExprKind::Path(Some(ref qself), ref path) => self.print_qpath(path, qself, true),
ast::ExprKind::Break(opt_label, ref opt_expr) => {
ast::ExprKind::Path(None, path) => self.print_path(path, true, 0),
ast::ExprKind::Path(Some(qself), path) => self.print_qpath(path, qself, true),
ast::ExprKind::Break(opt_label, opt_expr) => {
self.word("break");
if let Some(label) = opt_label {
self.space();
self.print_ident(label.ident);
}
if let Some(ref expr) = *opt_expr {
if let Some(expr) = opt_expr {
self.space();
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
}
@ -513,45 +505,45 @@ impl<'a> State<'a> {
self.print_ident(label.ident);
}
}
ast::ExprKind::Ret(ref result) => {
ast::ExprKind::Ret(result) => {
self.word("return");
if let Some(ref expr) = *result {
if let Some(expr) = result {
self.word(" ");
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
}
}
ast::ExprKind::Yeet(ref result) => {
ast::ExprKind::Yeet(result) => {
self.word("do");
self.word(" ");
self.word("yeet");
if let Some(ref expr) = *result {
if let Some(expr) = result {
self.word(" ");
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
}
}
ast::ExprKind::InlineAsm(ref a) => {
ast::ExprKind::InlineAsm(a) => {
self.word("asm!");
self.print_inline_asm(a);
}
ast::ExprKind::MacCall(ref m) => self.print_mac(m),
ast::ExprKind::Paren(ref e) => {
ast::ExprKind::MacCall(m) => self.print_mac(m),
ast::ExprKind::Paren(e) => {
self.popen();
self.print_expr(e);
self.pclose();
}
ast::ExprKind::Yield(ref e) => {
ast::ExprKind::Yield(e) => {
self.word("yield");
if let Some(ref expr) = *e {
if let Some(expr) = e {
self.space();
self.print_expr_maybe_paren(expr, parser::PREC_JUMP);
}
}
ast::ExprKind::Try(ref e) => {
ast::ExprKind::Try(e) => {
self.print_expr_maybe_paren(e, parser::PREC_POSTFIX);
self.word("?")
}
ast::ExprKind::TryBlock(ref blk) => {
ast::ExprKind::TryBlock(blk) => {
self.cbox(0);
self.ibox(0);
self.word_nbsp("try");
@ -578,15 +570,15 @@ impl<'a> State<'a> {
self.print_outer_attributes(&arm.attrs);
self.print_pat(&arm.pat);
self.space();
if let Some(ref e) = arm.guard {
if let Some(e) = &arm.guard {
self.word_space("if");
self.print_expr(e);
self.space();
}
self.word_space("=>");
match arm.body.kind {
ast::ExprKind::Block(ref blk, opt_label) => {
match &arm.body.kind {
ast::ExprKind::Block(blk, opt_label) => {
if let Some(label) = opt_label {
self.print_ident(label.ident);
self.word_space(":");

View File

@ -136,10 +136,10 @@ impl<'a> State<'a> {
self.maybe_print_comment(item.span.lo());
self.print_outer_attributes(&item.attrs);
self.ann.pre(self, AnnNode::Item(item));
match item.kind {
match &item.kind {
ast::ItemKind::ExternCrate(orig_name) => {
self.head(visibility_qualified(&item.vis, "extern crate"));
if let Some(orig_name) = orig_name {
if let &Some(orig_name) = orig_name {
self.print_name(orig_name);
self.space();
self.word("as");
@ -150,35 +150,41 @@ impl<'a> State<'a> {
self.end(); // end inner head-block
self.end(); // end outer head-block
}
ast::ItemKind::Use(ref tree) => {
ast::ItemKind::Use(tree) => {
self.print_visibility(&item.vis);
self.word_nbsp("use");
self.print_use_tree(tree);
self.word(";");
}
ast::ItemKind::Static(ref ty, mutbl, ref body) => {
ast::ItemKind::Static(ty, mutbl, body) => {
let def = ast::Defaultness::Final;
self.print_item_const(item.ident, Some(mutbl), ty, body.as_deref(), &item.vis, def);
self.print_item_const(
item.ident,
Some(*mutbl),
ty,
body.as_deref(),
&item.vis,
def,
);
}
ast::ItemKind::Const(def, ref ty, ref body) => {
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, def);
ast::ItemKind::Const(def, ty, body) => {
self.print_item_const(item.ident, None, ty, body.as_deref(), &item.vis, *def);
}
ast::ItemKind::Fn(box ast::Fn { defaultness, ref sig, ref generics, ref body }) => {
let body = body.as_deref();
ast::ItemKind::Fn(box ast::Fn { defaultness, sig, generics, body }) => {
self.print_fn_full(
sig,
item.ident,
generics,
&item.vis,
defaultness,
body,
*defaultness,
body.as_deref(),
&item.attrs,
);
}
ast::ItemKind::Mod(unsafety, ref mod_kind) => {
ast::ItemKind::Mod(unsafety, mod_kind) => {
self.head(Self::to_string(|s| {
s.print_visibility(&item.vis);
s.print_unsafety(unsafety);
s.print_unsafety(*unsafety);
s.word("mod");
}));
self.print_ident(item.ident);
@ -201,7 +207,7 @@ impl<'a> State<'a> {
}
}
}
ast::ItemKind::ForeignMod(ref nmod) => {
ast::ItemKind::ForeignMod(nmod) => {
self.head(Self::to_string(|s| {
s.print_unsafety(nmod.unsafety);
s.word("extern");
@ -215,7 +221,7 @@ impl<'a> State<'a> {
let empty = item.attrs.is_empty() && nmod.items.is_empty();
self.bclose(item.span, empty);
}
ast::ItemKind::GlobalAsm(ref asm) => {
ast::ItemKind::GlobalAsm(asm) => {
self.head(visibility_qualified(&item.vis, "global_asm!"));
self.print_inline_asm(asm);
self.word(";");
@ -224,32 +230,31 @@ impl<'a> State<'a> {
}
ast::ItemKind::TyAlias(box ast::TyAlias {
defaultness,
ref generics,
generics,
where_clauses,
where_predicates_split,
ref bounds,
ref ty,
bounds,
ty,
}) => {
let ty = ty.as_deref();
self.print_associated_type(
item.ident,
generics,
where_clauses,
where_predicates_split,
*where_clauses,
*where_predicates_split,
bounds,
ty,
ty.as_deref(),
&item.vis,
defaultness,
*defaultness,
);
}
ast::ItemKind::Enum(ref enum_definition, ref params) => {
ast::ItemKind::Enum(enum_definition, params) => {
self.print_enum_def(enum_definition, params, item.ident, item.span, &item.vis);
}
ast::ItemKind::Struct(ref struct_def, ref generics) => {
ast::ItemKind::Struct(struct_def, generics) => {
self.head(visibility_qualified(&item.vis, "struct"));
self.print_struct(struct_def, generics, item.ident, item.span, true);
}
ast::ItemKind::Union(ref struct_def, ref generics) => {
ast::ItemKind::Union(struct_def, generics) => {
self.head(visibility_qualified(&item.vis, "union"));
self.print_struct(struct_def, generics, item.ident, item.span, true);
}
@ -258,15 +263,15 @@ impl<'a> State<'a> {
polarity,
defaultness,
constness,
ref generics,
ref of_trait,
ref self_ty,
ref items,
generics,
of_trait,
self_ty,
items,
}) => {
self.head("");
self.print_visibility(&item.vis);
self.print_defaultness(defaultness);
self.print_unsafety(unsafety);
self.print_defaultness(*defaultness);
self.print_unsafety(*unsafety);
self.word("impl");
if generics.params.is_empty() {
@ -276,13 +281,13 @@ impl<'a> State<'a> {
self.space();
}
self.print_constness(constness);
self.print_constness(*constness);
if let ast::ImplPolarity::Negative(_) = polarity {
self.word("!");
}
if let Some(ref t) = *of_trait {
if let Some(t) = of_trait {
self.print_trait_ref(t);
self.space();
self.word_space("for");
@ -303,21 +308,21 @@ impl<'a> State<'a> {
ast::ItemKind::Trait(box ast::Trait {
is_auto,
unsafety,
ref generics,
ref bounds,
ref items,
generics,
bounds,
items,
..
}) => {
self.head("");
self.print_visibility(&item.vis);
self.print_unsafety(unsafety);
self.print_is_auto(is_auto);
self.print_unsafety(*unsafety);
self.print_is_auto(*is_auto);
self.word_nbsp("trait");
self.print_ident(item.ident);
self.print_generic_params(&generics.params);
let mut real_bounds = Vec::with_capacity(bounds.len());
for b in bounds.iter() {
if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
if let GenericBound::Trait(ptr, ast::TraitBoundModifier::Maybe) = b {
self.space();
self.word_space("for ?");
self.print_trait_ref(&ptr.trait_ref);
@ -339,14 +344,14 @@ impl<'a> State<'a> {
let empty = item.attrs.is_empty() && items.is_empty();
self.bclose(item.span, empty);
}
ast::ItemKind::TraitAlias(ref generics, ref bounds) => {
ast::ItemKind::TraitAlias(generics, bounds) => {
self.head(visibility_qualified(&item.vis, "trait"));
self.print_ident(item.ident);
self.print_generic_params(&generics.params);
let mut real_bounds = Vec::with_capacity(bounds.len());
// FIXME(durka) this seems to be some quite outdated syntax
for b in bounds.iter() {
if let GenericBound::Trait(ref ptr, ast::TraitBoundModifier::Maybe) = *b {
if let GenericBound::Trait(ptr, ast::TraitBoundModifier::Maybe) = b {
self.space();
self.word_space("for ?");
self.print_trait_ref(&ptr.trait_ref);
@ -364,13 +369,13 @@ impl<'a> State<'a> {
self.end(); // end inner head-block
self.end(); // end outer head-block
}
ast::ItemKind::MacCall(ref mac) => {
ast::ItemKind::MacCall(mac) => {
self.print_mac(mac);
if mac.args.need_semicolon() {
self.word(";");
}
}
ast::ItemKind::MacroDef(ref macro_def) => {
ast::ItemKind::MacroDef(macro_def) => {
self.print_mac_def(macro_def, &item.ident, item.span, |state| {
state.print_visibility(&item.vis)
});
@ -412,11 +417,11 @@ impl<'a> State<'a> {
}
pub(crate) fn print_visibility(&mut self, vis: &ast::Visibility) {
match vis.kind {
match &vis.kind {
ast::VisibilityKind::Public => self.word_nbsp("pub"),
ast::VisibilityKind::Restricted { ref path, id: _, shorthand } => {
ast::VisibilityKind::Restricted { path, shorthand, .. } => {
let path = Self::to_string(|s| s.print_path(path, false, 0));
if shorthand && (path == "crate" || path == "self" || path == "super") {
if *shorthand && (path == "crate" || path == "self" || path == "super") {
self.word_nbsp(format!("pub({})", path))
} else {
self.word_nbsp(format!("pub(in {})", path))
@ -465,7 +470,7 @@ impl<'a> State<'a> {
) {
self.print_ident(ident);
self.print_generic_params(&generics.params);
match struct_def {
match &struct_def {
ast::VariantData::Tuple(..) | ast::VariantData::Unit(..) => {
if let ast::VariantData::Tuple(..) = struct_def {
self.popen();
@ -484,7 +489,7 @@ impl<'a> State<'a> {
self.end();
self.end(); // Close the outer-box.
}
ast::VariantData::Struct(ref fields, ..) => {
ast::VariantData::Struct(fields, ..) => {
self.print_where_clause(&generics.where_clause);
self.print_record_struct_body(fields, span);
}
@ -496,7 +501,7 @@ impl<'a> State<'a> {
self.print_visibility(&v.vis);
let generics = ast::Generics::default();
self.print_struct(&v.data, &generics, v.ident, v.span, false);
if let Some(ref d) = v.disr_expr {
if let Some(d) = &v.disr_expr {
self.space();
self.word_space("=");
self.print_expr(&d.value)
@ -657,10 +662,10 @@ impl<'a> State<'a> {
}
fn print_use_tree(&mut self, tree: &ast::UseTree) {
match tree.kind {
match &tree.kind {
ast::UseTreeKind::Simple(rename, ..) => {
self.print_path(&tree.prefix, false, 0);
if let Some(rename) = rename {
if let &Some(rename) = rename {
self.nbsp();
self.word_nbsp("as");
self.print_ident(rename);
@ -673,7 +678,7 @@ impl<'a> State<'a> {
}
self.word("*");
}
ast::UseTreeKind::Nested(ref items) => {
ast::UseTreeKind::Nested(items) => {
if !tree.prefix.segments.is_empty() {
self.print_path(&tree.prefix, false, 0);
self.word("::");