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

Warn about explicit import of TryInto, TryFrom, and FromIterator when migrating to 2021 edition. #117448

Closed
cyqsimon opened this issue Oct 31, 2023 · 3 comments · Fixed by #117772
Assignees
Labels
A-edition-2021 Area: The 2021 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@cyqsimon
Copy link
Contributor

cyqsimon commented Oct 31, 2023

Problem

One of the changes made in 2021 edition is that TryInto, TryFrom, and FromIterator are now in scope by default (link). So an explicit use statement is now redundant.

However, it seems like neither cargo fix --edition nor cargo fix --edition-idioms (and not even clippy!) catches this. I'm not sure which one of the these should be responsible, but it seems like one of them should at least emit a warning for this.

Steps

  1. Checkout a Rust project on 2018 edition that uses TryInto, TryFrom, and FromIterator. An example would be bat before 4b33093.
  2. Perform the normal edition upgrade procedure.
  3. Notice that the explicit use statements are not caught (no warnings).

Possible Solution(s)

Make cargo fix --edition or cargo fix --edition-idioms catch this and warn about it. Or maybe have clippy do this. Which one is more appropriate I'm not sure.

Notes

No response

Version

cargo 1.73.0 (9c4383fb5 2023-08-26)
release: 1.73.0
commit-hash: 9c4383fb55986096b414d98125421ab87b5fd642
commit-date: 2023-08-26
host: x86_64-unknown-linux-gnu
libgit2: 1.6.4 (sys:0.17.2 vendored)
libcurl: 8.2.1-DEV (sys:0.4.65+curl-8.2.1 vendored ssl:OpenSSL/1.1.1u)
ssl: OpenSSL 1.1.1u  30 May 2023
os: EndeavourOS Rolling Release (rolling) [64-bit]
@cyqsimon cyqsimon added the C-bug Category: This is a bug. label Oct 31, 2023
@epage epage transferred this issue from rust-lang/cargo Oct 31, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 31, 2023
@Jules-Bertholet
Copy link
Contributor

Jules-Bertholet commented Oct 31, 2023

@rustbot label A-edition-2021 A-lint A-suggestion-diagnostics C-enhancement -C-bug

@rustbot rustbot added A-edition-2021 Area: The 2021 edition C-enhancement Category: An issue proposing an enhancement or a PR with one. A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. and removed C-bug Category: This is a bug. labels Oct 31, 2023
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 31, 2023
@Nemo157
Copy link
Member

Nemo157 commented Nov 1, 2023

Seems like this doesn't need to be edition 2021 or these traits specific, there could just be a warning for shadowing a prelude import with the exact same item (when that shadowing is not itself shadowing a glob import of a different item).

@surechen
Copy link
Contributor

surechen commented Nov 2, 2023

@rustbot claim

bors added a commit to rust-lang-ci/rust that referenced this issue Dec 20, 2023
Enhance lint unused_imports to check unnecessary imports in std::prelude

fixes rust-lang#117448

detects unnecessary imports in std::prelude that can be eliminated.

For example import:

```rust
use std::{option::{Iter, IterMut, IntoIter, Option::{self, Some}}, convert::{TryFrom, TryInto}, mem::drop};
```

detect : `Option::{self, Some}`   `mem::drop`
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 22, 2023
Enhance lint unused_imports to check unnecessary imports in std::prelude

fixes rust-lang#117448

detects unnecessary imports in std::prelude that can be eliminated.

For example import:

```rust
use std::{option::{Iter, IterMut, IntoIter, Option::{self, Some}}, convert::{TryFrom, TryInto}, mem::drop};
```

detect : `Option::{self, Some}`   `mem::drop`
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 23, 2023
Enhance lint unused_imports to check unnecessary imports in std::prelude

fixes rust-lang#117448

detects unnecessary imports in std::prelude that can be eliminated.

For example import:

```rust
use std::{option::{Iter, IterMut, IntoIter, Option::{self, Some}}, convert::{TryFrom, TryInto}, mem::drop};
```

detect : `Option::{self, Some}`   `mem::drop`
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 23, 2023
Enhance lint unused_imports to check unnecessary imports in std::prelude

fixes rust-lang#117448

detects unnecessary imports in std::prelude that can be eliminated.

For example import:

```rust
use std::{option::{Iter, IterMut, IntoIter, Option::{self, Some}}, convert::{TryFrom, TryInto}, mem::drop};
```

detect : `Option::{self, Some}`   `mem::drop`
bors added a commit to rust-lang-ci/rust that referenced this issue Dec 23, 2023
Enhance lint unused_imports to check unnecessary imports in std::prelude

fixes rust-lang#117448

detects unnecessary imports in std::prelude that can be eliminated.

For example import:

```rust
use std::{option::{Iter, IterMut, IntoIter, Option::{self, Some}}, convert::{TryFrom, TryInto}, mem::drop};
```

detect : `Option::{self, Some}`   `mem::drop`
surechen added a commit to surechen/rust that referenced this issue Feb 12, 2024
… other situations like module-relative uses, we can do more accurate redundant import checking.

fixes rust-lang#117448

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
surechen added a commit to surechen/rust that referenced this issue Feb 12, 2024
… other situations like module-relative uses, we can do more accurate redundant import checking.

fixes rust-lang#117448

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
surechen added a commit to surechen/rust that referenced this issue Feb 13, 2024
… other situations like module-relative uses, we can do more accurate redundant import checking.

fixes rust-lang#117448

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 13, 2024
Tracking import use types for more accurate redundant import checking

fixes rust-lang#117448

By tracking import use types to check whether it is scope uses or the other situations like module-relative uses,  we can do more accurate redundant import checking.

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
bors added a commit to rust-lang-ci/rust that referenced this issue Feb 18, 2024
Tracking import use types for more accurate redundant import checking

fixes rust-lang#117448

By tracking import use types to check whether it is scope uses or the other situations like module-relative uses,  we can do more accurate redundant import checking.

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
@bors bors closed this as completed in a61126c Feb 18, 2024
flip1995 pushed a commit to flip1995/rust that referenced this issue Feb 26, 2024
… other situations like module-relative uses, we can do more accurate redundant import checking.

fixes rust-lang#117448

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
bors pushed a commit to rust-lang-ci/rust that referenced this issue Mar 17, 2024
… other situations like module-relative uses, we can do more accurate redundant import checking.

fixes rust-lang#117448

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 17, 2024
Tracking import use types for more accurate redundant import checking

fixes rust-lang#117448

By tracking import use types to check whether it is scope uses or the other situations like module-relative uses,  we can do more accurate redundant import checking.

For example unnecessary imports in std::prelude that can be eliminated:

```rust
use std::option::Option::Some;//~ WARNING the item `Some` is imported redundantly
use std::option::Option::None; //~ WARNING the item `None` is imported redundantly
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2021 Area: The 2021 edition A-lint Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants