Skip to content

Commit a4e8d41

Browse files
committed
fix: cmp_null wrongly unmangled macros
1 parent 8599818 commit a4e8d41

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

clippy_lints/src/ptr/cmp_null.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ pub(super) fn check<'tcx>(
1414
l: &Expr<'_>,
1515
r: &Expr<'_>,
1616
) -> bool {
17+
let mut applicability = Applicability::MachineApplicable;
1718
let non_null_path_snippet = match (
1819
is_lint_allowed(cx, CMP_NULL, expr.hir_id),
1920
is_null_path(cx, l),
2021
is_null_path(cx, r),
2122
) {
22-
(false, true, false) if let Some(sugg) = Sugg::hir_opt(cx, r) => sugg.maybe_paren(),
23-
(false, false, true) if let Some(sugg) = Sugg::hir_opt(cx, l) => sugg.maybe_paren(),
23+
(false, true, false) => Sugg::hir_with_context(cx, r, expr.span.ctxt(), "..", &mut applicability).maybe_paren(),
24+
(false, false, true) => Sugg::hir_with_context(cx, l, expr.span.ctxt(), "..", &mut applicability).maybe_paren(),
2425
_ => return false,
2526
};
2627
let invert = if op == BinOpKind::Eq { "" } else { "!" };
@@ -32,7 +33,7 @@ pub(super) fn check<'tcx>(
3233
"comparing with null is better expressed by the `.is_null()` method",
3334
"try",
3435
format!("{invert}{non_null_path_snippet}.is_null()",),
35-
Applicability::MachineApplicable,
36+
applicability,
3637
);
3738
true
3839
}

tests/ui/cmp_null.fixed

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,23 @@ fn issue15010() {
3838
debug_assert!(!f.is_null());
3939
//~^ cmp_null
4040
}
41+
42+
fn issue16281() {
43+
use std::ptr;
44+
45+
struct Container {
46+
value: *const i32,
47+
}
48+
let x = Container { value: ptr::null() };
49+
50+
macro_rules! dot_value {
51+
($obj:expr) => {
52+
$obj.value
53+
};
54+
}
55+
56+
if dot_value!(x).is_null() {
57+
//~^ cmp_null
58+
todo!()
59+
}
60+
}

tests/ui/cmp_null.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,23 @@ fn issue15010() {
3838
debug_assert!(f != std::ptr::null_mut());
3939
//~^ cmp_null
4040
}
41+
42+
fn issue16281() {
43+
use std::ptr;
44+
45+
struct Container {
46+
value: *const i32,
47+
}
48+
let x = Container { value: ptr::null() };
49+
50+
macro_rules! dot_value {
51+
($obj:expr) => {
52+
$obj.value
53+
};
54+
}
55+
56+
if dot_value!(x) == ptr::null() {
57+
//~^ cmp_null
58+
todo!()
59+
}
60+
}

tests/ui/cmp_null.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,11 @@ error: comparing with null is better expressed by the `.is_null()` method
3737
LL | debug_assert!(f != std::ptr::null_mut());
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!f.is_null()`
3939

40-
error: aborting due to 6 previous errors
40+
error: comparing with null is better expressed by the `.is_null()` method
41+
--> tests/ui/cmp_null.rs:56:8
42+
|
43+
LL | if dot_value!(x) == ptr::null() {
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `dot_value!(x).is_null()`
45+
46+
error: aborting due to 7 previous errors
4147

0 commit comments

Comments
 (0)