fix [std_instead_of_core]: false positive for multi-imports#17252
fix [std_instead_of_core]: false positive for multi-imports#17252bushrat011899 wants to merge 1 commit into
Conversation
|
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @llogiq (or someone else) some time within the next two weeks. Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (
Why was this reviewer chosen?The reviewer was selected based on:
|
|
Lintcheck changes for 6adb667
This comment will be updated if you push new changes |
db1918a to
5dc77a7
Compare
|
Reminder, once the PR becomes ready for a review, use |
|
r? Jarcho |
3885b82 to
2eda7d5
Compare
This comment has been minimized.
This comment has been minimized.
2eda7d5 to
98e1658
Compare
This comment has been minimized.
This comment has been minimized.
…li-obk Fix(ICE): Remove `unwrap` from `get_module_children` # Objective Fix an ICE I ran into while developing rust-lang/rust-clippy#17252 that stems from `rustc_metadata::rmeta::decoder::CrateMetadata::get_module_children` calling `unwrap`. The local value `non_reexports` can be `None` when the passed in `DefIndex` is for a non-module-like item. The documentation for `get_module_children` *implies* that it must only be called with module-like item indices, however it seems to me that it would be appropriate to just return nothing when called on a non-module-like item (if it isn't module-like, it has zero children). ## Solution - Switched from an `Option::unwrap` to an `if let Some(...)` --- ## Notes * No AI tooling of any kind was used during the creation of this PR. * Please see [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/.E2.9C.94.20.60rustc.60.20ICE.20from.20.60TyCtxt.3A.3Amodule_children.60/with/603700184) for some discussion. * Please see this [failed Clippy CI run](https://github.com/rust-lang/rust-clippy/actions/runs/27596428127/job/81587609898#step:5:1) for an example of the ICE caused.
98e1658 to
130da4a
Compare
130da4a to
28d43c3
Compare
…etrochenkov,oli-obk Replace `unwrap` with `expect` in `get_module_children` # Objective I ran into an ICE I ran into while developing rust-lang/rust-clippy#17252 that stems from `rustc_metadata::rmeta::decoder::CrateMetadata::get_module_children` calling `unwrap`. The local value `non_reexports` can be `None` when the passed in `DefIndex` is for a non-module-like item. The documentation for `get_module_children` *implies* that it must only be called with module-like item indices, however the use of `unwrap` makes diagnosing the ICE trickier than it has to be. Additionally, this behaviour is not documented on `TyCtxt::module_children`. ## Solution - Switched from an `Option::unwrap` to a `Option::expect` - Added panic documentation to `get_module_children` - Replicated documentation for `TyCtxt::module_children` --- ## Notes * No AI tooling of any kind was used during the creation of this PR. * Please see [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/.E2.9C.94.20.60rustc.60.20ICE.20from.20.60TyCtxt.3A.3Amodule_children.60/with/603700184) for some discussion. * Please see this [failed Clippy CI run](https://github.com/rust-lang/rust-clippy/actions/runs/27596428127/job/81587609898#step:5:1) for an example of the ICE caused.
…unwrap, r=petrochenkov,oli-obk Replace `unwrap` with `expect` in `get_module_children` # Objective I ran into an ICE I ran into while developing rust-lang/rust-clippy#17252 that stems from `rustc_metadata::rmeta::decoder::CrateMetadata::get_module_children` calling `unwrap`. The local value `non_reexports` can be `None` when the passed in `DefIndex` is for a non-module-like item. The documentation for `get_module_children` *implies* that it must only be called with module-like item indices, however the use of `unwrap` makes diagnosing the ICE trickier than it has to be. Additionally, this behaviour is not documented on `TyCtxt::module_children`. ## Solution - Switched from an `Option::unwrap` to a `Option::expect` - Added panic documentation to `get_module_children` - Replicated documentation for `TyCtxt::module_children` --- ## Notes * No AI tooling of any kind was used during the creation of this PR. * Please see [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/.E2.9C.94.20.60rustc.60.20ICE.20from.20.60TyCtxt.3A.3Amodule_children.60/with/603700184) for some discussion. * Please see this [failed Clippy CI run](https://github.com/rust-lang/rust-clippy/actions/runs/27596428127/job/81587609898#step:5:1) for an example of the ICE caused.
…unwrap, r=petrochenkov,oli-obk Replace `unwrap` with `expect` in `get_module_children` # Objective I ran into an ICE I ran into while developing rust-lang/rust-clippy#17252 that stems from `rustc_metadata::rmeta::decoder::CrateMetadata::get_module_children` calling `unwrap`. The local value `non_reexports` can be `None` when the passed in `DefIndex` is for a non-module-like item. The documentation for `get_module_children` *implies* that it must only be called with module-like item indices, however the use of `unwrap` makes diagnosing the ICE trickier than it has to be. Additionally, this behaviour is not documented on `TyCtxt::module_children`. ## Solution - Switched from an `Option::unwrap` to a `Option::expect` - Added panic documentation to `get_module_children` - Replicated documentation for `TyCtxt::module_children` --- ## Notes * No AI tooling of any kind was used during the creation of this PR. * Please see [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/.E2.9C.94.20.60rustc.60.20ICE.20from.20.60TyCtxt.3A.3Amodule_children.60/with/603700184) for some discussion. * Please see this [failed Clippy CI run](https://github.com/rust-lang/rust-clippy/actions/runs/27596428127/job/81587609898#step:5:1) for an example of the ICE caused.
…unwrap, r=petrochenkov,oli-obk Replace `unwrap` with `expect` in `get_module_children` # Objective I ran into an ICE I ran into while developing rust-lang/rust-clippy#17252 that stems from `rustc_metadata::rmeta::decoder::CrateMetadata::get_module_children` calling `unwrap`. The local value `non_reexports` can be `None` when the passed in `DefIndex` is for a non-module-like item. The documentation for `get_module_children` *implies* that it must only be called with module-like item indices, however the use of `unwrap` makes diagnosing the ICE trickier than it has to be. Additionally, this behaviour is not documented on `TyCtxt::module_children`. ## Solution - Switched from an `Option::unwrap` to a `Option::expect` - Added panic documentation to `get_module_children` - Replicated documentation for `TyCtxt::module_children` --- ## Notes * No AI tooling of any kind was used during the creation of this PR. * Please see [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/.E2.9C.94.20.60rustc.60.20ICE.20from.20.60TyCtxt.3A.3Amodule_children.60/with/603700184) for some discussion. * Please see this [failed Clippy CI run](https://github.com/rust-lang/rust-clippy/actions/runs/27596428127/job/81587609898#step:5:1) for an example of the ICE caused.
Rollup merge of #158002 - bushrat011899:get_module_children_unwrap, r=petrochenkov,oli-obk Replace `unwrap` with `expect` in `get_module_children` # Objective I ran into an ICE I ran into while developing rust-lang/rust-clippy#17252 that stems from `rustc_metadata::rmeta::decoder::CrateMetadata::get_module_children` calling `unwrap`. The local value `non_reexports` can be `None` when the passed in `DefIndex` is for a non-module-like item. The documentation for `get_module_children` *implies* that it must only be called with module-like item indices, however the use of `unwrap` makes diagnosing the ICE trickier than it has to be. Additionally, this behaviour is not documented on `TyCtxt::module_children`. ## Solution - Switched from an `Option::unwrap` to a `Option::expect` - Added panic documentation to `get_module_children` - Replicated documentation for `TyCtxt::module_children` --- ## Notes * No AI tooling of any kind was used during the creation of this PR. * Please see [Zulip](https://rust-lang.zulipchat.com/#narrow/channel/122651-general/topic/.E2.9C.94.20.60rustc.60.20ICE.20from.20.60TyCtxt.3A.3Amodule_children.60/with/603700184) for some discussion. * Please see this [failed Clippy CI run](https://github.com/rust-lang/rust-clippy/actions/runs/27596428127/job/81587609898#step:5:1) for an example of the ICE caused.
28d43c3 to
5e134b4
Compare
|
This lint really needs to be re-written to work on You;ll want a setup like: fn check_item(&mut self, cx: &LateContext<'tcx>, i: &'tcx Item<'_>) {
if let ItemKind::Use(p, kind) = i.kind {
if p.segments.len() > 2 {
self.skip_paths += u8::from(p.res.value_ns.is_some()) + u8::from(p.res.type_ns.is_some()) + u8::from(p.res.mac_ns.is_some());
}
// Continue linting
}
}
fn check_path(&mut self, cx: &LateContext<'tcx>, p: &'tcx Path<'_>) {
if let [first, mid @ .., last] = path.segments {
if let Some(count) = self.skip_paths.checked_sub(1) {
self.skip_paths = count;
return;
}
// Continue linting
}
}Note that you want to do the minimum amount of work possible for single segment paths since they are by far the most common and trivially not lintable. If you want a fun case you can do something like: use {
some_crate::foo,
::{
std::{fmt::{*, Formatter}, fs, sync::atomic},
core::u32,
some_crate::bar,
},
::alloc::fmt::Result as FmtResult,
}; |
|
That's a good idea, thanks! I'm going to play with it and see what I can come up with. @rustbot author |
5e134b4 to
f3f0728
Compare
This comment has been minimized.
This comment has been minimized.
f3f0728 to
90de61a
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
90de61a to
6adb667
Compare
|
@rustbot ready Thanks for the suggestion Jarcho! I've done a larger re-write fixing multiple issues with this lint in #17364, but I've structured it so the multi-import fix can be pulled out into this PR for the sake of review. I've tried to structure my rewrite to match the original implementation as much as possible to make the diff easier to review, but it's probably easier to just read the new implementation as if it's new. |
Objective
std_instead_of_core]: false positives forcore::io/MSRV #16964.std_instead_of_corecreates an invalid fix on stacked imports #12468Solution
The current version of this lint can improperly trigger a suggestion for certain complex
usestatements. This PR attempts to resolve this by trackingItemKind::Useitems, which allows determining when paths are used within the context of a multi-import (UseKind::ListStem). Lints can then be appropriately grouped before determining if a suggestion is possible.This PR is the first commit from a re-write branch #17364, which acts on this suggestion and attempts to resolve many currently open issues for this lint.
One consequence of this PR is that
CrateNumis used to determine what crate items are being imported from, instead of the symbol of the first segment in the path. This resolves an unreported issue where doing something likeextern crate core as std;would causestd_instead_of_coreto emit erroneously. It also caused cases likeextern crate std as core;to not emitstd_instead_of_core.Unfortunately, this does lead to an issue for
extern crate std as alloc; (which is used e.g. here), since the suggestion will be to replaceallocwithalloc. This is fixed in #17364 since it also fixes #15836 & #16695 by checking what nameallocis available under, and demotes the suggestion to a help if it isn't.Notes
std_instead_of_core]: false positives forcore::io/MSRV #16964, but has been split off for ease of review. Either PR can be merged first.Please write a short comment explaining your change (or "none" for internal only changes)
changelog: [
std_instead_of_core], [std_instead_of_alloc], [alloc_instead_of_core]: fix certain false positives for mulit-imports.changelog: [
std_instead_of_core], [std_instead_of_alloc], [alloc_instead_of_core]: lint cases wherestdorallocare renamed.