Skip to content

fix [std_instead_of_core]: don't assume alloc/core are in the extern prelude#17296

Open
bushrat011899 wants to merge 2 commits into
rust-lang:masterfrom
bushrat011899:std_instead_of_core_check_extern_prelude
Open

fix [std_instead_of_core]: don't assume alloc/core are in the extern prelude#17296
bushrat011899 wants to merge 2 commits into
rust-lang:masterfrom
bushrat011899:std_instead_of_core_check_extern_prelude

Conversation

@bushrat011899

@bushrat011899 bushrat011899 commented Jun 23, 2026

Copy link
Copy Markdown
Contributor

Objective

Solution

The current version of this lint doesn't consider the extern prelude before emitting a suggestion, which can lead to fix failures. For example:

#![warn(clippy::std_instead_of_alloc)]

// The should not be a suggestion, since `alloc` is not in scope.
use std::vec::Vec;
//~^ std_instead_of_alloc

This currently emits a failing fix, since it suggests replacing std::vec::Vec with alloc::vec::Vec, but alloc is not available. This PR addresses this by demoting suggestions to helps if alloc/core aren't obviously available. "Obvious" means:

  • core is available if no_core and no_implicit_prelude aren't used or extern crate core; is present at the crate root (and thus added to the extern prelude)
  • alloc is available if extern crate alloc; is present at the crate root (and thus added to the extern prelude)

Renaming crates, or non-root usage of extern crate is not considered for brevity. Since the lint is always emitted, the worst case scenario is a suggestion isn't automatically applicable.


Notes


Please write a short comment explaining your change (or "none" for internal only changes)

changelog: fix certain false positive suggestions for [std_instead_of_core] when alloc or core aren't in the extern prelude.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 23, 2026
@rustbot

rustbot commented Jun 23, 2026

Copy link
Copy Markdown
Collaborator

r? @Jarcho

rustbot has assigned @Jarcho.
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, dswij, llogiq, samueltardieu

@bushrat011899

Copy link
Copy Markdown
Contributor Author

@rustbot blocked

#17252 should be merged first.

@rustbot rustbot added S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties labels Jun 23, 2026
If `alloc` or `core` aren't present in the prelude, then downgrade suggestions to helps to avoid suggesting broken fixes.
@bushrat011899 bushrat011899 force-pushed the std_instead_of_core_check_extern_prelude branch from 0478a30 to e34e2dc Compare June 23, 2026 13:08
@github-actions

Copy link
Copy Markdown

Lintcheck changes for e34e2dc

Lint Added Removed Changed
clippy::std_instead_of_alloc 3 1 462
clippy::std_instead_of_core 9 4 4

This comment will be updated if you push new changes

@bushrat011899

Copy link
Copy Markdown
Contributor Author

The large quantity of changes in std_instead_of_alloc is expected, as alloc is usually only explicitly included when a crate is already aiming for no_std compatibility. So there are likely many instances of the suggestion being made, when it should only be a help level emission.

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

Labels

S-blocked Status: marked as blocked ❌ on something else such as an RFC or other implementation work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

std-instead-of-alloc: there is no alloc::collections::VecDeque std-instead-of-core: core not in scope

3 participants