Skip to content

Rewrite std_instead_of_core#17364

Open
bushrat011899 wants to merge 5 commits into
rust-lang:masterfrom
bushrat011899:std_instead_of_core_rewrite
Open

Rewrite std_instead_of_core#17364
bushrat011899 wants to merge 5 commits into
rust-lang:masterfrom
bushrat011899:std_instead_of_core_rewrite

Conversation

@bushrat011899

@bushrat011899 bushrat011899 commented Jul 6, 2026

Copy link
Copy Markdown
Contributor

Objective

Solution

At a high level, the lint now tracks the name of alloc and core (and thus if they're available), and what use group we might be within. LintPoints now also use CrateNum to 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:

extern crate alloc;

use std::{
    sync::{Arc, Mutex},
    fmt::Result as FmtResult,
};
  1. check_crate is called and makes note that the crate is not no_implicit_prelude/no_core, so libcore is in the implicit prelude with the name core.
  2. check_mod is called and sets the current CrateContext to the crate default (libcore available as core, liballoc unavailable)
  3. check_item is called on extern crate alloc and notes that alloc's original name is indeed alloc, and thus liballoc is available under the name alloc. This is stored in the current CrateContext.
  4. check_item is called on the use std::{...}; statement which pushes an ItemContext for the full span onto the item context stack.
  5. check_item is called on the group (aka ListStem) std::{...}, the group's span is pushed on the item context stack.
  6. check_item is called on the group (aka ListStem) sync::{...}, the group's span is pushed on the item context stack.
  7. check_path is called on std::sync::Arc and a LintPoint is pushed, noting Arc from the type namespace and defined in liballoc is being used from libstd.
  8. check_path is called on std::sync::Mutex and a LintPoint is pushed, noting Mutex from the type namespace and defined in libstd is being used from libstd.
  9. check_item_post is called on the sync::{...} group again, so we inspect the collected LintPoints and, since they have mixed defining crate numbers, we leave them unmerged.
  10. check_path is called on std::fmt::Result and a LintPoint is pushed, noting Result from the type namespace and defined in libcore is being used from libstd.
  11. check_item_post is called on the std::{...} group again, so we inspect the collected LintPoints and, since they have mixed defining crate numbers, we leave them unmerged.
  12. check_item_post is called on the whole use statement again, so we check for potential merges and emit lints for std::sync::Arc and std::fmt::Result.

Lintcheck Interpretation

alloc_instead_of_core

  • Vast majority of additions come from linting cases like fmt::Result when a crate does use std::fmt;. This is intended.
  • The removal and change come from turning borrow::{Borrow, BorrowMut} from two lints into one merged emission.

std_instead_of_alloc

  • Additions come from sync::Arc and catching crate renames (extern crate std as alloc, etc.)
  • Removals come from combining lints together.
  • Vast majority of changes come from demoting suggestions down to help emissions, since alloc isn't actually available so the suggestion would immediately fail.

std_instead_of_core

  • Some additions come from non-FQN paths such as hash::Hash.
  • Further additions come from splitting incorrectly merged suggestion lints into multiple help emissions.
  • Most removals come from merging cases like atomic::{AtomicBool, Ordering}
  • Some removals come from catching crate renames (extern crate core as std, etc.)
  • Other removals come from improper merges, such as std::fmt::{self, Debug} (while Debug is defined in core, std::fmt::self is alloc::fmt, so this cannot be a merged suggestion.
  • All changes come from either splitting or combining lints

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-imports
changelog: [std_instead_of_core], [std_instead_of_alloc], [alloc_instead_of_core]: consider crate renames and availability
changelog: [std_instead_of_core], [std_instead_of_alloc], [alloc_instead_of_core]: include relative paths
changelog: [std_instead_of_core], [std_instead_of_alloc], [alloc_instead_of_core]: include macros

@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

Lintcheck changes for a834ba8

Lint Added Removed Changed
clippy::alloc_instead_of_core 2459 1 1
clippy::std_instead_of_alloc 9 2 412
clippy::std_instead_of_core 114 39 25

This comment will be updated if you push new changes

@bushrat011899 bushrat011899 force-pushed the std_instead_of_core_rewrite branch 3 times, most recently from eca13eb to 226f1c7 Compare July 7, 2026 01:07
@bushrat011899 bushrat011899 changed the title DRAFT: Rewrite [std_instead_of_core] DRAFT: Rewrite std_instead_of_core Jul 7, 2026
@bushrat011899 bushrat011899 changed the title DRAFT: Rewrite std_instead_of_core Rewrite std_instead_of_core Jul 8, 2026
@bushrat011899 bushrat011899 marked this pull request as ready for review July 8, 2026 01:12
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jul 8, 2026
@rustbot

rustbot commented Jul 8, 2026

Copy link
Copy Markdown
Collaborator

r? @llogiq

rustbot has assigned @llogiq.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: 8 candidates
  • 8 candidates expanded to 8 candidates
  • Random selection from Jarcho, llogiq, samueltardieu

@bushrat011899

Copy link
Copy Markdown
Contributor Author

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!

@rustbot rustbot assigned Jarcho and unassigned llogiq Jul 8, 2026
@rustbot

This comment has been minimized.

@bushrat011899 bushrat011899 force-pushed the std_instead_of_core_rewrite branch from 226f1c7 to a834ba8 Compare July 11, 2026 00:13
@rustbot

rustbot commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties

Projects

None yet

4 participants