rust-lang / rust-lang/rust-clippy

std-instead-of-core: core not in scope

Open
#15,836 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Using the following flags

--force-warn clippy::std-instead-of-core

this code:

// This test's filename is... a bit verbose. But it ensures we suggest the correct code when `Ord`
// is not in scope.
#![no_main]
#![no_implicit_prelude]
//@no-rustfix
extern crate std;

use std::cmp::{self, Eq, Ordering, PartialEq, PartialOrd};
use std::option::Option::{self, Some};
use std::todo;

// lint

#[derive(Eq, PartialEq)]
struct A(u32);

impl cmp::Ord for A {
    fn cmp(&self, other: &Self) -> Ordering {
        todo!();
    }
}

impl PartialOrd for A {
    //~^ non_canonical_partial_ord_impl
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // NOTE: This suggestion is wrong, as `Ord` is not in scope. But this should be fine as it isn't
        // automatically applied
        todo!();
    }
}

#[derive(Eq, PartialEq)]
struct B(u32);

impl B {
    fn cmp(&self, other: &Self) -> Ordering {
        todo!();
    }
}

impl cmp::Ord for B {
    fn cmp(&self, other: &Self) -> Ordering {
        todo!();
    }
}

impl PartialOrd for B {
    //~^ non_canonical_partial_ord_impl
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // This calls `B.cmp`, not `Ord::cmp`!
        Some(self.cmp(other))
    }
}

caused the following diagnostics:

    Checking _non_canonical_partial_ord_impl_fully_qual v0.1.0 (/tmp/icemaker_global_tempdir.paNtiJF6jVLU/icemaker_clippyfix_tempdir.MkAby3WtvAcW/_non_canonical_partial_ord_impl_fully_qual)
warning: used import from `std` instead of `core`
 --> src/lib.rs:8:5
  |
8 | use std::cmp::{self, Eq, Ordering, PartialEq, PartialOrd};
  |     ^^^ help: consider importing the item from `core`: `core`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_core
  = note: requested on the command line with `--force-warn clippy::std-instead-of-core`

warning: used import from `std` instead of `core`
 --> src/lib.rs:9:5
  |
9 | use std::option::Option::{self, Some};
  |     ^^^ help: consider importing the item from `core`: `core`
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#std_instead_of_core

warning: `_non_canonical_partial_ord_impl_fully_qual` (lib) generated 2 warnings (run `cargo clippy --fix --lib -p _non_canonical_partial_ord_impl_fully_qual` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.28s

However after applying these diagnostics, the resulting code:

// This test's filename is... a bit verbose. But it ensures we suggest the correct code when `Ord`
// is not in scope.
#![no_main]
#![no_implicit_prelude]
//@no-rustfix
extern crate std;

use core::cmp::{self, Eq, Ordering, PartialEq, PartialOrd};
use core::option::Option::{self, Some};
use std::todo;

// lint

#[derive(Eq, PartialEq)]
struct A(u32);

impl cmp::Ord for A {
    fn cmp(&self, other: &Self) -> Ordering {
        todo!();
    }
}

impl PartialOrd for A {
    //~^ non_canonical_partial_ord_impl
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // NOTE: This suggestion is wrong, as `Ord` is not in scope. But this should be fine as it isn't
        // automatically applied
        todo!();
    }
}

#[derive(Eq, PartialEq)]
struct B(u32);

impl B {
    fn cmp(&self, other: &Self) -> Ordering {
        todo!();
    }
}

impl cmp::Ord for B {
    fn cmp(&self, other: &Self) -> Ordering {
        todo!();
    }
}

impl PartialOrd for B {
    //~^ non_canonical_partial_ord_impl
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        // This calls `B.cmp`, not `Ord::cmp`!
        Some(self.cmp(other))
    }
}

no longer compiled:

    Checking _non_canonical_partial_ord_impl_fully_qual v0.1.0 (/tmp/icemaker_global_tempdir.paNtiJF6jVLU/icemaker_clippyfix_tempdir.MkAby3WtvAcW/_non_canonical_partial_ord_impl_fully_qual)
error[E0433]: failed to resolve: use of unresolved module or unlinked crate `core`
 --> src/lib.rs:8:5
  |
8 | use core::cmp::{self, Eq, Ordering, PartialEq, PartialOrd};
  |     ^^^^ use of unresolved module or unlinked crate `core`
  |
  = help: if you wanted to use a crate named `core`, use `cargo add core` to add it to your `Cargo.toml`

error[E0433]: failed to resolve: use of unresolved module or unlinked crate `core`
 --> src/lib.rs:9:5
  |
9 | use core::option::Option::{self, Some};
  |     ^^^^ use of unresolved module or unlinked crate `core`
  |
  = help: if you wanted to use a crate named `core`, use `cargo add core` to add it to your `Cargo.toml`

error[E0432]: unresolved import `core`
 --> src/lib.rs:8:5
  |
8 | use core::cmp::{self, Eq, Ordering, PartialEq, PartialOrd};
  |     ^^^^ use of unresolved module or unlinked crate `core`
  |
  = help: if you wanted to use a crate named `core`, use `cargo add core` to add it to your `Cargo.toml`

Some errors have detailed explanations: E0432, E0433.
For more information about an error, try `rustc --explain E0432`.
error: could not compile `_non_canonical_partial_ord_impl_fully_qual` (lib test) due to 3 previous errors
warning: build failed, waiting for other jobs to finish...
error: could not compile `_non_canonical_partial_ord_impl_fully_qual` (lib) due to 3 previous errors

Version:

rustc 1.92.0-nightly (4a54b26d3 2025-10-07)
binary: rustc
commit-hash: 4a54b26d30dac43778afb0e503524b763fce0eee
commit-date: 2025-10-07
host: x86_64-unknown-linux-gnu
release: 1.92.0-nightly
LLVM version: 21.1.2

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with --force-warn clippy::std-instead-of-core using the shown no_implicit_prelude example, then trace the std-instead-of-core lint and its suggestion handling. The fix is complete when applying the reported suggestions leaves the test compiling and preserves correct suggestions when Ord is not in scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.