mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-28 20:16:58 +03:00
fix: cmp_null wrongly unmangled macros
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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!()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user