Skip to content

std_instead_of_core creates an invalid fix on stacked imports #12468

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

Open
MarcusGrass opened this issue Mar 12, 2024 · 0 comments
Open

std_instead_of_core creates an invalid fix on stacked imports #12468

MarcusGrass opened this issue Mar 12, 2024 · 0 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@MarcusGrass
Copy link
Contributor

Summary

Found while fixing this one #12406.

It has existed probably since this lint was created, but became problematic once the clippy-fix was added.

The problem is that check_path will get each path separately, i.e. for:

use std::{fmt::Result, io::Write};

There will be two passes, and to the code, the paths will look like:

use std::fmt::Result;
// And separately
use std::io::Write;

But, the code will deduce that they are from the same import because the std-span is the same on those two passes, and it keeps the last span saved.

// Not problematic, will skip everything after the first io::Write which can't be taken from core
use std::{io::Write, fmt::Result};
// Is problematic, since it will recognize that fmt::Result can be taken from core
use std::{fmt::Result, io::Write};

There's some nuance here, in the case that the path is a part of a stacked import, each path in that import needs to be known to ensure that the fix will be valid, and at least with check_path that's hard to know without looking ahead.

Reproducer

I tried this code:

use std::{fmt::Result, io::Write};

I expected to see this happen:
Warn that std::fmt::Result can be imported from core and either:

  1. Don't fix, since core::io::Write does not exist.
  2. Fix by splitting up the import.

Instead, this happened:
std is replaced by core which doesn't compile.

Version

rustc 1.78.0-nightly (7d3702e47 2024-03-06)
binary: rustc
commit-hash: 7d3702e472b99be0f5de6608dd87af1df8f99428
commit-date: 2024-03-06
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0

Additional Labels

No response

@MarcusGrass MarcusGrass added the C-bug Category: Clippy is not doing the correct thing label Mar 12, 2024
@y21 y21 added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants