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

"item TryFrom is imported redundantly" doesn't have automatic fix #121315

Open
fintelia opened this issue Feb 19, 2024 · 7 comments
Open

"item TryFrom is imported redundantly" doesn't have automatic fix #121315

fintelia opened this issue Feb 19, 2024 · 7 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@fintelia
Copy link
Contributor

fintelia commented Feb 19, 2024

Recently rustc starting warning on imports of TryFrom that are redundant with the prelude.

use std::convert::TryFrom;

fn main() {
    let _ = u32::try_from(5i32);
}

produces:

warning: the item `TryFrom` is imported redundantly
   --> src/main.rs:1:5
    |
1   | use std::convert::TryFrom;
    |     ^^^^^^^^^^^^^^^^^^^^^
    |
   ::: /home/jonathan/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/std/src/prelude/mod.rs:129:13
    |
129 |     pub use core::prelude::rust_2021::*;
    |             ------------------------ the item `TryFrom` is already defined here
    |
    = note: `#[warn(unused_imports)]` on by default

Sadly, there's no automatic fix suggested here. For a single case it isn't too bad to do manually, but in a larger project the output may look more like:

$ cargo check --all-targets --all-features --message-format=short
    Checking image v0.24.8 (/home/jonathan/git/image)
src/io/mod.rs:3:5: warning: the item `TryFrom` is imported redundantly
src/codecs/avif/decoder.rs:6:5: warning: the item `TryFrom` is imported redundantly
src/codecs/bmp/decoder.rs:2:5: warning: the item `TryFrom` is imported redundantly
src/codecs/bmp/decoder.rs:4:25: warning: the item `Iterator` is imported redundantly
src/codecs/dxt.rs:10:5: warning: the item `TryFrom` is imported redundantly
src/codecs/farbfeld.rs:19:20: warning: the item `TryFrom` is imported redundantly
src/codecs/farbfeld.rs:19:29: warning: the item `TryInto` is imported redundantly
src/codecs/gif.rs:29:5: warning: the item `TryFrom` is imported redundantly
src/codecs/gif.rs:30:5: warning: the item `TryInto` is imported redundantly
src/codecs/hdr/decoder.rs:5:5: warning: the item `TryFrom` is imported redundantly
src/codecs/hdr/decoder.rs:7:5: warning: the item `Iterator` is imported redundantly
src/codecs/ico/decoder.rs:2:5: warning: the item `TryFrom` is imported redundantly
src/codecs/jpeg/decoder.rs:1:5: warning: the item `TryFrom` is imported redundantly
src/codecs/jpeg/encoder.rs:4:5: warning: the item `TryFrom` is imported redundantly
src/codecs/pnm/decoder.rs:1:5: warning: the item `TryFrom` is imported redundantly
src/codecs/pnm/decoder.rs:2:5: warning: the item `TryInto` is imported redundantly
src/codecs/tga/decoder.rs:11:5: warning: the item `TryFrom` is imported redundantly
src/codecs/tga/encoder.rs:6:11: warning: the item `TryFrom` is imported redundantly
src/codecs/tiff.rs:11:5: warning: the item `TryFrom` is imported redundantly
src/codecs/webp/encoder.rs:8:5: warning: the item `FromIterator` is imported redundantly
src/codecs/webp/decoder.rs:2:5: warning: the item `TryFrom` is imported redundantly
src/codecs/webp/extended.rs:1:5: warning: the item `TryInto` is imported redundantly
src/codecs/webp/huffman.rs:1:5: warning: the item `TryInto` is imported redundantly
src/codecs/webp/lossless.rs:7:5: warning: the item `TryFrom` is imported redundantly
src/codecs/webp/lossless.rs:8:5: warning: the item `TryInto` is imported redundantly
src/codecs/webp/lossless_transform.rs:1:5: warning: the item `TryFrom` is imported redundantly
src/codecs/webp/lossless_transform.rs:2:5: warning: the item `TryInto` is imported redundantly
src/codecs/webp/vp8.rs:16:5: warning: the item `TryInto` is imported redundantly
src/animation.rs:2:5: warning: the item `Iterator` is imported redundantly
src/image.rs:2:5: warning: the item `TryFrom` is imported redundantly
src/imageops/colorops.rs:548:28: warning: the item `ImageBuffer` is imported redundantly
src/codecs/hdr/decoder.rs:1024:9: warning: the item `Cursor` is imported redundantly
src/codecs/png.rs:792:9: warning: the item `ImageDecoder` is imported redundantly
src/codecs/png.rs:795:27: warning: the item `Read` is imported redundantly
warning: `image` (lib) generated 30 warnings
warning: `image` (lib test) generated 34 warnings (30 duplicates)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.67s
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 19, 2024
@jieyouxu jieyouxu added A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Feb 20, 2024
@fmease fmease added the D-papercut Diagnostics: An error or lint that needs small tweaks. label Feb 20, 2024
@heaths
Copy link

heaths commented Feb 20, 2024

I'm wondering if this condition is even worth the lint. We recently hit this on a very large code base and fixing them all - especially without automatic fix-ups - is counterproductive to any benefit I can imagine. Most were from adding some types to our various crates' preludes and all the source that imported both wasn't cleaned up. Didn't need to until now.

I'd love to just #[allow(...)] these, but they are part of unused_import and I don't want to blanket allow those. If there's still time, can duplicate imports be their own lint? If there ambiguity that actually matters - i.e., duplicate symbol names are actually different types - that would result in a meaningful compiler error already. This seems like just "busy work".

@RossSmyth
Copy link
Contributor

Yeah this breaks trying to test some software with #![deny(warnings)] with Miri. Probably something with build-std.

@kpreid
Copy link
Contributor

kpreid commented Feb 27, 2024

I agree with the original report that it would be really valuable to have a fix for this — it turns a potentially large annoyance into a quick fix.

@RossSmyth deny(warnings) is in general fragile in that way; it means that your build can be broken by any compiler update.

@chenyukang chenyukang self-assigned this Mar 6, 2024
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 11, 2024
…-removing, r=<try>

Merge RedundantImport into UnusedImport for suggesting removing spans

Fixes rust-lang#121315

I also changed the label of diagnostics for RedundantImport to be compatible with UnusedImport, or any other better ways?

r? `@petrochenkov`
@mmagician
Copy link

@chenyukang thanks for taking on the issue, very much a needed feature. Is this still being worked on?

@RossSmyth
Copy link
Contributor

I agree with the original report that it would be really valuable to have a fix for this — it turns a potentially large annoyance into a quick fix.

@RossSmyth deny(warnings) is in general fragile in that way; it means that your build can be broken by any compiler update.

Yeah, I don't have it enabled but wrenching on some other crates they do.

@RossSmyth
Copy link
Contributor

Another failure spotted in the wild:

Because of this line:
https://github.com/serialport/serialport-rs/blob/84f6066ffd92056829b91771afc7eebcbc6642e2/src/lib.rs#L35

And this time not even deny(warnings), but
https://github.com/serialport/serialport-rs/blob/84f6066ffd92056829b91771afc7eebcbc6642e2/src/lib.rs#L23
deny(unused)

Means that serialport-rs doesn't compile on nightly right now.

@chenyukang
Copy link
Member

@chenyukang thanks for taking on the issue, very much a needed feature. Is this still being worked on?

sorry, there are some delays, I will continue to work on it.

@chenyukang chenyukang removed their assignment Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-resolve Area: Name resolution A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants