Move a hint to an error message in cast_ref_to_mut lint

This matches mem::transmute::<&T, &mut T> lint in rustc.
This commit is contained in:
Konrad Borowski 2018-12-30 13:11:53 +01:00
parent 1cab4d15a2
commit 6faf1330aa
2 changed files with 7 additions and 14 deletions

View file

@ -16,8 +16,8 @@ use crate::utils::paths;
use crate::utils::{
clip, comparisons, differing_macro_contexts, higher, in_constant, in_macro, int_bits, last_path_segment,
match_def_path, match_path, multispan_sugg, opt_def_id, same_tys, sext, snippet, snippet_opt,
snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then,
span_note_and_lint, unsext, AbsolutePathBuffer,
snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg, span_lint_and_then, unsext,
AbsolutePathBuffer,
};
use if_chain::if_chain;
use rustc::hir;
@ -2291,13 +2291,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RefToMut {
if let TyKind::Ptr(MutTy { mutbl: Mutability::MutImmutable, .. }) = t.node;
if let Ref(..) = cx.tables.node_id_to_type(e.hir_id).sty;
then {
span_note_and_lint(
span_lint(
cx,
CAST_REF_TO_MUT,
expr.span,
"casting immutable reference to a mutable reference",
expr.span,
"consider implementing `UnsafeCell` instead",
"casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell",
);
}
}

View file

@ -1,27 +1,22 @@
error: casting immutable reference to a mutable reference
error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell
--> $DIR/cast_ref_to_mut.rs:18:9
|
LL | (*(a as *const _ as *mut String)).push_str(" world");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::cast-ref-to-mut` implied by `-D warnings`
= note: consider implementing `UnsafeCell` instead
error: casting immutable reference to a mutable reference
error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell
--> $DIR/cast_ref_to_mut.rs:19:9
|
LL | *(a as *const _ as *mut _) = String::from("Replaced");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: consider implementing `UnsafeCell` instead
error: casting immutable reference to a mutable reference
error: casting &T to &mut T may cause undefined behaviour, consider instead using an UnsafeCell
--> $DIR/cast_ref_to_mut.rs:20:9
|
LL | *(a as *const _ as *mut String) += " world";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: consider implementing `UnsafeCell` instead
error: aborting due to 3 previous errors