Clippy lints / Restriction
Clippy lint · Restriction · checked with Clippy 0.1.99
clippy::std_instead_of_alloc: used import from `std` instead of `alloc`
What it meansFinds items imported through std when available through alloc. It is in the restriction group and off by default.
What you see
warning: used import from `std` instead of `alloc` --> main.rs:3:5 | 3 | use std::vec::Vec; | ^^^ help: consider importing the item from `alloc`: `alloc` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#std_instead_of_alloc = note: requested on the command line with `-W clippy::std-instead-of-alloc`
Real output of Clippy 0.1.99 (Rust 1.99) on the example below. Names and line numbers in yours will differ.
Does it matter?
Crates which have no_std compatibility and require alloc may wish to ensure types are imported from alloc to ensure disabling std 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 std::vec::Vec;
Write this instead
use alloc::vec::Vec;
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]
std_instead_of_alloc = "warn"In code
#![warn(clippy::std_instead_of_alloc)]One run
cargo clippy -- -W clippy::std-instead-of-allocThe [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 std when available through alloc.
Why restrict this?
Crates which have no_std compatibility and require alloc may wish to ensure types are imported from
alloc to ensure disabling std does not cause the crate to fail to compile. This lint is also useful
for crates migrating to become no_std compatible.
Example
use std::vec::Vec;
Use instead:
use alloc::vec::Vec;
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
- shadow_unrelatedChecks for bindings that shadow other bindings already in scope, either without an initialization or with one that does not even use the original value
- single_call_fnthis function is only used once
- single_char_lifetime_namessingle-character lifetime names are likely uninformative
- std_instead_of_coreused import from `std` instead of `core`
- str_to_string`to_string()` called on a `&str`
- string_addyou added something to a string. Consider using `String::push_str()` instead
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?