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

Remove unused_extern_crates warning for crates used by nested code #44294

Closed
Tracked by #54910
dtolnay opened this issue Sep 3, 2017 · 4 comments
Closed
Tracked by #54910

Remove unused_extern_crates warning for crates used by nested code #44294

dtolnay opened this issue Sep 3, 2017 · 4 comments
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@dtolnay
Copy link
Member

dtolnay commented Sep 3, 2017

In the following scenario, the user will have a better experience if we allow them to have an "unused" extern crate.


[playground]

//extern crate serde;

struct NotSerializable;

struct TryingToSerialize {
    bad: NotSerializable,
}

// Code generated by a macro like #[derive(Serialize)].
const IMPL_SERIALIZE_FOR_TryingToSerialize: () = {
    extern crate serde;

    impl serde::Serialize for TryingToSerialize {
        fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where S: serde::Serializer
        {
            serde::Serialize::serialize(&self.bad, serializer)
        }
    }
};

If we permit them to write extern crate, as recommended by Serde's docs for this reason, the error message is:

8 | #[derive(Serialize)]
  |          ^^^^^^^^^ the trait `serde::Serialize` is not implemented
                       for `NotSerializable`

If they remove the unused extern crate, they generally get nastier error messages.

8 | #[derive(Serialize)]
  |          ^^^^^^^^^ the trait `_IMPL_SERIALIZE_FOR_TryingToSerialize::_serde::Serialize`
                       is not implemented for `NotSerializable`

If we can't fix the error message to be as good as it was before the unused_extern_crates warning, let's disable unused_extern_crates in cases where the same crate is used later in some nested scope (submodule or code block).


cc @ishitatsuyuki who has been involved with this warning.

@Mark-Simulacrum Mark-Simulacrum added A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Sep 3, 2017
@ishitatsuyuki
Copy link
Contributor

ishitatsuyuki commented Sep 4, 2017

Arguable point. As the statement doesn't impact the binary (linkage), it doesn't harm if we disabled the lint. I'm not sure about the way to implement it though.

dtolnay added a commit to dtolnay/rust that referenced this issue Sep 25, 2017
This is a partial revert of rust-lang#42588. There is a usability concern
reported in rust-lang#44294 that was not considered in the discussion of the PR,
so I would like to back this out of 1.21. As is, I think users would
have a worse and more confusing experience with this lint enabled by
default. We can re-enabled once there are better diagnostics or the case
in rust-lang#44294 does not trigger the lint.
bors added a commit that referenced this issue Sep 27, 2017
Allow unused extern crate again

This is a partial revert of #42588. There is a usability concern reported in #44294 that was not considered in the discussion of the PR, so I would like to back this out of 1.21. As is, I think users would have a worse and more confusing experience with this lint enabled by default. We can re-enabled once there are better diagnostics or the case in #44294 does not trigger the lint.
nikomatsakis pushed a commit to nikomatsakis/rust that referenced this issue Sep 28, 2017
This is a partial revert of rust-lang#42588. There is a usability concern
reported in rust-lang#44294 that was not considered in the discussion of the PR,
so I would like to back this out of 1.21. As is, I think users would
have a worse and more confusing experience with this lint enabled by
default. We can re-enabled once there are better diagnostics or the case
in rust-lang#44294 does not trigger the lint.
@Dylan-DPC-zz
Copy link

@dtolnay the above example works now without the requirement of extern crate. Is there anything else blocking this other than #46991?

@Dylan-DPC
Copy link
Member

Closing this as the error case is the same now irrespective of whether extern crate is included or not.

@dtolnay
Copy link
Member Author

dtolnay commented Aug 26, 2023

There is still a minor improvement from adding extern crate serde.

//extern crate serde;

struct NotSerializable;

#[derive(serde::Serialize)]
struct TryingToSerialize {
    bad: NotSerializable,
}
< note: required by a bound in `_serde::ser::SerializeStruct::serialize_field`
> note: required by a bound in `serde::ser::SerializeStruct::serialize_field`

Less bad compared to _IMPL_SERIALIZE_FOR_TryingToSerialize though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

5 participants