Skip to content

Improve ambiguous_wide_pointer_comparisons lint compare diagnostics #141536

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ lint_ambiguous_negative_literals = `-` has lower precedence than method calls, w
lint_ambiguous_wide_pointer_comparisons = ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
.addr_metadata_suggestion = use explicit `std::ptr::eq` method to compare metadata and addresses
.addr_suggestion = use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
.cast_suggestion = use untyped pointers to only compare their addresses
.expect_suggestion = or expect the lint to compare the pointers metadata and addresses

lint_associated_const_elided_lifetime = {$elided ->
[true] `&` without an explicit lifetime name cannot be used here
Expand Down
110 changes: 68 additions & 42 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1782,13 +1782,20 @@ pub(crate) enum InvalidNanComparisonsSuggestion {
#[derive(LintDiagnostic)]
pub(crate) enum AmbiguousWidePointerComparisons<'a> {
#[diag(lint_ambiguous_wide_pointer_comparisons)]
Spanful {
SpanfulEq {
#[subdiagnostic]
addr_suggestion: AmbiguousWidePointerComparisonsAddrSuggestion<'a>,
#[subdiagnostic]
addr_metadata_suggestion: Option<AmbiguousWidePointerComparisonsAddrMetadataSuggestion<'a>>,
},
#[diag(lint_ambiguous_wide_pointer_comparisons)]
SpanfulCmp {
#[subdiagnostic]
cast_suggestion: AmbiguousWidePointerComparisonsCastSuggestion<'a>,
#[subdiagnostic]
expect_suggestion: AmbiguousWidePointerComparisonsExpectSuggestion<'a>,
},
#[diag(lint_ambiguous_wide_pointer_comparisons)]
#[help(lint_addr_metadata_suggestion)]
#[help(lint_addr_suggestion)]
Spanless,
Expand Down Expand Up @@ -1816,48 +1823,67 @@ pub(crate) struct AmbiguousWidePointerComparisonsAddrMetadataSuggestion<'a> {
}

#[derive(Subdiagnostic)]
pub(crate) enum AmbiguousWidePointerComparisonsAddrSuggestion<'a> {
#[multipart_suggestion(
lint_addr_suggestion,
style = "verbose",
// FIXME(#53934): make machine-applicable again
applicability = "maybe-incorrect"
)]
AddrEq {
ne: &'a str,
deref_left: &'a str,
deref_right: &'a str,
l_modifiers: &'a str,
r_modifiers: &'a str,
#[suggestion_part(code = "{ne}std::ptr::addr_eq({deref_left}")]
left: Span,
#[suggestion_part(code = "{l_modifiers}, {deref_right}")]
middle: Span,
#[suggestion_part(code = "{r_modifiers})")]
right: Span,
},
#[multipart_suggestion(
lint_addr_suggestion,
style = "verbose",
// FIXME(#53934): make machine-applicable again
applicability = "maybe-incorrect"
#[multipart_suggestion(
lint_addr_suggestion,
style = "verbose",
// FIXME(#53934): make machine-applicable again
applicability = "maybe-incorrect"
)]
pub(crate) struct AmbiguousWidePointerComparisonsAddrSuggestion<'a> {
pub(crate) ne: &'a str,
pub(crate) deref_left: &'a str,
pub(crate) deref_right: &'a str,
pub(crate) l_modifiers: &'a str,
pub(crate) r_modifiers: &'a str,
#[suggestion_part(code = "{ne}std::ptr::addr_eq({deref_left}")]
pub(crate) left: Span,
#[suggestion_part(code = "{l_modifiers}, {deref_right}")]
pub(crate) middle: Span,
#[suggestion_part(code = "{r_modifiers})")]
pub(crate) right: Span,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
lint_cast_suggestion,
style = "verbose",
// FIXME(#53934): make machine-applicable again
applicability = "maybe-incorrect"
)]
pub(crate) struct AmbiguousWidePointerComparisonsCastSuggestion<'a> {
pub(crate) deref_left: &'a str,
pub(crate) deref_right: &'a str,
pub(crate) paren_left: &'a str,
pub(crate) paren_right: &'a str,
pub(crate) l_modifiers: &'a str,
pub(crate) r_modifiers: &'a str,
#[suggestion_part(code = "({deref_left}")]
pub(crate) left_before: Option<Span>,
#[suggestion_part(code = "{l_modifiers}{paren_left}.cast::<()>()")]
pub(crate) left_after: Span,
#[suggestion_part(code = "({deref_right}")]
pub(crate) right_before: Option<Span>,
#[suggestion_part(code = "{r_modifiers}{paren_right}.cast::<()>()")]
pub(crate) right_after: Span,
}

#[derive(Subdiagnostic)]
#[multipart_suggestion(
lint_expect_suggestion,
style = "verbose",
// FIXME(#53934): make machine-applicable again
applicability = "maybe-incorrect"
)]
pub(crate) struct AmbiguousWidePointerComparisonsExpectSuggestion<'a> {
pub(crate) paren_left: &'a str,
pub(crate) paren_right: &'a str,
// FIXME(#127436): Adjust once resolved
#[suggestion_part(
code = r#"{{ #[expect(ambiguous_wide_pointer_comparisons, reason = "...")] {paren_left}"#
)]
Cast {
deref_left: &'a str,
deref_right: &'a str,
paren_left: &'a str,
paren_right: &'a str,
l_modifiers: &'a str,
r_modifiers: &'a str,
#[suggestion_part(code = "({deref_left}")]
left_before: Option<Span>,
#[suggestion_part(code = "{l_modifiers}{paren_left}.cast::<()>()")]
left_after: Span,
#[suggestion_part(code = "({deref_right}")]
right_before: Option<Span>,
#[suggestion_part(code = "{r_modifiers}{paren_right}.cast::<()>()")]
right_after: Span,
},
pub(crate) before: Span,
#[suggestion_part(code = "{paren_right} }}")]
pub(crate) after: Span,
}

#[derive(LintDiagnostic)]
Expand Down
52 changes: 31 additions & 21 deletions compiler/rustc_lint/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ mod improper_ctypes;

use crate::lints::{
AmbiguousWidePointerComparisons, AmbiguousWidePointerComparisonsAddrMetadataSuggestion,
AmbiguousWidePointerComparisonsAddrSuggestion, AtomicOrderingFence, AtomicOrderingLoad,
AmbiguousWidePointerComparisonsAddrSuggestion, AmbiguousWidePointerComparisonsCastSuggestion,
AmbiguousWidePointerComparisonsExpectSuggestion, AtomicOrderingFence, AtomicOrderingLoad,
AtomicOrderingStore, ImproperCTypes, InvalidAtomicOrderingDiag, InvalidNanComparisons,
InvalidNanComparisonsSuggestion, UnpredictableFunctionPointerComparisons,
UnpredictableFunctionPointerComparisonsSuggestion, UnusedComparisons, UsesPowerAlignment,
Expand Down Expand Up @@ -362,6 +363,7 @@ fn lint_wide_pointer<'tcx>(
let ne = if cmpop == ComparisonOp::BinOp(hir::BinOpKind::Ne) { "!" } else { "" };
let is_eq_ne = matches!(cmpop, ComparisonOp::BinOp(hir::BinOpKind::Eq | hir::BinOpKind::Ne));
let is_dyn_comparison = l_inner_ty_is_dyn && r_inner_ty_is_dyn;
let via_method_call = matches!(&e.kind, ExprKind::MethodCall(..) | ExprKind::Call(..));

let left = e.span.shrink_to_lo().until(l_span.shrink_to_lo());
let middle = l_span.shrink_to_hi().until(r_span.shrink_to_lo());
Expand All @@ -376,21 +378,21 @@ fn lint_wide_pointer<'tcx>(
cx.emit_span_lint(
AMBIGUOUS_WIDE_POINTER_COMPARISONS,
e.span,
AmbiguousWidePointerComparisons::Spanful {
addr_metadata_suggestion: (is_eq_ne && !is_dyn_comparison).then(|| {
AmbiguousWidePointerComparisonsAddrMetadataSuggestion {
ne,
deref_left,
deref_right,
l_modifiers,
r_modifiers,
left,
middle,
right,
}
}),
addr_suggestion: if is_eq_ne {
AmbiguousWidePointerComparisonsAddrSuggestion::AddrEq {
if is_eq_ne {
AmbiguousWidePointerComparisons::SpanfulEq {
addr_metadata_suggestion: (!is_dyn_comparison).then(|| {
AmbiguousWidePointerComparisonsAddrMetadataSuggestion {
ne,
deref_left,
deref_right,
l_modifiers,
r_modifiers,
left,
middle,
right,
}
}),
addr_suggestion: AmbiguousWidePointerComparisonsAddrSuggestion {
ne,
deref_left,
deref_right,
Expand All @@ -399,9 +401,11 @@ fn lint_wide_pointer<'tcx>(
left,
middle,
right,
}
} else {
AmbiguousWidePointerComparisonsAddrSuggestion::Cast {
},
}
} else {
AmbiguousWidePointerComparisons::SpanfulCmp {
cast_suggestion: AmbiguousWidePointerComparisonsCastSuggestion {
deref_left,
deref_right,
l_modifiers,
Expand All @@ -412,8 +416,14 @@ fn lint_wide_pointer<'tcx>(
left_after: l_span.shrink_to_hi(),
right_before: (r_ty_refs != 0).then_some(r_span.shrink_to_lo()),
right_after: r_span.shrink_to_hi(),
}
},
},
expect_suggestion: AmbiguousWidePointerComparisonsExpectSuggestion {
paren_left: if via_method_call { "" } else { "(" },
paren_right: if via_method_call { "" } else { ")" },
before: e.span.shrink_to_lo(),
after: e.span.shrink_to_hi(),
},
}
},
);
}
Expand Down
Loading
Loading