about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2025-06-26 22:51:58 +0200
committerSamuel Tardieu <sam@rfc1149.net>2025-06-26 23:09:37 +0200
commit9117cb0223eaf72ab9015cee10549b2b774d8f74 (patch)
treeb3bea3c17f03b182f8a16805841b39634354cafb
parentc5dbd1de07e0407b9687619a868384d6de06253f (diff)
downloadrust-9117cb0223eaf72ab9015cee10549b2b774d8f74.tar.gz
rust-9117cb0223eaf72ab9015cee10549b2b774d8f74.zip
`zero_ptr`: lint in `const` context as well
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.
-rw-r--r--book/src/lint_configuration.md1
-rw-r--r--clippy_config/src/conf.rs1
-rw-r--r--clippy_lints/src/casts/mod.rs2
-rw-r--r--clippy_lints/src/casts/zero_ptr.rs5
-rw-r--r--clippy_utils/src/msrvs.rs2
-rw-r--r--tests/ui/zero_ptr.fixed8
-rw-r--r--tests/ui/zero_ptr.rs8
-rw-r--r--tests/ui/zero_ptr.stderr8
8 files changed, 30 insertions, 5 deletions
diff --git a/book/src/lint_configuration.md b/book/src/lint_configuration.md
index e9b7f42a183..992ed2c6aaa 100644
--- a/book/src/lint_configuration.md
+++ b/book/src/lint_configuration.md
@@ -892,6 +892,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
 * [`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 841facdca06..555f54bcfb8 100644
--- a/clippy_config/src/conf.rs
+++ b/clippy_config/src/conf.rs
@@ -794,6 +794,7 @@ define_Conf! {
         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 daae9a8bb08..37accff5eaa 100644
--- a/clippy_lints/src/casts/mod.rs
+++ b/clippy_lints/src/casts/mod.rs
@@ -878,7 +878,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
             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 a34af6bc226..f4738e7b0d5 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 rustc_lint::LateContext;
 
 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 7a0bef1a9bb..24ed4c3a8be 100644
--- a/clippy_utils/src/msrvs.rs
+++ b/clippy_utils/src/msrvs.rs
@@ -74,7 +74,7 @@ msrv_aliases! {
     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 f2375d57f3a..f9d9d2db176 100644
--- a/tests/ui/zero_ptr.fixed
+++ b/tests/ui/zero_ptr.fixed
@@ -16,3 +16,11 @@ fn main() {
     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 ee01e426a43..41455fee5b5 100644
--- a/tests/ui/zero_ptr.rs
+++ b/tests/ui/zero_ptr.rs
@@ -16,3 +16,11 @@ fn main() {
     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 8dc781f3625..81269de6c60 100644
--- a/tests/ui/zero_ptr.stderr
+++ b/tests/ui/zero_ptr.stderr
@@ -31,5 +31,11 @@ error: `0 as *mut _` detected
 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