Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6f6d6ea

Browse files
committedJan 31, 2025··
add autofix for cmp_null
1 parent 88a00a8 commit 6f6d6ea

File tree

4 files changed

+65
-6
lines changed

4 files changed

+65
-6
lines changed
 

‎clippy_lints/src/ptr.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
1+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
22
use clippy_utils::source::SpanRangeExt;
33
use clippy_utils::visitors::contains_unsafe_block;
44
use clippy_utils::{get_expr_use_or_unification_node, is_lint_allowed, path_def_id, path_to_local, std_or_core};
@@ -252,11 +252,20 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
252252
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
253253
if let ExprKind::Binary(ref op, l, r) = expr.kind {
254254
if (op.node == BinOpKind::Eq || op.node == BinOpKind::Ne) && (is_null_path(cx, l) || is_null_path(cx, r)) {
255-
span_lint(
255+
span_lint_and_sugg(
256256
cx,
257257
CMP_NULL,
258258
expr.span,
259259
"comparing with null is better expressed by the `.is_null()` method",
260+
"try",
261+
format!(
262+
"{}.is_null()",
263+
if is_null_path(cx, l) { r } else { l }
264+
.span
265+
.get_source_text(cx)
266+
.unwrap()
267+
),
268+
Applicability::MachineApplicable,
260269
);
261270
}
262271
} else {

‎tests/ui/cmp_null.fixed

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![warn(clippy::cmp_null)]
2+
#![allow(unused_mut)]
3+
4+
use std::ptr;
5+
6+
fn main() {
7+
let x = 0;
8+
let p: *const usize = &x;
9+
if p.is_null() {
10+
//~^ ERROR: comparing with null is better expressed by the `.is_null()` method
11+
//~| NOTE: `-D clippy::cmp-null` implied by `-D warnings`
12+
println!("This is surprising!");
13+
}
14+
if p.is_null() {
15+
//~^ ERROR: comparing with null is better expressed by the `.is_null()` method
16+
println!("This is surprising!");
17+
}
18+
19+
let mut y = 0;
20+
let mut m: *mut usize = &mut y;
21+
if m.is_null() {
22+
//~^ ERROR: comparing with null is better expressed by the `.is_null()` method
23+
println!("This is surprising, too!");
24+
}
25+
if m.is_null() {
26+
//~^ ERROR: comparing with null is better expressed by the `.is_null()` method
27+
println!("This is surprising, too!");
28+
}
29+
}

‎tests/ui/cmp_null.rs

+9
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@ fn main() {
1111
//~| NOTE: `-D clippy::cmp-null` implied by `-D warnings`
1212
println!("This is surprising!");
1313
}
14+
if ptr::null() == p {
15+
//~^ ERROR: comparing with null is better expressed by the `.is_null()` method
16+
println!("This is surprising!");
17+
}
18+
1419
let mut y = 0;
1520
let mut m: *mut usize = &mut y;
1621
if m == ptr::null_mut() {
1722
//~^ ERROR: comparing with null is better expressed by the `.is_null()` method
1823
println!("This is surprising, too!");
1924
}
25+
if ptr::null_mut() == m {
26+
//~^ ERROR: comparing with null is better expressed by the `.is_null()` method
27+
println!("This is surprising, too!");
28+
}
2029
}

‎tests/ui/cmp_null.stderr

+16-4
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,28 @@ error: comparing with null is better expressed by the `.is_null()` method
22
--> tests/ui/cmp_null.rs:9:8
33
|
44
LL | if p == ptr::null() {
5-
| ^^^^^^^^^^^^^^^^
5+
| ^^^^^^^^^^^^^^^^ help: try: `p.is_null()`
66
|
77
= note: `-D clippy::cmp-null` implied by `-D warnings`
88
= help: to override `-D warnings` add `#[allow(clippy::cmp_null)]`
99

1010
error: comparing with null is better expressed by the `.is_null()` method
11-
--> tests/ui/cmp_null.rs:16:8
11+
--> tests/ui/cmp_null.rs:14:8
12+
|
13+
LL | if ptr::null() == p {
14+
| ^^^^^^^^^^^^^^^^ help: try: `p.is_null()`
15+
16+
error: comparing with null is better expressed by the `.is_null()` method
17+
--> tests/ui/cmp_null.rs:21:8
1218
|
1319
LL | if m == ptr::null_mut() {
14-
| ^^^^^^^^^^^^^^^^^^^^
20+
| ^^^^^^^^^^^^^^^^^^^^ help: try: `m.is_null()`
21+
22+
error: comparing with null is better expressed by the `.is_null()` method
23+
--> tests/ui/cmp_null.rs:25:8
24+
|
25+
LL | if ptr::null_mut() == m {
26+
| ^^^^^^^^^^^^^^^^^^^^ help: try: `m.is_null()`
1527

16-
error: aborting due to 2 previous errors
28+
error: aborting due to 4 previous errors
1729

0 commit comments

Comments
 (0)
Please sign in to comment.