rust-lang/rustfix

Test-only modules don't get fixed

jrvidal opened this issue · 5 comments

rustfix does not seem to descend into #[cfg(test)] modules.

Test case

$ rustup --version
rustup 1.11.0 (e751ff9f8 2018-02-13)
$ cat rust-toolchain 
nightly-2018-07-18
$ cargo fix --version
cargo-fix 0.4.1

lib.rs

#![feature(rust_2018_preview)]
use helpers::*; // This should be fixed

#[cfg(test)]
mod tests;
mod helpers;

pub fn exported_foo() -> usize {
  foo()
}

helpers.rs

pub fn foo() -> usize { 1 }

tests.rs

use helpers::*; // This does not get fixed and it should

#[test]
fn it_works() {
  assert_eq!(1, foo());
}

Output of cargo fix

diff --git a/src/lib.rs b/src/lib.rs
index 54c2d4a..eaa3bdf 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,5 +1,5 @@
 #![feature(rust_2018_preview)]
-use helpers::*;
+use crate::helpers::*;
 
 #[cfg(test)]
 mod tests;

Expected result

That use helpers::* in tests.rs would get rewritten to use crate::helpers::* as well.

Thanks for the great report! Can you try if it works when using --all-targets?

Relatedly, rust-lang/cargo#5739 :)

Hmm, it doesn't seem to be available:

$ cargo fix --all-targets --prepare-for 2018
error: Found argument '--all-targets' which wasn't expected, or isn't valid in this context

USAGE:
    cargo fix [FLAGS] [OPTIONS] [args]...

For more information try --help
$ cargo fix --prepare-for 2018 --all-targets
error: Found argument '--all-targets' which wasn't expected, or isn't valid in this context

USAGE:
    cargo fix --prepare-for <edition>

For more information try --help

@jrvidal oh for now temporarily you'll need to do cargo fix --prepare-for 2018 -- --all-targets (note the extra --)

That does the trick. Please feel free to close this, or to leave it open until --all-targets is added by default (or whatever ends up being implemented).

Cool! We already have rust-lang/cargo#5739 to track making it the default so I'll close this one.

Thanks for reporting this!