Skip to content

Conversation

asquared31415
Copy link
Contributor

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:

#[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)]
   | ~~~~~~~~~~~~

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 16, 2022
@rust-highfive
Copy link
Contributor

r? @estebank

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 16, 2022
)
.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)
Copy link
Contributor

@PatchMixolydic PatchMixolydic Jan 16, 2022

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.

Copy link
Contributor Author

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.

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Feb 17, 2022

📌 Commit 6d05e2a has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 17, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Feb 17, 2022
…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)]
     | ~~~~~~~~~~~~
   ```
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 18, 2022
…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
@bors bors merged commit 659382f into rust-lang:master Feb 18, 2022
@rustbot rustbot added this to the 1.60.0 milestone Feb 18, 2022
estebank added a commit to estebank/rust that referenced this pull request Mar 4, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Mar 5, 2022
Downgrade `#[test]` on macro call to warning

Follow up to rust-lang#92959. Address rust-lang#94508.
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this pull request Mar 5, 2022
Downgrade `#[test]` on macro call to warning

Follow up to rust-lang#92959. Address rust-lang#94508.
Mark-Simulacrum pushed a commit to Mark-Simulacrum/rust that referenced this pull request Mar 14, 2022
@asquared31415 asquared31415 deleted the test-non-fn-help branch September 3, 2022 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants