Skip to content

Commit

Permalink
Auto merge of #13454 - samueltardieu:push-ymnkzlqloptz, r=xFrednet
Browse files Browse the repository at this point in the history
Use correct std/core prefix in lint output

changelog: none

I was waiting for #13452 to be merged before sending this one. `std` is used instead of `core` when appropriate in messages.
  • Loading branch information
bors committed Oct 14, 2024
2 parents 43e99e5 + 46a0770 commit f444012
Show file tree
Hide file tree
Showing 6 changed files with 299 additions and 47 deletions.
8 changes: 5 additions & 3 deletions clippy_lints/src/ptr.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
use clippy_utils::source::SpanRangeExt;
use clippy_utils::visitors::contains_unsafe_block;
use clippy_utils::{get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local};
use clippy_utils::{get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local, std_or_core};
use hir::LifetimeName;
use rustc_errors::{Applicability, MultiSpan};
use rustc_hir::hir_id::{HirId, HirIdMap};
Expand Down Expand Up @@ -294,14 +294,16 @@ fn check_invalid_ptr_usage<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
};

for &arg_idx in arg_indices {
if let Some(arg) = args.get(arg_idx).filter(|arg| is_null_path(cx, arg)) {
if let Some(arg) = args.get(arg_idx).filter(|arg| is_null_path(cx, arg))
&& let Some(std_or_core) = std_or_core(cx)
{
span_lint_and_sugg(
cx,
INVALID_NULL_PTR_USAGE,
arg.span,
"pointer must be non-null",
"change this to",
"core::ptr::NonNull::dangling().as_ptr()".to_string(),
format!("{std_or_core}::ptr::NonNull::dangling().as_ptr()"),
Applicability::MachineApplicable,
);
}
Expand Down
44 changes: 22 additions & 22 deletions tests/ui/invalid_null_ptr_usage.fixed
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
fn main() {
unsafe {
let _slice: &[usize] = std::slice::from_raw_parts(core::ptr::NonNull::dangling().as_ptr(), 0);
let _slice: &[usize] = std::slice::from_raw_parts(core::ptr::NonNull::dangling().as_ptr(), 0);
let _slice: &[usize] = std::slice::from_raw_parts(std::ptr::NonNull::dangling().as_ptr(), 0);
let _slice: &[usize] = std::slice::from_raw_parts(std::ptr::NonNull::dangling().as_ptr(), 0);

let _slice: &[usize] = std::slice::from_raw_parts_mut(core::ptr::NonNull::dangling().as_ptr(), 0);
let _slice: &[usize] = std::slice::from_raw_parts_mut(std::ptr::NonNull::dangling().as_ptr(), 0);

std::ptr::copy::<usize>(core::ptr::NonNull::dangling().as_ptr(), std::ptr::NonNull::dangling().as_ptr(), 0);
std::ptr::copy::<usize>(std::ptr::NonNull::dangling().as_ptr(), core::ptr::NonNull::dangling().as_ptr(), 0);
std::ptr::copy::<usize>(std::ptr::NonNull::dangling().as_ptr(), std::ptr::NonNull::dangling().as_ptr(), 0);
std::ptr::copy::<usize>(std::ptr::NonNull::dangling().as_ptr(), std::ptr::NonNull::dangling().as_ptr(), 0);

std::ptr::copy_nonoverlapping::<usize>(core::ptr::NonNull::dangling().as_ptr(), std::ptr::NonNull::dangling().as_ptr(), 0);
std::ptr::copy_nonoverlapping::<usize>(std::ptr::NonNull::dangling().as_ptr(), core::ptr::NonNull::dangling().as_ptr(), 0);
std::ptr::copy_nonoverlapping::<usize>(std::ptr::NonNull::dangling().as_ptr(), std::ptr::NonNull::dangling().as_ptr(), 0);
std::ptr::copy_nonoverlapping::<usize>(std::ptr::NonNull::dangling().as_ptr(), std::ptr::NonNull::dangling().as_ptr(), 0);

struct A; // zero sized struct
assert_eq!(std::mem::size_of::<A>(), 0);

let _a: A = std::ptr::read(core::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read(core::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read(std::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read(std::ptr::NonNull::dangling().as_ptr());

let _a: A = std::ptr::read_unaligned(core::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read_unaligned(core::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read_unaligned(std::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read_unaligned(std::ptr::NonNull::dangling().as_ptr());

let _a: A = std::ptr::read_volatile(core::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read_volatile(core::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read_volatile(std::ptr::NonNull::dangling().as_ptr());
let _a: A = std::ptr::read_volatile(std::ptr::NonNull::dangling().as_ptr());

let _a: A = std::ptr::replace(core::ptr::NonNull::dangling().as_ptr(), A);
let _a: A = std::ptr::replace(std::ptr::NonNull::dangling().as_ptr(), A);
let _slice: *const [usize] = std::ptr::slice_from_raw_parts(std::ptr::null_mut(), 0); // shouldn't lint
let _slice: *const [usize] = std::ptr::slice_from_raw_parts_mut(std::ptr::null_mut(), 0);

std::ptr::swap::<A>(core::ptr::NonNull::dangling().as_ptr(), &mut A);
std::ptr::swap::<A>(&mut A, core::ptr::NonNull::dangling().as_ptr());
std::ptr::swap::<A>(std::ptr::NonNull::dangling().as_ptr(), &mut A);
std::ptr::swap::<A>(&mut A, std::ptr::NonNull::dangling().as_ptr());

std::ptr::swap_nonoverlapping::<A>(core::ptr::NonNull::dangling().as_ptr(), &mut A, 0);
std::ptr::swap_nonoverlapping::<A>(&mut A, core::ptr::NonNull::dangling().as_ptr(), 0);
std::ptr::swap_nonoverlapping::<A>(std::ptr::NonNull::dangling().as_ptr(), &mut A, 0);
std::ptr::swap_nonoverlapping::<A>(&mut A, std::ptr::NonNull::dangling().as_ptr(), 0);

std::ptr::write(core::ptr::NonNull::dangling().as_ptr(), A);
std::ptr::write(std::ptr::NonNull::dangling().as_ptr(), A);

std::ptr::write_unaligned(core::ptr::NonNull::dangling().as_ptr(), A);
std::ptr::write_unaligned(std::ptr::NonNull::dangling().as_ptr(), A);

std::ptr::write_volatile(core::ptr::NonNull::dangling().as_ptr(), A);
std::ptr::write_volatile(std::ptr::NonNull::dangling().as_ptr(), A);

std::ptr::write_bytes::<usize>(core::ptr::NonNull::dangling().as_ptr(), 42, 0);
std::ptr::write_bytes::<usize>(std::ptr::NonNull::dangling().as_ptr(), 42, 0);
}
}
44 changes: 22 additions & 22 deletions tests/ui/invalid_null_ptr_usage.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,135 +2,135 @@ error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:3:59
|
LL | let _slice: &[usize] = std::slice::from_raw_parts(std::ptr::null(), 0);
| ^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`
|
= note: `#[deny(clippy::invalid_null_ptr_usage)]` on by default

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:4:59
|
LL | let _slice: &[usize] = std::slice::from_raw_parts(std::ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:6:63
|
LL | let _slice: &[usize] = std::slice::from_raw_parts_mut(std::ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:8:33
|
LL | std::ptr::copy::<usize>(std::ptr::null(), std::ptr::NonNull::dangling().as_ptr(), 0);
| ^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:9:73
|
LL | std::ptr::copy::<usize>(std::ptr::NonNull::dangling().as_ptr(), std::ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:11:48
|
LL | std::ptr::copy_nonoverlapping::<usize>(std::ptr::null(), std::ptr::NonNull::dangling().as_ptr(), 0);
| ^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:12:88
|
LL | std::ptr::copy_nonoverlapping::<usize>(std::ptr::NonNull::dangling().as_ptr(), std::ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:17:36
|
LL | let _a: A = std::ptr::read(std::ptr::null());
| ^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:18:36
|
LL | let _a: A = std::ptr::read(std::ptr::null_mut());
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:20:46
|
LL | let _a: A = std::ptr::read_unaligned(std::ptr::null());
| ^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:21:46
|
LL | let _a: A = std::ptr::read_unaligned(std::ptr::null_mut());
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:23:45
|
LL | let _a: A = std::ptr::read_volatile(std::ptr::null());
| ^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:24:45
|
LL | let _a: A = std::ptr::read_volatile(std::ptr::null_mut());
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:26:39
|
LL | let _a: A = std::ptr::replace(std::ptr::null_mut(), A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:30:29
|
LL | std::ptr::swap::<A>(std::ptr::null_mut(), &mut A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:31:37
|
LL | std::ptr::swap::<A>(&mut A, std::ptr::null_mut());
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:33:44
|
LL | std::ptr::swap_nonoverlapping::<A>(std::ptr::null_mut(), &mut A, 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:34:52
|
LL | std::ptr::swap_nonoverlapping::<A>(&mut A, std::ptr::null_mut(), 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:36:25
|
LL | std::ptr::write(std::ptr::null_mut(), A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:38:35
|
LL | std::ptr::write_unaligned(std::ptr::null_mut(), A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:40:34
|
LL | std::ptr::write_volatile(std::ptr::null_mut(), A);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: pointer must be non-null
--> tests/ui/invalid_null_ptr_usage.rs:42:40
|
LL | std::ptr::write_bytes::<usize>(std::ptr::null_mut(), 42, 0);
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `core::ptr::NonNull::dangling().as_ptr()`
| ^^^^^^^^^^^^^^^^^^^^ help: change this to: `std::ptr::NonNull::dangling().as_ptr()`

error: aborting due to 22 previous errors

57 changes: 57 additions & 0 deletions tests/ui/invalid_null_ptr_usage_no_std.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#![no_std]
#![feature(lang_items)]

use core::panic::PanicInfo;

#[lang = "eh_personality"]
extern "C" fn eh_personality() {}

#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
loop {}
}

fn main() {
unsafe {
let _slice: &[usize] = core::slice::from_raw_parts(core::ptr::NonNull::dangling().as_ptr(), 0);
let _slice: &[usize] = core::slice::from_raw_parts(core::ptr::NonNull::dangling().as_ptr(), 0);

let _slice: &[usize] = core::slice::from_raw_parts_mut(core::ptr::NonNull::dangling().as_ptr(), 0);

core::ptr::copy::<usize>(core::ptr::NonNull::dangling().as_ptr(), core::ptr::NonNull::dangling().as_ptr(), 0);
core::ptr::copy::<usize>(core::ptr::NonNull::dangling().as_ptr(), core::ptr::NonNull::dangling().as_ptr(), 0);

core::ptr::copy_nonoverlapping::<usize>(core::ptr::NonNull::dangling().as_ptr(), core::ptr::NonNull::dangling().as_ptr(), 0);
core::ptr::copy_nonoverlapping::<usize>(core::ptr::NonNull::dangling().as_ptr(), core::ptr::NonNull::dangling().as_ptr(), 0);

struct A; // zero sized struct
assert_eq!(core::mem::size_of::<A>(), 0);

let _a: A = core::ptr::read(core::ptr::NonNull::dangling().as_ptr());
let _a: A = core::ptr::read(core::ptr::NonNull::dangling().as_ptr());

let _a: A = core::ptr::read_unaligned(core::ptr::NonNull::dangling().as_ptr());
let _a: A = core::ptr::read_unaligned(core::ptr::NonNull::dangling().as_ptr());

let _a: A = core::ptr::read_volatile(core::ptr::NonNull::dangling().as_ptr());
let _a: A = core::ptr::read_volatile(core::ptr::NonNull::dangling().as_ptr());

let _a: A = core::ptr::replace(core::ptr::NonNull::dangling().as_ptr(), A);
let _slice: *const [usize] = core::ptr::slice_from_raw_parts(core::ptr::null_mut(), 0); // shouldn't lint
let _slice: *const [usize] = core::ptr::slice_from_raw_parts_mut(core::ptr::null_mut(), 0);

core::ptr::swap::<A>(core::ptr::NonNull::dangling().as_ptr(), &mut A);
core::ptr::swap::<A>(&mut A, core::ptr::NonNull::dangling().as_ptr());

core::ptr::swap_nonoverlapping::<A>(core::ptr::NonNull::dangling().as_ptr(), &mut A, 0);
core::ptr::swap_nonoverlapping::<A>(&mut A, core::ptr::NonNull::dangling().as_ptr(), 0);

core::ptr::write(core::ptr::NonNull::dangling().as_ptr(), A);

core::ptr::write_unaligned(core::ptr::NonNull::dangling().as_ptr(), A);

core::ptr::write_volatile(core::ptr::NonNull::dangling().as_ptr(), A);

core::ptr::write_bytes::<usize>(core::ptr::NonNull::dangling().as_ptr(), 42, 0);
}
}
Loading

0 comments on commit f444012

Please sign in to comment.