Skip to content

Commit 5ec9ec1

Browse files
committed
Remove useless Option<Ident> arg.
`FmtVisitor::visit_mac` has an `Option<Ident>` arg which is always either `None` or `Some(kw::Empty)`, because `ItemKind::MacCall` always has an empty ident. This value is passed through various functions until it reaches `rewrite_macro_name`, which treats `None` and `Some(kw::Empty)` the same. In other words, the argument is useless. This commit removes it. There is no change in behaviour. The commit also changes a few `symbol::Ident` occurrences to `Ident` in `macros.rs`; `Symbol` is imported in that file so `Ident` might as well be, too. (This is a good example of why it's a bad idea for `Itemt` to have an `ident` field when various item kinds don't have an identifier. It's easy to get confused when "empty identifier" is used to mean "no identifier". This will be fixed in a subsequent commit.)
1 parent 8ca8228 commit 5ec9ec1

File tree

6 files changed

+16
-38
lines changed

6 files changed

+16
-38
lines changed

src/tools/rustfmt/src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ pub(crate) fn format_expr(
246246
| ast::ExprKind::Await(_, _)
247247
| ast::ExprKind::Yield(ast::YieldKind::Postfix(_)) => rewrite_chain(expr, context, shape),
248248
ast::ExprKind::MacCall(ref mac) => {
249-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|_| {
249+
rewrite_macro(mac, context, shape, MacroPosition::Expression).or_else(|_| {
250250
wrap_str(
251251
context.snippet(expr.span).to_owned(),
252252
context.config.max_width(),

src/tools/rustfmt/src/items.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3508,7 +3508,7 @@ impl Rewrite for ast::ForeignItem {
35083508
)
35093509
}
35103510
ast::ForeignItemKind::MacCall(ref mac) => {
3511-
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
3511+
rewrite_macro(mac, context, shape, MacroPosition::Item)
35123512
}
35133513
}?;
35143514

src/tools/rustfmt/src/macros.rs

+7-27
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,7 @@ use rustc_ast::token::{Delimiter, Token, TokenKind};
1616
use rustc_ast::tokenstream::{TokenStream, TokenStreamIter, TokenTree};
1717
use rustc_ast::{ast, ptr};
1818
use rustc_ast_pretty::pprust;
19-
use rustc_span::{
20-
BytePos, DUMMY_SP, Span, Symbol,
21-
symbol::{self, kw},
22-
};
19+
use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol};
2320
use tracing::debug;
2421

2522
use crate::comment::{
@@ -60,7 +57,7 @@ pub(crate) enum MacroArg {
6057
Ty(ptr::P<ast::Ty>),
6158
Pat(ptr::P<ast::Pat>),
6259
Item(ptr::P<ast::Item>),
63-
Keyword(symbol::Ident, Span),
60+
Keyword(Ident, Span),
6461
}
6562

6663
impl MacroArg {
@@ -103,20 +100,12 @@ impl Rewrite for MacroArg {
103100
}
104101

105102
/// Rewrite macro name without using pretty-printer if possible.
106-
fn rewrite_macro_name(
107-
context: &RewriteContext<'_>,
108-
path: &ast::Path,
109-
extra_ident: Option<symbol::Ident>,
110-
) -> String {
111-
let name = if path.segments.len() == 1 {
103+
fn rewrite_macro_name(context: &RewriteContext<'_>, path: &ast::Path) -> String {
104+
if path.segments.len() == 1 {
112105
// Avoid using pretty-printer in the common case.
113106
format!("{}!", rewrite_ident(context, path.segments[0].ident))
114107
} else {
115108
format!("{}!", pprust::path_to_string(path))
116-
};
117-
match extra_ident {
118-
Some(ident) if ident.name != kw::Empty => format!("{name} {ident}"),
119-
_ => name,
120109
}
121110
}
122111

@@ -165,7 +154,6 @@ fn return_macro_parse_failure_fallback(
165154

166155
pub(crate) fn rewrite_macro(
167156
mac: &ast::MacCall,
168-
extra_ident: Option<symbol::Ident>,
169157
context: &RewriteContext<'_>,
170158
shape: Shape,
171159
position: MacroPosition,
@@ -179,14 +167,7 @@ pub(crate) fn rewrite_macro(
179167
} else {
180168
let guard = context.enter_macro();
181169
let result = catch_unwind(AssertUnwindSafe(|| {
182-
rewrite_macro_inner(
183-
mac,
184-
extra_ident,
185-
context,
186-
shape,
187-
position,
188-
guard.is_nested(),
189-
)
170+
rewrite_macro_inner(mac, context, shape, position, guard.is_nested())
190171
}));
191172
match result {
192173
Err(..) => {
@@ -207,7 +188,6 @@ pub(crate) fn rewrite_macro(
207188

208189
fn rewrite_macro_inner(
209190
mac: &ast::MacCall,
210-
extra_ident: Option<symbol::Ident>,
211191
context: &RewriteContext<'_>,
212192
shape: Shape,
213193
position: MacroPosition,
@@ -222,7 +202,7 @@ fn rewrite_macro_inner(
222202

223203
let original_style = macro_style(mac, context);
224204

225-
let macro_name = rewrite_macro_name(context, &mac.path, extra_ident);
205+
let macro_name = rewrite_macro_name(context, &mac.path);
226206
let is_forced_bracket = FORCED_BRACKET_MACROS.contains(&&macro_name[..]);
227207

228208
let style = if is_forced_bracket && !is_nested_macro {
@@ -433,7 +413,7 @@ pub(crate) fn rewrite_macro_def(
433413
shape: Shape,
434414
indent: Indent,
435415
def: &ast::MacroDef,
436-
ident: symbol::Ident,
416+
ident: Ident,
437417
vis: &ast::Visibility,
438418
span: Span,
439419
) -> RewriteResult {

src/tools/rustfmt/src/patterns.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,7 @@ impl Rewrite for Pat {
307307
context,
308308
shape,
309309
),
310-
PatKind::MacCall(ref mac) => {
311-
rewrite_macro(mac, None, context, shape, MacroPosition::Pat)
312-
}
310+
PatKind::MacCall(ref mac) => rewrite_macro(mac, context, shape, MacroPosition::Pat),
313311
PatKind::Paren(ref pat) => pat
314312
.rewrite_result(
315313
context,

src/tools/rustfmt/src/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ impl Rewrite for ast::Ty {
10181018
ast::TyKind::BareFn(ref bare_fn) => rewrite_bare_fn(bare_fn, self.span, context, shape),
10191019
ast::TyKind::Never => Ok(String::from("!")),
10201020
ast::TyKind::MacCall(ref mac) => {
1021-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression)
1021+
rewrite_macro(mac, context, shape, MacroPosition::Expression)
10221022
}
10231023
ast::TyKind::ImplicitSelf => Ok(String::from("")),
10241024
ast::TyKind::ImplTrait(_, ref it) => {

src/tools/rustfmt/src/visitor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
172172
get_span_without_attrs(stmt.as_ast_node()),
173173
);
174174
} else {
175-
self.visit_mac(&mac_stmt.mac, None, MacroPosition::Statement);
175+
self.visit_mac(&mac_stmt.mac, MacroPosition::Statement);
176176
}
177177
self.format_missing(stmt.span().hi());
178178
}
@@ -531,7 +531,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
531531
self.format_mod(mod_kind, safety, &item.vis, item.span, item.ident, attrs);
532532
}
533533
ast::ItemKind::MacCall(ref mac) => {
534-
self.visit_mac(mac, Some(item.ident), MacroPosition::Item);
534+
self.visit_mac(mac, MacroPosition::Item);
535535
}
536536
ast::ItemKind::ForeignMod(ref foreign_mod) => {
537537
self.format_missing_with_indent(source!(self, item.span).lo());
@@ -677,7 +677,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
677677
self.visit_ty_alias_kind(ty_alias, &ai.vis, ai.ident, visitor_kind, ai.span);
678678
}
679679
(ast::AssocItemKind::MacCall(ref mac), _) => {
680-
self.visit_mac(mac, Some(ai.ident), MacroPosition::Item);
680+
self.visit_mac(mac, MacroPosition::Item);
681681
}
682682
_ => unreachable!(),
683683
}
@@ -691,12 +691,12 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
691691
self.visit_assoc_item(ii, ItemVisitorKind::AssocImplItem);
692692
}
693693

694-
fn visit_mac(&mut self, mac: &ast::MacCall, ident: Option<symbol::Ident>, pos: MacroPosition) {
694+
fn visit_mac(&mut self, mac: &ast::MacCall, pos: MacroPosition) {
695695
skip_out_of_file_lines_range_visitor!(self, mac.span());
696696

697697
// 1 = ;
698698
let shape = self.shape().saturating_sub_width(1);
699-
let rewrite = self.with_context(|ctx| rewrite_macro(mac, ident, ctx, shape, pos).ok());
699+
let rewrite = self.with_context(|ctx| rewrite_macro(mac, ctx, shape, pos).ok());
700700
// As of v638 of the rustc-ap-* crates, the associated span no longer includes
701701
// the trailing semicolon. This determines the correct span to ensure scenarios
702702
// with whitespace between the delimiters and trailing semi (i.e. `foo!(abc) ;`)

0 commit comments

Comments
 (0)