Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::alloc_instead_of_core: Finds items imported through alloc when available through core
What it meansFinds items imported through alloc when available through core. It is in the restriction group and off by default.
Does it matter?
Crates which have no_std compatibility and may optionally require alloc may wish to ensure types are imported from core to ensure disabling alloc does not cause the crate to fail to compile. This lint is also useful for crates migrating to become no_std compatible.
Lints that forbid language features for specific codebases (no unwrap, no indexing...). Off by default; pick them one by one, never the whole group.
How to fix it
Clippy can fix this one for you: run cargo clippy --fix (add --allow-dirty if you have uncommitted changes). Review the diff before you commit.
Flagged
use alloc::slice::from_ref;
Write this instead
use core::slice::from_ref;
Turn it off, or on
It is off by default. To use it, set it to warn or deny:
Whole crate (Cargo.toml)
[lints.clippy]
alloc_instead_of_core = "warn"In code
#![warn(clippy::alloc_instead_of_core)]One run
cargo clippy -- -W clippy::alloc-instead-of-coreThe [lints] table needs Rust 1.74 or newer. In a workspace, put it under [workspace.lints.clippy] and add [lints] workspace = true to each member.
Clippy's own documentation
What it does
Finds items imported through alloc when available through core.
Why restrict this?
Crates which have no_std compatibility and may optionally require alloc may wish to ensure types are
imported from core to ensure disabling alloc does not cause the crate to fail to compile. This lint
is also useful for crates migrating to become no_std compatible.
Known problems
The lint is only partially aware of the required MSRV for items that were originally in std but moved
to core.
Example
use alloc::slice::from_ref;
Use instead:
use core::slice::from_ref;
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on Options
- module_name_repetitionsDetects public item names that are prefixed or suffixed by the containing public module’s name
- expect_usedChecks for .expect() or .expect_err() calls on Results and .expect() call on Options
- absolute_pathsconsider bringing this path into scope with the `use` keyword
- allow_attributes#[allow] attribute found
- allow_attributes_without_reason`allow` attribute without specifying a reason
- arbitrary_source_item_orderingincorrect ordering of items (must be alphabetically ordered)
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::unwrap_usedChecks for .unwrap() or .unwrap_err() calls on Results and .unwrap() call on OptionsRead it next →Did this answer your question?