Rewrite std_instead_of_core#17364
Conversation
|
Lintcheck changes for a834ba8
This comment will be updated if you push new changes |
eca13eb to
226f1c7
Compare
std_instead_of_core
std_instead_of_corestd_instead_of_core
|
r? @llogiq rustbot has assigned @llogiq. Use Why was this reviewer chosen?The reviewer was selected based on:
|
|
r? Jarcho Please feel free to reverse this! I'm assigning it to you since this is an alternative to #17252 and based on your feedback to me, so I feel you'd be most appropriate to review. Again, thanks for all your help so far! |
This comment has been minimized.
This comment has been minimized.
226f1c7 to
a834ba8
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. |
Objective
std_instead_of_coredoes not detect uses of non-fully qualified items #11159std_instead_of_corecreates an invalid fix on stacked imports #12468alloc::collections::VecDeque#16695std_instead_of_core/etc. miss macros #17260Solution
At a high level, the lint now tracks the name of
allocandcore(and thus if they're available), and whatusegroup we might be within.LintPoints now also useCrateNumto determine where items are defined in, and tracks this information across all namespaces. These high level changes allow for fixing all of the above issues.As a worked example for how the lint is now designed to work, consider the following crate:
check_crateis called and makes note that the crate is notno_implicit_prelude/no_core, solibcoreis in the implicit prelude with the namecore.check_modis called and sets the currentCrateContextto the crate default (libcoreavailable ascore,liballocunavailable)check_itemis called onextern crate allocand notes thatalloc's original name is indeedalloc, and thusliballocis available under the namealloc. This is stored in the currentCrateContext.check_itemis called on theuse std::{...};statement which pushes anItemContextfor the full span onto the item context stack.check_itemis called on the group (akaListStem)std::{...}, the group's span is pushed on the item context stack.check_itemis called on the group (akaListStem)sync::{...}, the group's span is pushed on the item context stack.check_pathis called onstd::sync::Arcand aLintPointis pushed, notingArcfrom the type namespace and defined inliballocis being used fromlibstd.check_pathis called onstd::sync::Mutexand aLintPointis pushed, notingMutexfrom the type namespace and defined inlibstdis being used fromlibstd.check_item_postis called on thesync::{...}group again, so we inspect the collectedLintPoints and, since they have mixed defining crate numbers, we leave them unmerged.check_pathis called onstd::fmt::Resultand aLintPointis pushed, notingResultfrom the type namespace and defined inlibcoreis being used fromlibstd.check_item_postis called on thestd::{...}group again, so we inspect the collectedLintPoints and, since they have mixed defining crate numbers, we leave them unmerged.check_item_postis called on the wholeusestatement again, so we check for potential merges and emit lints forstd::sync::Arcandstd::fmt::Result.Lintcheck Interpretation
alloc_instead_of_corefmt::Resultwhen a crate doesuse std::fmt;. This is intended.borrow::{Borrow, BorrowMut}from two lints into one merged emission.std_instead_of_allocsync::Arcand catching crate renames (extern crate std as alloc, etc.)allocisn't actually available so the suggestion would immediately fail.std_instead_of_corehash::Hash.atomic::{AtomicBool, Ordering}extern crate core as std, etc.)std::fmt::{self, Debug}(whileDebugis defined incore,std::fmt::selfisalloc::fmt, so this cannot be a merged suggestion.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 false positive suggestions for multi-importschangelog: [
std_instead_of_core], [std_instead_of_alloc], [alloc_instead_of_core]: consider crate renames and availabilitychangelog: [
std_instead_of_core], [std_instead_of_alloc], [alloc_instead_of_core]: include relative pathschangelog: [
std_instead_of_core], [std_instead_of_alloc], [alloc_instead_of_core]: include macros