fix: cmp_null wrongly unmangled macros

This commit is contained in:
Linshu Yang
2025-12-21 22:01:46 +00:00
parent 85998185db
commit a4e8d41802
4 changed files with 51 additions and 4 deletions
+4 -3
View File
@@ -14,13 +14,14 @@ pub(super) fn check<'tcx>(
l: &Expr<'_>,
r: &Expr<'_>,
) -> bool {
let mut applicability = Applicability::MachineApplicable;
let non_null_path_snippet = match (
is_lint_allowed(cx, CMP_NULL, expr.hir_id),
is_null_path(cx, l),
is_null_path(cx, r),
) {
(false, true, false) if let Some(sugg) = Sugg::hir_opt(cx, r) => sugg.maybe_paren(),
(false, false, true) if let Some(sugg) = Sugg::hir_opt(cx, l) => sugg.maybe_paren(),
(false, true, false) => Sugg::hir_with_context(cx, r, expr.span.ctxt(), "..", &mut applicability).maybe_paren(),
(false, false, true) => Sugg::hir_with_context(cx, l, expr.span.ctxt(), "..", &mut applicability).maybe_paren(),
_ => return false,
};
let invert = if op == BinOpKind::Eq { "" } else { "!" };
@@ -32,7 +33,7 @@ pub(super) fn check<'tcx>(
"comparing with null is better expressed by the `.is_null()` method",
"try",
format!("{invert}{non_null_path_snippet}.is_null()",),
Applicability::MachineApplicable,
applicability,
);
true
}
+20
View File
@@ -38,3 +38,23 @@ fn issue15010() {
debug_assert!(!f.is_null());
//~^ cmp_null
}
fn issue16281() {
use std::ptr;
struct Container {
value: *const i32,
}
let x = Container { value: ptr::null() };
macro_rules! dot_value {
($obj:expr) => {
$obj.value
};
}
if dot_value!(x).is_null() {
//~^ cmp_null
todo!()
}
}
+20
View File
@@ -38,3 +38,23 @@ fn issue15010() {
debug_assert!(f != std::ptr::null_mut());
//~^ cmp_null
}
fn issue16281() {
use std::ptr;
struct Container {
value: *const i32,
}
let x = Container { value: ptr::null() };
macro_rules! dot_value {
($obj:expr) => {
$obj.value
};
}
if dot_value!(x) == ptr::null() {
//~^ cmp_null
todo!()
}
}
+7 -1
View File
@@ -37,5 +37,11 @@ error: comparing with null is better expressed by the `.is_null()` method
LL | debug_assert!(f != std::ptr::null_mut());
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `!f.is_null()`
error: aborting due to 6 previous errors
error: comparing with null is better expressed by the `.is_null()` method
--> tests/ui/cmp_null.rs:56:8
|
LL | if dot_value!(x) == ptr::null() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `dot_value!(x).is_null()`
error: aborting due to 7 previous errors