Skip to content

fix [std_instead_of_core]: false negative for non-FQ paths #17303

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

fix [std_instead_of_core]: false negative for non-FQ paths #17303
bushrat011899 wants to merge 2 commits into
rust-lang:masterfrom
bushrat011899:std_instead_of_core_non_fq_path

Conversation

@bushrat011899

Copy link
Copy Markdown
Contributor

Objective

Solution

The current version of this lint only considers fully qualified paths. For example:

#![warn(clippy::std_instead_of_core)]
#![warn(clippy::std_instead_of_alloc)]
#![warn(clippy::alloc_instead_of_core)]

use std::fmt;

struct S;

impl fmt::Display for S {
    fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
        todo!()
    }
}

This should warn that Display, Formatter, and Result are all std_instead_of_core or suggest replacing std::fmt with core::fmt. However, in the late lint pass it's not straightforward to determine all usages of that particular fmt, so the current best effort suggestion that can be made is std_instead_of_alloc on std::fmt, since it is a reexport of alloc::fmt.

This PR allows the lint to work on non-fully qualified paths by checking the resolution of the base of a path and prepending it to the currently checked path. For example, when checking fmt::Display, fmt is known not to be a crate root, so we check what its fully resolved path is, and determine it to be alloc::fmt. Concatenating the base and the path to check yields a fully qualified alloc::fmt::Display, and the lint can proceed with its checks, and emit alloc_instead_of_core.

Note that in this example, fmt::Display is linted as-if it comes from alloc::fmt::Display, yet in the user's code it actually comes from std::fmt::Display. This actually works perfectly, since we also know that std::fmt will be linted as std_instead_of_alloc. In effect, the non-fully qualified paths get linted as-if their base paths are already passing. If they aren't, we know they'll be linted anyway, so the user will be made aware.


Notes


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

changelog: allow [std_instead_of_core] to check non-fully qualified paths.

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

rustbot commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

r? @samueltardieu

rustbot has assigned @samueltardieu.
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

r? Jarcho

I already have 2 other related PRs assigned to Jarcho so it probably makes the most sense for them to review this one as well.

@rustbot blocked

Shouldn't be reviewed until #17252 is merged

@rustbot rustbot assigned Jarcho and unassigned samueltardieu Jun 24, 2026
@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 24, 2026
@github-actions

Copy link
Copy Markdown

Lintcheck changes for 1bc460f

Lint Added Removed Changed
clippy::alloc_instead_of_core 2464 0 0
clippy::std_instead_of_alloc 5 1 65
clippy::std_instead_of_core 109 4 4

This comment will be updated if you push new changes

@bushrat011899

Copy link
Copy Markdown
Contributor Author

The large quantity of additions to alloc_instead_of_core is expected, as there are many instances of crates importing std::fmt exclusively as shorthand for items available in core::fmt. It's emitted as alloc_instead_of_core because the lint sees fmt as resolving to alloc::fmt (alloc defines its own fmt module which reexports the contents of core::fmt, so it is a distinct module). A similar explanation exists for std_instead_of_core and std::hash.

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_core does not detect uses of non-fully qualified items

4 participants