Skip to content

Commit 8633bcb

Browse files
authored
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
2 parents c5dbd1d + 9117cb0 commit 8633bcb

File tree

8 files changed

+30
-5
lines changed

8 files changed

+30
-5
lines changed

book/src/lint_configuration.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -892,6 +892,7 @@ The minimum rust version that the project supports. Defaults to the `rust-versio
892892
* [`unnested_or_patterns`](https://rust-lang.github.io/rust-clippy/master/index.html#unnested_or_patterns)
893893
* [`unused_trait_names`](https://rust-lang.github.io/rust-clippy/master/index.html#unused_trait_names)
894894
* [`use_self`](https://rust-lang.github.io/rust-clippy/master/index.html#use_self)
895+
* [`zero_ptr`](https://rust-lang.github.io/rust-clippy/master/index.html#zero_ptr)
895896

896897

897898
## `pass-by-value-size-limit`

clippy_config/src/conf.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -794,6 +794,7 @@ define_Conf! {
794794
unnested_or_patterns,
795795
unused_trait_names,
796796
use_self,
797+
zero_ptr,
797798
)]
798799
msrv: Msrv = Msrv::default(),
799800
/// The minimum size (in bytes) to consider a type for passing by reference instead of by value.

clippy_lints/src/casts/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -878,7 +878,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
878878
confusing_method_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
879879
fn_to_numeric_cast::check(cx, expr, cast_from_expr, cast_from, cast_to);
880880
fn_to_numeric_cast_with_truncation::check(cx, expr, cast_from_expr, cast_from, cast_to);
881-
zero_ptr::check(cx, expr, cast_from_expr, cast_to_hir);
881+
zero_ptr::check(cx, expr, cast_from_expr, cast_to_hir, self.msrv);
882882

883883
if self.msrv.meets(cx, msrvs::MANUAL_DANGLING_PTR) {
884884
manual_dangling_ptr::check(cx, expr, cast_from_expr, cast_to_hir);

clippy_lints/src/casts/zero_ptr.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::msrvs::{self, Msrv};
23
use clippy_utils::source::SpanRangeExt;
34
use clippy_utils::{is_in_const_context, is_integer_literal, std_or_core};
45
use rustc_errors::Applicability;
@@ -7,10 +8,10 @@ use rustc_lint::LateContext;
78

89
use super::ZERO_PTR;
910

10-
pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) {
11+
pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>, msrv: Msrv) {
1112
if let TyKind::Ptr(ref mut_ty) = to.kind
1213
&& is_integer_literal(from, 0)
13-
&& !is_in_const_context(cx)
14+
&& (!is_in_const_context(cx) || msrv.meets(cx, msrvs::PTR_NULL))
1415
&& let Some(std_or_core) = std_or_core(cx)
1516
{
1617
let (msg, sugg_fn) = match mut_ty.mutbl {

clippy_utils/src/msrvs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ msrv_aliases! {
7474
1,28,0 { FROM_BOOL, REPEAT_WITH, SLICE_FROM_REF }
7575
1,27,0 { ITERATOR_TRY_FOLD }
7676
1,26,0 { RANGE_INCLUSIVE, STRING_RETAIN }
77-
1,24,0 { IS_ASCII_DIGIT }
77+
1,24,0 { IS_ASCII_DIGIT, PTR_NULL }
7878
1,18,0 { HASH_MAP_RETAIN, HASH_SET_RETAIN }
7979
1,17,0 { FIELD_INIT_SHORTHAND, STATIC_IN_CONST, EXPECT_ERR }
8080
1,16,0 { STR_REPEAT }

tests/ui/zero_ptr.fixed

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ fn main() {
1616
let z = 0;
1717
let _ = z as *const usize; // this is currently not caught
1818
}
19+
20+
const fn in_const_context() {
21+
#[clippy::msrv = "1.23"]
22+
let _: *const usize = 0 as *const _;
23+
#[clippy::msrv = "1.24"]
24+
let _: *const usize = std::ptr::null();
25+
//~^ zero_ptr
26+
}

tests/ui/zero_ptr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,11 @@ fn main() {
1616
let z = 0;
1717
let _ = z as *const usize; // this is currently not caught
1818
}
19+
20+
const fn in_const_context() {
21+
#[clippy::msrv = "1.23"]
22+
let _: *const usize = 0 as *const _;
23+
#[clippy::msrv = "1.24"]
24+
let _: *const usize = 0 as *const _;
25+
//~^ zero_ptr
26+
}

tests/ui/zero_ptr.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,11 @@ error: `0 as *mut _` detected
3131
LL | foo(0 as *const _, 0 as *mut _);
3232
| ^^^^^^^^^^^ help: try: `std::ptr::null_mut()`
3333

34-
error: aborting due to 5 previous errors
34+
error: `0 as *const _` detected
35+
--> tests/ui/zero_ptr.rs:24:27
36+
|
37+
LL | let _: *const usize = 0 as *const _;
38+
| ^^^^^^^^^^^^^ help: try: `std::ptr::null()`
39+
40+
error: aborting due to 6 previous errors
3541

0 commit comments

Comments
 (0)