Rollup merge of #141536 - Urgau:ambi_wide_ptr-cmp-diag, r=fee1-dead
Improve `ambiguous_wide_pointer_comparisons` lint compare diagnostics This PR improves the `ambiguous_wide_pointer_comparisons` lint compare diagnostics: `cmp`/`partial_cmp`, but also the operators `<`/`>`/`>=`/`<=`, by: 1. removing the reference to `std::ptr::addr_eq` which only works for equality 2. and adding an `#[expect]` suggestion for keeping the current behavior Fixes rust-lang/rust#141510
This commit is contained in:
@@ -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,
|
||||
@@ -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"
|
||||
#[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}"#
|
||||
)]
|
||||
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"
|
||||
)]
|
||||
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)]
|
||||
|
||||
@@ -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,
|
||||
@@ -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());
|
||||
@@ -376,9 +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 {
|
||||
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,
|
||||
@@ -387,21 +401,11 @@ fn lint_wide_pointer<'tcx>(
|
||||
left,
|
||||
middle,
|
||||
right,
|
||||
}
|
||||
}),
|
||||
addr_suggestion: if is_eq_ne {
|
||||
AmbiguousWidePointerComparisonsAddrSuggestion::AddrEq {
|
||||
ne,
|
||||
deref_left,
|
||||
deref_right,
|
||||
l_modifiers,
|
||||
r_modifiers,
|
||||
left,
|
||||
middle,
|
||||
right,
|
||||
}
|
||||
} else {
|
||||
AmbiguousWidePointerComparisonsAddrSuggestion::Cast {
|
||||
},
|
||||
}
|
||||
} else {
|
||||
AmbiguousWidePointerComparisons::SpanfulCmp {
|
||||
cast_suggestion: AmbiguousWidePointerComparisonsCastSuggestion {
|
||||
deref_left,
|
||||
deref_right,
|
||||
l_modifiers,
|
||||
@@ -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(),
|
||||
},
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user