Skip to content

std-instead-of-core: crate change is suggested even though not all items come from the new crate #15143

Open
@matthiaskrgr

Description

@matthiaskrgr

Using the following flags

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

this code:

#![deny(dangling_pointers_from_temporaries)]
#![feature(sync_unsafe_cell)]

use std::cell::{Cell, SyncUnsafeCell, UnsafeCell};
use std::ffi::{CStr, CString};
use std::mem::MaybeUninit;

struct AsPtrFake;

impl AsPtrFake {
    fn as_ptr(&self) -> *const () {
        std::ptr::null()
    }
}

fn declval<T>() -> T {
    loop {}
}

fn main() {
    declval::<CString>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
    declval::<String>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `String` will be dropped
    declval::<Vec<u8>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Vec<u8>` will be dropped
    declval::<Box<CString>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<CString>` will be dropped
    declval::<Box<[u8]>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<[u8]>` will be dropped
    declval::<Box<str>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<str>` will be dropped
    declval::<Box<CStr>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<CStr>` will be dropped
    declval::<[u8; 10]>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `[u8; 10]` will be dropped
    declval::<Box<[u8; 10]>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<[u8; 10]>` will be dropped
    declval::<Box<Vec<u8>>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<Vec<u8>>` will be dropped
    declval::<Box<String>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<String>` will be dropped
    declval::<Box<Box<Box<Box<[u8]>>>>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<Box<Box<Box<[u8]>>>>` will be dropped
    declval::<Cell<u8>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Cell<u8>` will be dropped
    declval::<MaybeUninit<u8>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `MaybeUninit<u8>` will be dropped
    declval::<Vec<AsPtrFake>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Vec<AsPtrFake>` will be dropped
    declval::<UnsafeCell<u8>>().get();
    //~^ ERROR a dangling pointer will be produced because the temporary `UnsafeCell<u8>` will be dropped
    declval::<SyncUnsafeCell<u8>>().get();
    //~^ ERROR a dangling pointer will be produced because the temporary `SyncUnsafeCell<u8>` will be dropped
    declval::<Box<AsPtrFake>>().as_ptr();
    declval::<AsPtrFake>().as_ptr();
}

caused the following diagnostics:

    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Blocking waiting for file lock on package cache
    Checking _types v0.1.0 (/tmp/icemaker_global_tempdir.hTWDZB3NcvpG/icemaker_clippyfix_tempdir.V4DD5ifBcV5q/_types)
warning: used import from `std` instead of `core`
 --> src/main.rs:4:5
  |
4 | use std::cell::{Cell, SyncUnsafeCell, UnsafeCell};
  |     ^^^ 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/main.rs:5:5
  |
5 | use std::ffi::{CStr, CString};
  |     ^^^ 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: used import from `std` instead of `core`
 --> src/main.rs:6:5
  |
6 | use std::mem::MaybeUninit;
  |     ^^^ 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: used import from `std` instead of `core`
  --> src/main.rs:12:9
   |
12 |         std::ptr::null()
   |         ^^^ 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: `_types` (bin "_types") generated 4 warnings (run `cargo clippy --fix --bin "_types"` to apply 4 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.56s

However after applying these diagnostics, the resulting code:

#![deny(dangling_pointers_from_temporaries)]
#![feature(sync_unsafe_cell)]

use core::cell::{Cell, SyncUnsafeCell, UnsafeCell};
use core::ffi::{CStr, CString};
use core::mem::MaybeUninit;

struct AsPtrFake;

impl AsPtrFake {
    fn as_ptr(&self) -> *const () {
        core::ptr::null()
    }
}

fn declval<T>() -> T {
    loop {}
}

fn main() {
    declval::<CString>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `CString` will be dropped
    declval::<String>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `String` will be dropped
    declval::<Vec<u8>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Vec<u8>` will be dropped
    declval::<Box<CString>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<CString>` will be dropped
    declval::<Box<[u8]>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<[u8]>` will be dropped
    declval::<Box<str>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<str>` will be dropped
    declval::<Box<CStr>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<CStr>` will be dropped
    declval::<[u8; 10]>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `[u8; 10]` will be dropped
    declval::<Box<[u8; 10]>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<[u8; 10]>` will be dropped
    declval::<Box<Vec<u8>>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<Vec<u8>>` will be dropped
    declval::<Box<String>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<String>` will be dropped
    declval::<Box<Box<Box<Box<[u8]>>>>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Box<Box<Box<Box<[u8]>>>>` will be dropped
    declval::<Cell<u8>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Cell<u8>` will be dropped
    declval::<MaybeUninit<u8>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `MaybeUninit<u8>` will be dropped
    declval::<Vec<AsPtrFake>>().as_ptr();
    //~^ ERROR a dangling pointer will be produced because the temporary `Vec<AsPtrFake>` will be dropped
    declval::<UnsafeCell<u8>>().get();
    //~^ ERROR a dangling pointer will be produced because the temporary `UnsafeCell<u8>` will be dropped
    declval::<SyncUnsafeCell<u8>>().get();
    //~^ ERROR a dangling pointer will be produced because the temporary `SyncUnsafeCell<u8>` will be dropped
    declval::<Box<AsPtrFake>>().as_ptr();
    declval::<AsPtrFake>().as_ptr();
}

no longer compiled:

    Checking _types v0.1.0 (/tmp/icemaker_global_tempdir.hTWDZB3NcvpG/icemaker_clippyfix_tempdir.V4DD5ifBcV5q/_types)
error[E0432]: unresolved import `core::ffi::CString`
 --> src/main.rs:5:23
  |
5 | use core::ffi::{CStr, CString};
  |                       ^^^^^^^ no `CString` in `ffi`
  |
  = help: consider importing this struct instead:
          std::ffi::CString

For more information about this error, try `rustc --explain E0432`.
error: could not compile `_types` (bin "_types" test) due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_types` (bin "_types") due to 1 previous error

Version:

rustc 1.90.0-nightly (8cf5fad73 2025-06-25)
binary: rustc
commit-hash: 8cf5fad73d4e8f41863ecc3bcfa114eabc951faa
commit-date: 2025-06-25
host: x86_64-unknown-linux-gnu
release: 1.90.0-nightly
LLVM version: 20.1.7

Metadata

Metadata

Assignees

No one assigned

    Labels

    I-suggestion-causes-errorIssue: The suggestions provided by this Lint cause an ICE/error when applied

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions