Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit bbbc1e8

Browse files
committed
refrect topecongiro reviews
- &Vec<syntax::ast::PathSegment> => &[ast::PathSegment] - remove unnecessary implements - transfer skip logic to inside rewrite_macro - fix test - use util methods in libsyntax - use meta_item_list directly - avoid no_entry.rs for test using module system - add logic to skip rustfmt::skip::macros only - remove base_skip_macro_names - remove Rc - use clone to append skip_macro_names
1 parent 558a2c3 commit bbbc1e8

File tree

15 files changed

+311
-215
lines changed

15 files changed

+311
-215
lines changed

src/expr.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -190,21 +190,13 @@ pub fn format_expr(
190190
rewrite_chain(expr, context, shape)
191191
}
192192
ast::ExprKind::Mac(ref mac) => {
193-
let should_skip = context
194-
.skip_macro_names
195-
.borrow()
196-
.contains(&context.snippet(mac.node.path.span).to_owned());
197-
if should_skip {
198-
None
199-
} else {
200-
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
201-
wrap_str(
202-
context.snippet(expr.span).to_owned(),
203-
context.config.max_width(),
204-
shape,
205-
)
206-
})
207-
}
193+
rewrite_macro(mac, None, context, shape, MacroPosition::Expression).or_else(|| {
194+
wrap_str(
195+
context.snippet(expr.span).to_owned(),
196+
context.config.max_width(),
197+
shape,
198+
)
199+
})
208200
}
209201
ast::ExprKind::Ret(None) => Some("return".to_owned()),
210202
ast::ExprKind::Ret(Some(ref expr)) => {
@@ -1928,7 +1920,6 @@ pub fn rewrite_assign_rhs_with<S: Into<String>, R: Rewrite>(
19281920
offset: shape.offset + last_line_width + 1,
19291921
..shape
19301922
});
1931-
// dbg!(
19321923
let rhs = choose_rhs(
19331924
context,
19341925
ex,

src/formatting.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use syntax::source_map::{FilePathMapping, SourceMap, Span, DUMMY_SP};
1515
use crate::comment::{CharClasses, FullCodeCharKind};
1616
use crate::config::{Config, FileName, Verbosity};
1717
use crate::issues::BadIssueSeeker;
18+
use crate::utils::{count_newlines, get_skip_macro_names};
1819
use crate::visitor::{FmtVisitor, SnippetProvider};
1920
use crate::{modules, source_file, ErrorKind, FormatReport, Input, Session};
2021

@@ -153,6 +154,10 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
153154
&snippet_provider,
154155
self.report.clone(),
155156
);
157+
visitor
158+
.skip_macro_names
159+
.borrow_mut()
160+
.append(&mut get_skip_macro_names(&self.krate.attrs));
156161

157162
// Format inner attributes if available.
158163
if !self.krate.attrs.is_empty() && is_root {
@@ -168,10 +173,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
168173
visitor.format_separate_mod(module, &*source_file);
169174
};
170175

171-
debug_assert_eq!(
172-
visitor.line_number,
173-
crate::utils::count_newlines(&visitor.buffer)
174-
);
176+
debug_assert_eq!(visitor.line_number, count_newlines(&visitor.buffer));
175177

176178
// For some reason, the source_map does not include terminating
177179
// newlines so we must add one on for each file. This is sad.

src/macros.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,12 +208,21 @@ pub fn rewrite_macro(
208208
shape: Shape,
209209
position: MacroPosition,
210210
) -> Option<String> {
211-
let guard = InsideMacroGuard::inside_macro_context(context);
212-
let result = rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested);
213-
if result.is_none() {
214-
context.macro_rewrite_failure.replace(true);
211+
let should_skip = context
212+
.skip_macro_names
213+
.borrow()
214+
.contains(&context.snippet(mac.node.path.span).to_owned());
215+
if should_skip {
216+
None
217+
} else {
218+
let guard = InsideMacroGuard::inside_macro_context(context);
219+
let result =
220+
rewrite_macro_inner(mac, extra_ident, context, shape, position, guard.is_nested);
221+
if result.is_none() {
222+
context.macro_rewrite_failure.replace(true);
223+
}
224+
result
215225
}
216-
result
217226
}
218227

219228
fn check_keyword<'a, 'b: 'a>(parser: &'a mut Parser<'b>) -> Option<MacroArg> {

src/rewrite.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// A generic trait to abstract the rewriting of an element (of the AST).
22

33
use std::cell::RefCell;
4-
use std::rc::Rc;
54

65
use syntax::parse::ParseSess;
76
use syntax::ptr;
@@ -40,7 +39,7 @@ pub struct RewriteContext<'a> {
4039
// Used for `format_snippet`
4140
pub(crate) macro_rewrite_failure: RefCell<bool>,
4241
pub(crate) report: FormatReport,
43-
pub skip_macro_names: Rc<RefCell<Vec<String>>>,
42+
pub skip_macro_names: RefCell<Vec<String>>,
4443
}
4544

4645
impl<'a> RewriteContext<'a> {

src/test/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ const SKIP_FILE_WHITE_LIST: &[&str] = &[
2222
// We want to make sure that the `skip_children` is correctly working,
2323
// so we do not want to test this file directly.
2424
"configs/skip_children/foo/mod.rs",
25+
"issue-3434/no_entry.rs",
2526
];
2627

2728
fn is_file_skip(path: &Path) -> bool {

src/utils.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,26 @@ pub(crate) fn unicode_str_width(s: &str) -> usize {
603603
s.width()
604604
}
605605

606+
pub fn get_skip_macro_names(attrs: &[ast::Attribute]) -> Vec<String> {
607+
let mut skip_macro_names = vec![];
608+
for attr in attrs {
609+
// syntax::ast::Path is implemented partialEq
610+
// but it is designed for segments.len() == 1
611+
if format!("{}", attr.path) != "rustfmt::skip::macros" {
612+
continue;
613+
}
614+
615+
if let Some(list) = attr.meta_item_list() {
616+
for spanned in list {
617+
if let Some(name) = spanned.name() {
618+
skip_macro_names.push(name.to_string());
619+
}
620+
}
621+
}
622+
}
623+
skip_macro_names
624+
}
625+
606626
#[cfg(test)]
607627
mod test {
608628
use super::*;

0 commit comments

Comments
 (0)