Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc_ast: remove ref patterns #104500

Merged
merged 1 commit into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 22 additions & 22 deletions compiler/rustc_ast/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,9 @@ impl GenericArgs {
}

pub fn span(&self) -> Span {
match *self {
AngleBracketed(ref data) => data.span,
Parenthesized(ref data) => data.span,
match self {
AngleBracketed(data) => data.span,
Parenthesized(data) => data.span,
}
}
}
Expand Down Expand Up @@ -312,8 +312,8 @@ pub enum GenericBound {
impl GenericBound {
pub fn span(&self) -> Span {
match self {
GenericBound::Trait(ref t, ..) => t.span,
GenericBound::Outlives(ref l) => l.ident.span,
GenericBound::Trait(t, ..) => t.span,
GenericBound::Outlives(l) => l.ident.span,
}
}
}
Expand Down Expand Up @@ -1115,23 +1115,23 @@ impl Expr {
/// If this is not the case, name resolution does not resolve `N` when using
/// `min_const_generics` as more complex expressions are not supported.
pub fn is_potential_trivial_const_param(&self) -> bool {
let this = if let ExprKind::Block(ref block, None) = self.kind {
if block.stmts.len() == 1 {
if let StmtKind::Expr(ref expr) = block.stmts[0].kind { expr } else { self }
} else {
self
}
let this = if let ExprKind::Block(block, None) = &self.kind
&& block.stmts.len() == 1
&& let StmtKind::Expr(expr) = &block.stmts[0].kind
{
expr
} else {
self
};

if let ExprKind::Path(None, ref path) = this.kind {
if path.segments.len() == 1 && path.segments[0].args.is_none() {
return true;
}
if let ExprKind::Path(None, path) = &this.kind
&& path.segments.len() == 1
&& path.segments[0].args.is_none()
{
true
} else {
false
}

false
}

pub fn to_bound(&self) -> Option<GenericBound> {
Expand Down Expand Up @@ -2393,9 +2393,9 @@ pub enum FnRetTy {

impl FnRetTy {
pub fn span(&self) -> Span {
match *self {
FnRetTy::Default(span) => span,
FnRetTy::Ty(ref ty) => ty.span,
match self {
&FnRetTy::Default(span) => span,
FnRetTy::Ty(ty) => ty.span,
}
}
}
Expand Down Expand Up @@ -2657,8 +2657,8 @@ pub enum VariantData {
impl VariantData {
/// Return the fields of this variant.
pub fn fields(&self) -> &[FieldDef] {
match *self {
VariantData::Struct(ref fields, ..) | VariantData::Tuple(ref fields, _) => fields,
match self {
VariantData::Struct(fields, ..) | VariantData::Tuple(fields, _) => fields,
_ => &[],
}
}
Expand Down
90 changes: 44 additions & 46 deletions compiler/rustc_ast/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,16 @@ impl MarkedAttrs {
impl NestedMetaItem {
/// Returns the `MetaItem` if `self` is a `NestedMetaItem::MetaItem`.
pub fn meta_item(&self) -> Option<&MetaItem> {
match *self {
NestedMetaItem::MetaItem(ref item) => Some(item),
match self {
NestedMetaItem::MetaItem(item) => Some(item),
_ => None,
}
}

/// Returns the `Lit` if `self` is a `NestedMetaItem::Literal`s.
pub fn literal(&self) -> Option<&Lit> {
match *self {
NestedMetaItem::Literal(ref lit) => Some(lit),
match self {
NestedMetaItem::Literal(lit) => Some(lit),
_ => None,
}
}
Expand Down Expand Up @@ -116,18 +116,18 @@ impl NestedMetaItem {
impl Attribute {
#[inline]
pub fn has_name(&self, name: Symbol) -> bool {
match self.kind {
AttrKind::Normal(ref normal) => normal.item.path == name,
match &self.kind {
AttrKind::Normal(normal) => normal.item.path == name,
AttrKind::DocComment(..) => false,
}
}

/// For a single-segment attribute, returns its name; otherwise, returns `None`.
pub fn ident(&self) -> Option<Ident> {
match self.kind {
AttrKind::Normal(ref normal) => {
if normal.item.path.segments.len() == 1 {
Some(normal.item.path.segments[0].ident)
match &self.kind {
AttrKind::Normal(normal) => {
if let [ident] = &*normal.item.path.segments {
Some(ident.ident)
} else {
None
}
Expand All @@ -140,17 +140,15 @@ impl Attribute {
}

pub fn value_str(&self) -> Option<Symbol> {
match self.kind {
AttrKind::Normal(ref normal) => {
normal.item.meta_kind().and_then(|kind| kind.value_str())
}
match &self.kind {
AttrKind::Normal(normal) => normal.item.meta_kind().and_then(|kind| kind.value_str()),
AttrKind::DocComment(..) => None,
}
}

pub fn meta_item_list(&self) -> Option<Vec<NestedMetaItem>> {
match self.kind {
AttrKind::Normal(ref normal) => match normal.item.meta_kind() {
match &self.kind {
AttrKind::Normal(normal) => match normal.item.meta_kind() {
Some(MetaItemKind::List(list)) => Some(list),
_ => None,
},
Expand Down Expand Up @@ -191,8 +189,8 @@ impl MetaItem {
}

pub fn meta_item_list(&self) -> Option<&[NestedMetaItem]> {
match self.kind {
MetaItemKind::List(ref l) => Some(&l[..]),
match &self.kind {
MetaItemKind::List(l) => Some(&**l),
_ => None,
}
}
Expand Down Expand Up @@ -268,9 +266,9 @@ impl Attribute {
/// * `#[doc = "doc"]` returns `Some("doc")`.
/// * `#[doc(...)]` returns `None`.
pub fn doc_str(&self) -> Option<Symbol> {
match self.kind {
AttrKind::DocComment(.., data) => Some(data),
AttrKind::Normal(ref normal) if normal.item.path == sym::doc => {
match &self.kind {
AttrKind::DocComment(.., data) => Some(*data),
AttrKind::Normal(normal) if normal.item.path == sym::doc => {
normal.item.meta_kind().and_then(|kind| kind.value_str())
}
_ => None,
Expand All @@ -282,8 +280,8 @@ impl Attribute {
}

pub fn get_normal_item(&self) -> &AttrItem {
match self.kind {
AttrKind::Normal(ref normal) => &normal.item,
match &self.kind {
AttrKind::Normal(normal) => &normal.item,
AttrKind::DocComment(..) => panic!("unexpected doc comment"),
}
}
Expand All @@ -297,28 +295,28 @@ impl Attribute {

/// Extracts the MetaItem from inside this Attribute.
pub fn meta(&self) -> Option<MetaItem> {
match self.kind {
AttrKind::Normal(ref normal) => normal.item.meta(self.span),
match &self.kind {
AttrKind::Normal(normal) => normal.item.meta(self.span),
AttrKind::DocComment(..) => None,
}
}

pub fn meta_kind(&self) -> Option<MetaItemKind> {
match self.kind {
AttrKind::Normal(ref normal) => normal.item.meta_kind(),
match &self.kind {
AttrKind::Normal(normal) => normal.item.meta_kind(),
AttrKind::DocComment(..) => None,
}
}

pub fn tokens(&self) -> TokenStream {
match self.kind {
AttrKind::Normal(ref normal) => normal
match &self.kind {
AttrKind::Normal(normal) => normal
.tokens
.as_ref()
.unwrap_or_else(|| panic!("attribute is missing tokens: {:?}", self))
.to_attr_token_stream()
.to_tokenstream(),
AttrKind::DocComment(comment_kind, data) => TokenStream::new(vec![TokenTree::Token(
&AttrKind::DocComment(comment_kind, data) => TokenStream::new(vec![TokenTree::Token(
Token::new(token::DocComment(comment_kind, self.style, data), self.span),
Spacing::Alone,
)]),
Expand Down Expand Up @@ -496,17 +494,17 @@ impl MetaItem {
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
Path { span, segments, tokens: None }
}
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match *nt {
token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
token::Nonterminal::NtPath(ref path) => (**path).clone(),
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. }, _)) => match &*nt {
token::Nonterminal::NtMeta(item) => return item.meta(item.path.span),
token::Nonterminal::NtPath(path) => (**path).clone(),
_ => return None,
},
_ => return None,
};
let list_closing_paren_pos = tokens.peek().map(|tt| tt.span().hi());
let kind = MetaItemKind::from_tokens(tokens)?;
let hi = match kind {
MetaItemKind::NameValue(ref lit) => lit.span.hi(),
let hi = match &kind {
MetaItemKind::NameValue(lit) => lit.span.hi(),
MetaItemKind::List(..) => list_closing_paren_pos.unwrap_or(path.span.hi()),
_ => path.span.hi(),
};
Expand All @@ -518,8 +516,8 @@ impl MetaItem {
impl MetaItemKind {
pub fn value_str(&self) -> Option<Symbol> {
match self {
MetaItemKind::NameValue(ref v) => match v.kind {
LitKind::Str(ref s, _) => Some(*s),
MetaItemKind::NameValue(v) => match v.kind {
LitKind::Str(s, _) => Some(s),
_ => None,
},
_ => None,
Expand Down Expand Up @@ -557,15 +555,15 @@ impl MetaItemKind {
}

fn token_trees(&self, span: Span) -> Vec<TokenTree> {
match *self {
match self {
MetaItemKind::Word => vec![],
MetaItemKind::NameValue(ref lit) => {
MetaItemKind::NameValue(lit) => {
vec![
TokenTree::token_alone(token::Eq, span),
TokenTree::Token(lit.to_token(), Spacing::Alone),
]
}
MetaItemKind::List(ref list) => {
MetaItemKind::List(list) => {
let mut tokens = Vec::new();
for (i, item) in list.iter().enumerate() {
if i > 0 {
Expand Down Expand Up @@ -648,16 +646,16 @@ impl MetaItemKind {

impl NestedMetaItem {
pub fn span(&self) -> Span {
match *self {
NestedMetaItem::MetaItem(ref item) => item.span,
NestedMetaItem::Literal(ref lit) => lit.span,
match self {
NestedMetaItem::MetaItem(item) => item.span,
NestedMetaItem::Literal(lit) => lit.span,
}
}

fn token_trees(&self) -> Vec<TokenTree> {
match *self {
NestedMetaItem::MetaItem(ref item) => item.token_trees(),
NestedMetaItem::Literal(ref lit) => {
match self {
NestedMetaItem::MetaItem(item) => item.token_trees(),
NestedMetaItem::Literal(lit) => {
vec![TokenTree::Token(lit.to_token(), Spacing::Alone)]
}
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_ast/src/mut_visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,15 +439,15 @@ pub fn noop_visit_constraint<T: MutVisitor>(
) {
vis.visit_id(id);
vis.visit_ident(ident);
if let Some(ref mut gen_args) = gen_args {
if let Some(gen_args) = gen_args {
vis.visit_generic_args(gen_args);
}
match kind {
AssocConstraintKind::Equality { ref mut term } => match term {
AssocConstraintKind::Equality { term } => match term {
Term::Ty(ty) => vis.visit_ty(ty),
Term::Const(c) => vis.visit_anon_const(c),
},
AssocConstraintKind::Bound { ref mut bounds } => visit_bounds(bounds, vis),
AssocConstraintKind::Bound { bounds } => visit_bounds(bounds, vis),
}
vis.visit_span(span);
}
Expand Down Expand Up @@ -880,7 +880,7 @@ pub fn noop_flat_map_generic_param<T: MutVisitor>(
let GenericParam { id, ident, attrs, bounds, kind, colon_span, is_placeholder: _ } = &mut param;
vis.visit_id(id);
vis.visit_ident(ident);
if let Some(ref mut colon_span) = colon_span {
if let Some(colon_span) = colon_span {
vis.visit_span(colon_span);
}
visit_attrs(attrs, vis);
Expand Down
8 changes: 5 additions & 3 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -601,17 +601,18 @@ impl Token {

/// Returns `true` if the token is an interpolated path.
fn is_path(&self) -> bool {
if let Interpolated(ref nt) = self.kind && let NtPath(..) = **nt {
if let Interpolated(nt) = &self.kind && let NtPath(..) = **nt {
return true;
}

false
}

/// Would `maybe_whole_expr` in `parser.rs` return `Ok(..)`?
/// That is, is this a pre-parsed expression dropped into the token stream
/// (which happens while parsing the result of macro expansion)?
pub fn is_whole_expr(&self) -> bool {
if let Interpolated(ref nt) = self.kind
if let Interpolated(nt) = &self.kind
&& let NtExpr(_) | NtLiteral(_) | NtPath(_) | NtBlock(_) = **nt
{
return true;
Expand All @@ -622,9 +623,10 @@ impl Token {

// Is the token an interpolated block (`$b:block`)?
pub fn is_whole_block(&self) -> bool {
if let Interpolated(ref nt) = self.kind && let NtBlock(..) = **nt {
if let Interpolated(nt) = &self.kind && let NtBlock(..) = **nt {
return true;
}

false
}

Expand Down
22 changes: 11 additions & 11 deletions compiler/rustc_ast/src/util/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,26 +377,26 @@ pub fn needs_par_as_let_scrutinee(order: i8) -> bool {
/// parens or other delimiters, e.g., `X { y: 1 }`, `X { y: 1 }.method()`, `foo == X { y: 1 }` and
/// `X { y: 1 } == foo` all do, but `(X { y: 1 }) == foo` does not.
pub fn contains_exterior_struct_lit(value: &ast::Expr) -> bool {
match value.kind {
match &value.kind {
ast::ExprKind::Struct(..) => true,

ast::ExprKind::Assign(ref lhs, ref rhs, _)
| ast::ExprKind::AssignOp(_, ref lhs, ref rhs)
| ast::ExprKind::Binary(_, ref lhs, ref rhs) => {
ast::ExprKind::Assign(lhs, rhs, _)
| ast::ExprKind::AssignOp(_, lhs, rhs)
| ast::ExprKind::Binary(_, lhs, rhs) => {
// X { y: 1 } + X { y: 2 }
contains_exterior_struct_lit(&lhs) || contains_exterior_struct_lit(&rhs)
}
ast::ExprKind::Await(ref x)
| ast::ExprKind::Unary(_, ref x)
| ast::ExprKind::Cast(ref x, _)
| ast::ExprKind::Type(ref x, _)
| ast::ExprKind::Field(ref x, _)
| ast::ExprKind::Index(ref x, _) => {
ast::ExprKind::Await(x)
| ast::ExprKind::Unary(_, x)
| ast::ExprKind::Cast(x, _)
| ast::ExprKind::Type(x, _)
| ast::ExprKind::Field(x, _)
| ast::ExprKind::Index(x, _) => {
// &X { y: 1 }, X { y: 1 }.y
contains_exterior_struct_lit(&x)
}

ast::ExprKind::MethodCall(box ast::MethodCall { ref receiver, .. }) => {
ast::ExprKind::MethodCall(box ast::MethodCall { receiver, .. }) => {
// X { y: 1 }.bar(...)
contains_exterior_struct_lit(&receiver)
}
Expand Down
Loading