Skip to content

Commit 9e70541

Browse files
committed
refactor: extract not_testable_error into function
1 parent f4e7a99 commit 9e70541

File tree

1 file changed

+32
-32
lines changed
  • compiler/rustc_builtin_macros/src

1 file changed

+32
-32
lines changed

Diff for: compiler/rustc_builtin_macros/src/test.rs

+32-32
Original file line numberDiff line numberDiff line change
@@ -107,36 +107,6 @@ pub fn expand_test_or_bench(
107107
return vec![];
108108
}
109109

110-
let not_testable_error = |item: Option<&ast::Item>| {
111-
let diag = &cx.sess.parse_sess.span_diagnostic;
112-
let msg = "the `#[test]` attribute may only be used on a non-associated function";
113-
let mut err = match item.map(|i| &i.kind) {
114-
// These were a warning before #92959 and need to continue being that to avoid breaking
115-
// stable user code (#94508).
116-
Some(ast::ItemKind::MacCall(_)) => diag.struct_span_warn(attr_sp, msg),
117-
// `.forget_guarantee()` needed to get these two arms to match types. Because of how
118-
// locally close the `.emit()` call is I'm comfortable with it, but if it can be
119-
// reworked in the future to not need it, it'd be nice.
120-
_ => diag.struct_span_err(attr_sp, msg).forget_guarantee(),
121-
};
122-
if let Some(item) = item {
123-
err.span_label(
124-
item.span,
125-
format!(
126-
"expected a non-associated function, found {} {}",
127-
item.kind.article(),
128-
item.kind.descr()
129-
),
130-
);
131-
}
132-
err.span_label(attr_sp, "the `#[test]` macro causes a function to be run as a test and has no effect on non-functions")
133-
.span_suggestion(attr_sp,
134-
"replace with conditional compilation to make the item only exist when tests are being run",
135-
"#[cfg(test)]",
136-
Applicability::MaybeIncorrect)
137-
.emit();
138-
};
139-
140110
let (item, is_stmt) = match item {
141111
Annotatable::Item(i) => (i, false),
142112
Annotatable::Stmt(stmt) if matches!(stmt.kind, ast::StmtKind::Item(_)) => {
@@ -148,13 +118,13 @@ pub fn expand_test_or_bench(
148118
}
149119
}
150120
other => {
151-
not_testable_error(None);
121+
not_testable_error(cx, attr_sp, None);
152122
return vec![other];
153123
}
154124
};
155125

156126
let ast::ItemKind::Fn(fn_) = &item.kind else {
157-
not_testable_error(Some(&item));
127+
not_testable_error(cx, attr_sp, Some(&item));
158128
return if is_stmt {
159129
vec![Annotatable::Stmt(P(ast::Stmt {
160130
id: ast::DUMMY_NODE_ID,
@@ -416,6 +386,36 @@ pub fn expand_test_or_bench(
416386
}
417387
}
418388

389+
fn not_testable_error(cx: &ExtCtxt<'_>, attr_sp: Span, item: Option<&ast::Item>) {
390+
let diag = &cx.sess.parse_sess.span_diagnostic;
391+
let msg = "the `#[test]` attribute may only be used on a non-associated function";
392+
let mut err = match item.map(|i| &i.kind) {
393+
// These were a warning before #92959 and need to continue being that to avoid breaking
394+
// stable user code (#94508).
395+
Some(ast::ItemKind::MacCall(_)) => diag.struct_span_warn(attr_sp, msg),
396+
// `.forget_guarantee()` needed to get these two arms to match types. Because of how
397+
// locally close the `.emit()` call is I'm comfortable with it, but if it can be
398+
// reworked in the future to not need it, it'd be nice.
399+
_ => diag.struct_span_err(attr_sp, msg).forget_guarantee(),
400+
};
401+
if let Some(item) = item {
402+
err.span_label(
403+
item.span,
404+
format!(
405+
"expected a non-associated function, found {} {}",
406+
item.kind.article(),
407+
item.kind.descr()
408+
),
409+
);
410+
}
411+
err.span_label(attr_sp, "the `#[test]` macro causes a function to be run as a test and has no effect on non-functions")
412+
.span_suggestion(attr_sp,
413+
"replace with conditional compilation to make the item only exist when tests are being run",
414+
"#[cfg(test)]",
415+
Applicability::MaybeIncorrect)
416+
.emit();
417+
}
418+
419419
fn get_location_info(cx: &ExtCtxt<'_>, item: &ast::Item) -> (Symbol, usize, usize, usize, usize) {
420420
let span = item.ident.span;
421421
let (source_file, lo_line, lo_col, hi_line, hi_col) =

0 commit comments

Comments
 (0)