Clippy lints / Style
Clippy lint · Style · checked with Clippy 0.1.99
clippy::zero_ptr: `0 as *const _` detected
What it meansCatch casts from 0 to some pointer type It is in the style group and warns by default.
What you see
warning: `0 as *const _` detected --> main.rs:3:9 | 3 | let a = 0 as *const u32; | ^^^^^^^^^^^^^^^ help: try: `std::ptr::null::<u32>()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/stable/index.html#zero_ptr = note: `#[warn(clippy::zero_ptr)]` on by default
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?
This generally means null and is better expressed as {std, core}::ptr::{null, null_mut}.
Code that works but is not idiomatic Rust. Warns by default.
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
let a = 0 as *const u32;
Write this instead
let a = std::ptr::null::<u32>();
Turn it off, or on
Allow it where it does not fit your code, as narrowly as you can:
On one item
#[allow(clippy::zero_ptr)]
fn my_function() { /* ... */ }Whole crate (Cargo.toml)
[lints.clippy]
zero_ptr = "allow"One run
cargo clippy -- -A clippy::zero-ptrThe [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.
Settings in clippy.toml
msrv: The minimum rust version that the project supports. Defaults to the rust-version field in Cargo.toml
Clippy's own documentation
What it does
Catch casts from 0 to some pointer type
Why is this bad?
This generally means null and is better expressed as
{std, core}::ptr::{null, null_mut}.
Example
let a = 0 as *const u32;
Use instead:
let a = std::ptr::null::<u32>();
Configuration
-
msrv: The minimum rust version that the project supports. Defaults to therust-versionfield inCargo.toml(default:
current version)
From the Clippy lint list (0.1.99), part of the Rust project, MIT or Apache-2.0. Official entry.
Related lints
- needless_returnunneeded `return` statement
- new_without_defaultChecks for public types with a pub fn new() -> Self method and no implementation of Default
- len_zeroChecks for getting the length of something via .len() just to compare to zero, and suggests using .is_empty() where applicable
- write_with_newlineThis lint warns when you use write!() with a format string that ends in a newline
- writeln_empty_stringThis lint warns when you use writeln!(buf, "") to print a newline
- wrong_self_conventionChecks for methods with certain name prefixes or suffixes, and which do not adhere to standard conventions regarding how self is taken. The actual rules are: PrefixPostfixself takenself type as_none&self or &mut selfany from_nonenoneany into_noneselfany is_none&mut self or &self or noneany to__mut&mut selfany to_not _mutselfCopy to_not _mut&selfnot Copy Note: Clippy doesn’t trigger methods with to_ prefix in: Traits definition. Clippy can not tell if a type that implements a trait is Copy or not. Traits implementation, when &self is taken. The method signature is controlled by the trait and often &self is required for all types that implement the trait (see e.g
All 847 Clippy lints · Rust compiler errors · Cargo.toml checker
Up next
Related lintclippy::needless_returnunneeded `return` statementRead it next →Did this answer your question?