-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Add more info and suggestions to use of #[test] on invalid items #92959
Conversation
r? @estebank (rust-highfive has picked a reviewer for you, use r? to override) |
) | ||
.note("the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions") | ||
.span_label(item.span, format!("expected a non-associated function, found {} {}", item.kind.article(), item.kind.descr())) | ||
.span_suggestion(attr_sp, "replace with conditional compilation to make the item only exist when tests are being run", String::from("#[cfg(test)]"), Applicability::MachineApplicable) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using #[cfg(test)]
might not be what the user meant (for instance, they might have tried to write a test as an associated function), so this suggestion should probably be MaybeIncorrect
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#[test]
on an associated function does not trigger this error, it's caught elsewhere. However it still may be an unwanted suggestion.
f3333eb
to
6d05e2a
Compare
@bors r+ |
📌 Commit 6d05e2a has been approved by |
…stebank Add more info and suggestions to use of #[test] on invalid items This pr changes the diagnostics for using `#[test]` on an item that can't be used as a test to explain that the attribute has no meaningful effect on non-functions and suggests the use of `#[cfg(test)]` for conditional compilation instead. Example change: ```rs #[test] mod test {} ``` previously output ``` error: only functions may be used as tests --> src/lib.rs:2:1 | 2 | mod test {} | ^^^^^^^^^^^ ``` now outputs ``` error: the `#[test]` attribute may only be used on a non-associated function --> $DIR/test-on-not-fn.rs:3:1 | LL | #[test] | ^^^^^^^ LL | mod test {} | ----------- expected a non-associated function, found a module | = note: the `#[test]` macro causes a a function to be run on a test and has no effect on non-functions help: replace with conditional compilation to make the item only exist when tests are being run | LL | #[cfg(test)] | ~~~~~~~~~~~~ ```
…askrgr Rollup of 6 pull requests Successful merges: - rust-lang#92683 (Suggest copying trait associated type bounds on lifetime error) - rust-lang#92933 (Deny mixing bin crate type with lib crate types) - rust-lang#92959 (Add more info and suggestions to use of #[test] on invalid items) - rust-lang#93024 (Do not ICE when inlining a function with un-satisfiable bounds) - rust-lang#93613 (Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator`) - rust-lang#93634 (compiler: clippy::complexity fixes) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Follow up to rust-lang#92959. Address rust-lang#94508.
Downgrade `#[test]` on macro call to warning Follow up to rust-lang#92959. Address rust-lang#94508.
Downgrade `#[test]` on macro call to warning Follow up to rust-lang#92959. Address rust-lang#94508.
Follow up to rust-lang#92959. Address rust-lang#94508.
This pr changes the diagnostics for using
#[test]
on an item that can't be used as a test to explain that the attribute has no meaningful effect on non-functions and suggests the use of#[cfg(test)]
for conditional compilation instead.Example change:
previously output
now outputs