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

Many "unused imports" warning occurs #343

Closed
ghost opened this issue Oct 11, 2021 · 15 comments · Fixed by #346
Closed

Many "unused imports" warning occurs #343

ghost opened this issue Oct 11, 2021 · 15 comments · Fixed by #346

Comments

@ghost
Copy link

ghost commented Oct 11, 2021

thank you for this great library.

v0.10.2

Like example below,

#[mockall::automock]
pub mod forms {
    use std::convert::TryInto;

    pub async fn hoge() -> Hoge {
        let row = sth...
        Ok(row.try_into()?)
    }
}

In this case, TryInto surely is used but warns unused imports. Not only this, but also like below.

#[mockall::automock]
pub mod forms {
    use crate::{a, b}; // <- b occurs unused imports
    use crate::hoge::somepubfunction; // <- occurs
}

I think this related to https://github.com/asomers/mockall/pull/54/files, but I can't find where to fix.

@asomers
Copy link
Owner

asomers commented Oct 11, 2021

Please tell me what version of rustc you are using, and the exact warning you see.

@ghost
Copy link
Author

ghost commented Oct 11, 2021

❯ rustc --version
rustc 1.52.1 (9bc8c42bb 2021-05-09)

every warning is same

warning: unused import: `hoge`
 --> crates/~~~
  |
7 |     use crate::~~~hoge

@asomers
Copy link
Owner

asomers commented Oct 11, 2021

I don't understand. What code corresponds to that warning? I was expecting to see something about TryInto.

@ghost
Copy link
Author

ghost commented Oct 11, 2021

Ok, I can't upload all codes here but it's like this.

#[mockall::automock]
pub mod Sth {
    use crate::somerecord;
    use crate::someerror;
	use crate::somepool;
    use std::convert::TryInto; // <- this causes unused imports warning.
    use uuid::Uuid;

    pub async fn find_by_id(id: Uuid) -> Result<somerecord, someerror> {
        let row = sqlx::query("SELECT * FROM sth WHERE id = $1::uuid")
            .bind(id)
            .fetch_one(somepool.get().unwrap())
            .await
            .map_err::<someerror, _>(Into::into)?;

        Ok(row.try_into()?)
    }
}

@asomers
Copy link
Owner

asomers commented Oct 11, 2021

And that code produces what warning?

@ghost
Copy link
Author

ghost commented Oct 11, 2021

This one.

warning: unused import: TryInto
--> crates/~~~
|
7 | use std::convert::TryInto;

@asomers
Copy link
Owner

asomers commented Oct 11, 2021

Should be easy to suppress. The generated mock module needs those imports, in case they're used by the mock functions' signatures. But in this case, they're only used by the bodies, so the import isn't used. Suppressing the warning in mockall is the correct solution.

@ghost
Copy link
Author

ghost commented Oct 11, 2021

Ah yes that's what I'm doing currently with allow unused_imports.

Thank you for replying. It will be great if that causes no warning without suppressing.

@ghost ghost closed this as completed Oct 11, 2021
@asomers
Copy link
Owner

asomers commented Oct 11, 2021

Reopening, because Mockall should suppress this warning itself.

@asomers asomers reopened this Oct 11, 2021
asomers added a commit that referenced this issue Oct 17, 2021
A user might import a type or trait into an automocked module.  Mockall
must emit those same imports in the generated code, because the imported
type might be used by the signature of a mocked method.  But if it's
only used by a body, then it will trigger an unused_import warning.  So
Mockall must suppress those.

Fixes #343
@asomers
Copy link
Owner

asomers commented Oct 17, 2021

@ulwlu could you please test your crate against the branch in the linked PR?

@ghost
Copy link
Author

ghost commented Oct 18, 2021

Sure, now I am working but will try in a day.

@ghost
Copy link
Author

ghost commented Oct 19, 2021

@asomers

I tried with the cargo.toml below, and all errors are gone! thx!!!

mockall = { git = "https://github.com/asomers/mockall", branch = "unused-warnings"}

@ghost
Copy link
Author

ghost commented Dec 8, 2021

@asomers

Hi do you plan to release this one in next version...?
I'm fine with this way of specifiying git url in Cargo.toml, but hopefully you can release next patch version soon.....

@asomers
Copy link
Owner

asomers commented Dec 11, 2021

@ulwlu done!

@ghost
Copy link
Author

ghost commented Dec 11, 2021

Thank you very much!! And thank you for this great mocking library again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant