Skip to content
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

Allow use super::*; in test modules #5554

Closed
MrAwesome opened this issue May 1, 2020 · 0 comments · Fixed by #5564
Closed

Allow use super::*; in test modules #5554

MrAwesome opened this issue May 1, 2020 · 0 comments · Fixed by #5564
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy

Comments

@MrAwesome
Copy link
Contributor

Current behavior:

Using use super::*; in a test module throws a warning in pedantic mode:

warning: usage of wildcard import
   --> src/wifi_scan/mod.rs:104:9
    |
104 |     use super::*;
    |         ^^^^^^^^ help: try: `super::{ALLOWED_SYNCHRONOUS_RETRIES, DEVICE_OR_RESOURCE_BUSY_EXIT_CODE, SYNCHRONOUS_RETRY_DELAY_SECS, WifiIPInterface}`

Expected behavior:

No warning when use super::*; is used in a test module.

Reasoning:

Using a wildcard import in test modules to bring in everything from the (parent) module being tested seems to be a very common idiom in Rust. It's recommended in The Book, it's more common in the standard library than explicitly-listed imports, with the same bring true in a sample of commonly-used crates.

Warning on this seems counter-productive - forcing explicit imports introduces a lot of additional friction to writing tests. For the first attempt at writing tests you could use a wildcard import and then quickfix it to explicitly import, but any future tests written in that module will still require hopping up to the use statement and manually plugging in everything needed from the parent module. That's not the end of the world of course, but I'm always wary of anything that introduces unnecessary friction to the act of writing tests.

I think it makes a lot of sense to allow use super::*; in tests, in the same way prelude::* imports are allowed. Something along the lines of if current_module is [cfg(test)] and current_module_name matches "test[s]" and import is "super::*", do not warn.

I'm happy to put up a pull request to implement this, if it sounds like a reasonable idea. I can reason from the above code how to match on "super", but I'd need some guidance on how to find the name of the current module and/or how to check for #[cfg(test)].

@flip1995 flip1995 added good-first-issue These issues are a good way to get started with Clippy C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages labels May 1, 2020
@bors bors closed this as completed in 43a1777 May 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: Enhancement of lints, like adding more cases or adding help messages good-first-issue These issues are a good way to get started with Clippy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants