1
Fork 0

Improve diagnostics of the invalid_reference_casting lint

This commit is contained in:
Urgau 2023-07-14 22:25:47 +02:00
parent 50a46710a9
commit 20a6b57106
5 changed files with 151 additions and 76 deletions

View file

@ -745,10 +745,17 @@ pub enum InvalidFromUtf8Diag {
// reference_casting.rs
#[derive(LintDiagnostic)]
#[diag(lint_invalid_reference_casting)]
pub struct InvalidReferenceCastingDiag {
#[label]
pub orig_cast: Option<Span>,
pub enum InvalidReferenceCastingDiag {
#[diag(lint_invalid_reference_casting_borrow_as_mut)]
BorrowAsMut {
#[label]
orig_cast: Option<Span>,
},
#[diag(lint_invalid_reference_casting_assign_to_ref)]
AssignToRef {
#[label]
orig_cast: Option<Span>,
},
}
// hidden_unicode_codepoints.rs

View file

@ -73,13 +73,25 @@ impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
return;
};
if is_cast_from_const_to_mut(cx, e) {
cx.emit_spanned_lint(INVALID_REFERENCE_CASTING, expr.span, InvalidReferenceCastingDiag { orig_cast: None });
let orig_cast = if is_cast_from_const_to_mut(cx, e) {
None
} else if let ExprKind::Path(QPath::Resolved(_, path)) = e.kind
&& let Res::Local(hir_id) = &path.res
&& let Some(orig_cast) = self.casted.get(hir_id) {
cx.emit_spanned_lint(INVALID_REFERENCE_CASTING, expr.span, InvalidReferenceCastingDiag { orig_cast: Some(*orig_cast) });
}
Some(*orig_cast)
} else {
return;
};
cx.emit_spanned_lint(
INVALID_REFERENCE_CASTING,
expr.span,
if matches!(expr.kind, ExprKind::AddrOf(..)) {
InvalidReferenceCastingDiag::BorrowAsMut { orig_cast }
} else {
InvalidReferenceCastingDiag::AssignToRef { orig_cast }
},
);
}
}