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
This commit is contained in:
commit
8633bcbc3c
8 changed files with 30 additions and 5 deletions
|
|
@ -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`
|
||||
|
|
|
|||
|
|
@ -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.
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue