Skip to content

Remove unused_extern_crates warning for crates used by nested code #44294

Closed
Listed in
@dtolnay

Description

@dtolnay
Member

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.

Activity

added
A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.
C-enhancementCategory: An issue proposing an enhancement or a PR with one.
on Sep 3, 2017
ishitatsuyuki

ishitatsuyuki commented on Sep 4, 2017

@ishitatsuyuki
Contributor

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.

added a commit that references this issue on Sep 25, 2017
added a commit that references this issue on Sep 27, 2017

Auto merge of #44825 - dtolnay:cratelint, r=aturon

added a commit that references this issue on Sep 28, 2017
Dylan-DPC-zz

Dylan-DPC-zz commented on Dec 25, 2019

@Dylan-DPC-zz

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

Dylan-DPC

Dylan-DPC commented on Aug 26, 2023

@Dylan-DPC
Member

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

dtolnay

dtolnay commented on Aug 26, 2023

@dtolnay
MemberAuthor

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.C-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dtolnay@Mark-Simulacrum@Dylan-DPC-zz@ishitatsuyuki@Dylan-DPC

        Issue actions

          Remove unused_extern_crates warning for crates used by nested code · Issue #44294 · rust-lang/rust