Skip to content

Suggest naming types before using explicit type names #14996

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 33 additions & 7 deletions clippy_lints/src/transmute/missing_transmute_annotations.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_and_then;
use rustc_errors::Applicability;
use rustc_hir::{GenericArg, HirId, LetStmt, Node, Path, TyKind};
use rustc_lint::LateContext;
use rustc_middle::ty::Ty;
use rustc_middle::ty::{self, Ty};

use crate::transmute::MISSING_TRANSMUTE_ANNOTATIONS;

Expand Down Expand Up @@ -68,14 +68,40 @@ pub(super) fn check<'tcx>(
} else if is_function_block(cx, expr_hir_id) {
return false;
}
span_lint_and_sugg(
let span = last.ident.span.with_hi(path.span.hi());
span_lint_and_then(
cx,
MISSING_TRANSMUTE_ANNOTATIONS,
last.ident.span.with_hi(path.span.hi()),
span,
"transmute used without annotations",
"consider adding missing annotations",
format!("{}::<{from_ty}, {to_ty}>", last.ident),
Applicability::MaybeIncorrect,
|diag| {
let from_ty_no_name = ty_cannot_be_named(from_ty);
let to_ty_no_name = ty_cannot_be_named(to_ty);
if from_ty_no_name || to_ty_no_name {
let to_name = match (from_ty_no_name, to_ty_no_name) {
(true, false) => "origin type",
(false, true) => "destination type",
_ => "source and destination types",
};
diag.help(format!(
"consider giving the {to_name} a name, and adding missing type annotations"
));
} else {
diag.span_suggestion(
span,
"consider adding missing annotations",
format!("{}::<{from_ty}, {to_ty}>", last.ident),
Applicability::MaybeIncorrect,
);
}
},
);
true
}

fn ty_cannot_be_named(ty: Ty<'_>) -> bool {
matches!(
ty.kind(),
ty::Alias(ty::AliasTyKind::Opaque | ty::AliasTyKind::Inherent, _)
)
}
29 changes: 29 additions & 0 deletions tests/ui/missing_transmute_annotations_unfixable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//@no-rustfix

fn issue14984() {
async fn e() {}
async fn x() -> u32 {
0
}
async fn y() -> f32 {
0.0
};
let mut yy = unsafe { std::ptr::read(&y()) };
yy = unsafe { std::mem::transmute(std::ptr::read(&x())) };
//~^ missing_transmute_annotations

let mut zz = 0u8;
zz = unsafe { std::mem::transmute(std::ptr::read(&x())) };
//~^ missing_transmute_annotations

yy = unsafe { std::mem::transmute(zz) };
//~^ missing_transmute_annotations

fn a() -> impl Sized {
0u32
}

let mut b: f32 = 0.0;
b = unsafe { std::mem::transmute(a()) };
//~^ missing_transmute_annotations
}
36 changes: 36 additions & 0 deletions tests/ui/missing_transmute_annotations_unfixable.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
error: transmute used without annotations
--> tests/ui/missing_transmute_annotations_unfixable.rs:12:29
|
LL | yy = unsafe { std::mem::transmute(std::ptr::read(&x())) };
| ^^^^^^^^^
|
= help: consider giving the source and destination types a name, and adding missing type annotations
= note: `-D clippy::missing-transmute-annotations` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::missing_transmute_annotations)]`

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations_unfixable.rs:16:29
|
LL | zz = unsafe { std::mem::transmute(std::ptr::read(&x())) };
| ^^^^^^^^^
|
= help: consider giving the origin type a name, and adding missing type annotations

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations_unfixable.rs:19:29
|
LL | yy = unsafe { std::mem::transmute(zz) };
| ^^^^^^^^^
|
= help: consider giving the destination type a name, and adding missing type annotations

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations_unfixable.rs:27:28
|
LL | b = unsafe { std::mem::transmute(a()) };
| ^^^^^^^^^
|
= help: consider giving the origin type a name, and adding missing type annotations

error: aborting due to 4 previous errors

Loading