`zero_ptr`: lint in `const` context as well (#15152)
The lint was extra restrictive, and didn't suggest using
`core::ptr::null` and `core::ptr::null_mut` in `const` contexts although
they have been const-stabilized since Rust 1.24.
changelog: [`zero_ptr`]: lint in `const` context as well
@rustbot label +I-false-negative +C-bug
diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md
index e9b7f42..992ed2c 100644
--- a/book/src/lint_configuration.md
+++ b/book/src/lint_configuration.md
@@ -892,6 +892,7 @@
* [`unnested_or_patterns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnested_or_patterns)
* [`unused_trait_names`](https://rust-lang.github.io/rust-clippy/master/index.html#unused_trait_names)
* [`use_self`](https://rust-lang.github.io/rust-clippy/master/index.html#use_self)
+* [`zero_ptr`](https://rust-lang.github.io/rust-clippy/master/index.html#zero_ptr)
## `pass-by-value-size-limit`
diff --git a/clippy_config/src/conf.rs b/clippy_config/src/conf.rs
index 841facd..555f54b 100644
--- a/clippy_config/src/conf.rs
+++ b/clippy_config/src/conf.rs
@@ -794,6 +794,7 @@
unnested_or_patterns,
unused_trait_names,
use_self,
+ zero_ptr,
)]
msrv: Msrv = Msrv::default(),
/// The minimum size (in bytes) to consider a type for passing by reference instead of by value.
diff --git a/clippy_lints/src/casts/mod.rs b/clippy_lints/src/casts/mod.rs
index daae9a8..37accff 100644
--- a/clippy_lints/src/casts/mod.rs
+++ b/clippy_lints/src/casts/mod.rs
@@ -878,7 +878,7 @@
confusing_method_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
fn_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_from_expr, cast_from, cast_to);
- zero_ptr::check(cx, expr, cast_from_expr, cast_to_hir);
+ zero_ptr::check(cx, expr, cast_from_expr, cast_to_hir, self.msrv);
if self.msrv.meets(cx, msrvs::MANUAL_DANGLING_PTR) {
manual_dangling_ptr::check(cx, expr, cast_from_expr, cast_to_hir);
diff --git a/clippy_lints/src/casts/zero_ptr.rs b/clippy_lints/src/casts/zero_ptr.rs
index a34af6b..f4738e7 100644
--- a/clippy_lints/src/casts/zero_ptr.rs
+++ b/clippy_lints/src/casts/zero_ptr.rs
@@ -1,4 +1,5 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
+use clippy_utils::msrvs::{self, Msrv};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::{is_in_const_context, is_integer_literal, std_or_core};
use rustc_errors::Applicability;
@@ -7,10 +8,10 @@
use super::ZERO_PTR;
-pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) {
+pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>, msrv: Msrv) {
if let TyKind::Ptr(ref mut_ty) = to.kind
&& is_integer_literal(from, 0)
- && !is_in_const_context(cx)
+ && (!is_in_const_context(cx) || msrv.meets(cx, msrvs::PTR_NULL))
&& let Some(std_or_core) = std_or_core(cx)
{
let (msg, sugg_fn) = match mut_ty.mutbl {
diff --git a/clippy_utils/src/msrvs.rs b/clippy_utils/src/msrvs.rs
index 7a0bef1..24ed4c3 100644
--- a/clippy_utils/src/msrvs.rs
+++ b/clippy_utils/src/msrvs.rs
@@ -74,7 +74,7 @@
1,28,0 { FROM_BOOL, REPEAT_WITH, SLICE_FROM_REF }
1,27,0 { ITERATOR_TRY_FOLD }
1,26,0 { RANGE_INCLUSIVE, STRING_RETAIN }
- 1,24,0 { IS_ASCII_DIGIT }
+ 1,24,0 { IS_ASCII_DIGIT, PTR_NULL }
1,18,0 { HASH_MAP_RETAIN, HASH_SET_RETAIN }
1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST, EXPECT_ERR }
1,16,0 { STR_REPEAT }
diff --git a/tests/ui/zero_ptr.fixed b/tests/ui/zero_ptr.fixed
index f2375d5..f9d9d2d 100644
--- a/tests/ui/zero_ptr.fixed
+++ b/tests/ui/zero_ptr.fixed
@@ -16,3 +16,11 @@
let z = 0;
let _ = z as *const usize; // this is currently not caught
}
+
+const fn in_const_context() {
+ #[clippy::msrv = "1.23"]
+ let _: *const usize = 0 as *const _;
+ #[clippy::msrv = "1.24"]
+ let _: *const usize = std::ptr::null();
+ //~^ zero_ptr
+}
diff --git a/tests/ui/zero_ptr.rs b/tests/ui/zero_ptr.rs
index ee01e42..41455fe 100644
--- a/tests/ui/zero_ptr.rs
+++ b/tests/ui/zero_ptr.rs
@@ -16,3 +16,11 @@
let z = 0;
let _ = z as *const usize; // this is currently not caught
}
+
+const fn in_const_context() {
+ #[clippy::msrv = "1.23"]
+ let _: *const usize = 0 as *const _;
+ #[clippy::msrv = "1.24"]
+ let _: *const usize = 0 as *const _;
+ //~^ zero_ptr
+}
diff --git a/tests/ui/zero_ptr.stderr b/tests/ui/zero_ptr.stderr
index 8dc781f..81269de 100644
--- a/tests/ui/zero_ptr.stderr
+++ b/tests/ui/zero_ptr.stderr
@@ -31,5 +31,11 @@
LL | foo(0 as *const _, 0 as *mut _);
| ^^^^^^^^^^^ help: try: `std::ptr::null_mut()`
-error: aborting due to 5 previous errors
+error: `0 as *const _` detected
+ --> tests/ui/zero_ptr.rs:24:27
+ |
+LL | let _: *const usize = 0 as *const _;
+ | ^^^^^^^^^^^^^ help: try: `std::ptr::null()`
+
+error: aborting due to 6 previous errors