8

Add more info and suggestions to use of #[test] on invalid items by asquared3141...

 2 years ago
source link: https://github.com/rust-lang/rust/pull/92959
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Copy link

Contributor

asquared31415 commented on Jan 16

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

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK