Skip to content

Commit 87bd829

Browse files
committed
Suggest naming types before using explicit type names
`missing_transmute_annotations` will suggest naming the origin and destination types if they do not have explicit names already.
1 parent 6bb0c97 commit 87bd829

File tree

3 files changed

+98
-7
lines changed

3 files changed

+98
-7
lines changed

clippy_lints/src/transmute/missing_transmute_annotations.rs

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use rustc_errors::Applicability;
33
use rustc_hir::{GenericArg, HirId, LetStmt, Node, Path, TyKind};
44
use rustc_lint::LateContext;
5-
use rustc_middle::ty::Ty;
5+
use rustc_middle::ty::{self, Ty};
66

77
use crate::transmute::MISSING_TRANSMUTE_ANNOTATIONS;
88

@@ -68,14 +68,40 @@ pub(super) fn check<'tcx>(
6868
} else if is_function_block(cx, expr_hir_id) {
6969
return false;
7070
}
71-
span_lint_and_sugg(
71+
let span = last.ident.span.with_hi(path.span.hi());
72+
span_lint_and_then(
7273
cx,
7374
MISSING_TRANSMUTE_ANNOTATIONS,
74-
last.ident.span.with_hi(path.span.hi()),
75+
span,
7576
"transmute used without annotations",
76-
"consider adding missing annotations",
77-
format!("{}::<{from_ty}, {to_ty}>", last.ident),
78-
Applicability::MaybeIncorrect,
77+
|diag| {
78+
let from_ty_no_name = ty_cannot_be_named(from_ty);
79+
let to_ty_no_name = ty_cannot_be_named(to_ty);
80+
if from_ty_no_name || to_ty_no_name {
81+
let to_name = match (from_ty_no_name, to_ty_no_name) {
82+
(true, false) => "origin type",
83+
(false, true) => "destination type",
84+
_ => "source and destination types",
85+
};
86+
diag.help(format!(
87+
"consider giving the {to_name} a name, and adding missing type annotations"
88+
));
89+
} else {
90+
diag.span_suggestion(
91+
span,
92+
"consider adding missing annotations",
93+
format!("{}::<{from_ty}, {to_ty}>", last.ident),
94+
Applicability::MaybeIncorrect,
95+
);
96+
}
97+
},
7998
);
8099
true
81100
}
101+
102+
fn ty_cannot_be_named(ty: Ty<'_>) -> bool {
103+
matches!(
104+
ty.kind(),
105+
ty::Alias(ty::AliasTyKind::Opaque | ty::AliasTyKind::Inherent, _)
106+
)
107+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@no-rustfix
2+
3+
fn issue14984() {
4+
async fn e() {}
5+
async fn x() -> u32 {
6+
0
7+
}
8+
async fn y() -> f32 {
9+
0.0
10+
};
11+
let mut yy = unsafe { std::ptr::read(&y()) };
12+
yy = unsafe { std::mem::transmute(std::ptr::read(&x())) };
13+
//~^ missing_transmute_annotations
14+
15+
let mut zz = 0u8;
16+
zz = unsafe { std::mem::transmute(std::ptr::read(&x())) };
17+
//~^ missing_transmute_annotations
18+
19+
yy = unsafe { std::mem::transmute(zz) };
20+
//~^ missing_transmute_annotations
21+
22+
fn a() -> impl Sized {
23+
0u32
24+
}
25+
26+
let mut b: f32 = 0.0;
27+
b = unsafe { std::mem::transmute(a()) };
28+
//~^ missing_transmute_annotations
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
error: transmute used without annotations
2+
--> tests/ui/missing_transmute_annotations_unfixable.rs:12:29
3+
|
4+
LL | yy = unsafe { std::mem::transmute(std::ptr::read(&x())) };
5+
| ^^^^^^^^^
6+
|
7+
= help: consider giving the source and destination types a name, and adding missing type annotations
8+
= note: `-D clippy::missing-transmute-annotations` implied by `-D warnings`
9+
= help: to override `-D warnings` add `#[allow(clippy::missing_transmute_annotations)]`
10+
11+
error: transmute used without annotations
12+
--> tests/ui/missing_transmute_annotations_unfixable.rs:16:29
13+
|
14+
LL | zz = unsafe { std::mem::transmute(std::ptr::read(&x())) };
15+
| ^^^^^^^^^
16+
|
17+
= help: consider giving the origin type a name, and adding missing type annotations
18+
19+
error: transmute used without annotations
20+
--> tests/ui/missing_transmute_annotations_unfixable.rs:19:29
21+
|
22+
LL | yy = unsafe { std::mem::transmute(zz) };
23+
| ^^^^^^^^^
24+
|
25+
= help: consider giving the destination type a name, and adding missing type annotations
26+
27+
error: transmute used without annotations
28+
--> tests/ui/missing_transmute_annotations_unfixable.rs:27:28
29+
|
30+
LL | b = unsafe { std::mem::transmute(a()) };
31+
| ^^^^^^^^^
32+
|
33+
= help: consider giving the origin type a name, and adding missing type annotations
34+
35+
error: aborting due to 4 previous errors
36+

0 commit comments

Comments
 (0)