Skip to content

Commit 1bbdfde

Browse files
Correctly handle transmute as return value from block and let var: _ = transmute
1 parent 06031c6 commit 1bbdfde

File tree

4 files changed

+62
-13
lines changed

4 files changed

+62
-13
lines changed

clippy_lints/src/transmute/missing_transmute_annotations.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_errors::Applicability;
2-
use rustc_hir::{GenericArg, HirId, Node, Path, TyKind};
2+
use rustc_hir::{GenericArg, HirId, Local, Node, Path, TyKind};
33
use rustc_lint::LateContext;
44
use rustc_middle::lint::in_external_macro;
55
use rustc_middle::ty::Ty;
@@ -8,6 +8,27 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
88

99
use crate::transmute::MISSING_TRANSMUTE_ANNOTATIONS;
1010

11+
fn get_parent_local_binding_ty<'tcx>(cx: &LateContext<'tcx>, expr_hir_id: HirId) -> Option<Local<'tcx>> {
12+
let mut parent_iter = cx.tcx.hir().parent_iter(expr_hir_id);
13+
if let Some((_, node)) = parent_iter.next() {
14+
match node {
15+
Node::Local(local) => Some(*local),
16+
Node::Block(_) => {
17+
if let Some((parent_hir_id, Node::Expr(expr))) = parent_iter.next()
18+
&& matches!(expr.kind, rustc_hir::ExprKind::Block(_, _))
19+
{
20+
get_parent_local_binding_ty(cx, parent_hir_id)
21+
} else {
22+
None
23+
}
24+
},
25+
_ => None,
26+
}
27+
} else {
28+
None
29+
}
30+
}
31+
1132
pub(super) fn check<'tcx>(
1233
cx: &LateContext<'tcx>,
1334
path: &Path<'tcx>,
@@ -33,12 +54,14 @@ pub(super) fn check<'tcx>(
3354
return false;
3455
}
3556
// If it's being set as a local variable value...
36-
if let Some((_, node)) = cx.tcx.hir().parent_iter(expr_hir_id).next()
37-
&& let Node::Local(local) = node
57+
if let Some(local) = get_parent_local_binding_ty(cx, expr_hir_id)
3858
// ... which does have type annotations.
39-
&& local.ty.is_some()
59+
&& let Some(ty) = local.ty
4060
{
41-
return false;
61+
// If this is a `let x: _ =`, we shouldn't lint.
62+
if !matches!(ty.kind, TyKind::Infer) {
63+
return false;
64+
}
4265
}
4366
span_lint_and_sugg(
4467
cx,

tests/ui/missing_transmute_annotations.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@aux-build:macro_rules.rs
22

33
#![warn(clippy::missing_transmute_annotations)]
4+
#![allow(clippy::let_with_type_underscore)]
45

56
#[macro_use]
67
extern crate macro_rules;
@@ -53,12 +54,18 @@ unsafe fn foo7() -> i32 {
5354
}
5455

5556
fn main() {
57+
let x: _ = unsafe { std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]) };
58+
//~^ ERROR: transmute used without annotations
5659
unsafe {
60+
let x: _ = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
61+
//~^ ERROR: transmute used without annotations
62+
5763
// Should not warn.
5864
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
5965
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
6066
let x: i32 = std::mem::transmute::<[u16; 2], _>([1u16, 2u16]);
6167
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
6268
let x: i32 = std::mem::transmute([1u16, 2u16]);
6369
}
70+
let x: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };
6471
}

tests/ui/missing_transmute_annotations.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//@aux-build:macro_rules.rs
22

33
#![warn(clippy::missing_transmute_annotations)]
4+
#![allow(clippy::let_with_type_underscore)]
45

56
#[macro_use]
67
extern crate macro_rules;
@@ -53,12 +54,18 @@ unsafe fn foo7() -> i32 {
5354
}
5455

5556
fn main() {
57+
let x: _ = unsafe { std::mem::transmute::<_, i32>([1u16, 2u16]) };
58+
//~^ ERROR: transmute used without annotations
5659
unsafe {
60+
let x: _ = std::mem::transmute::<_, i32>([1u16, 2u16]);
61+
//~^ ERROR: transmute used without annotations
62+
5763
// Should not warn.
5864
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
5965
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
6066
let x: i32 = std::mem::transmute::<[u16; 2], _>([1u16, 2u16]);
6167
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
6268
let x: i32 = std::mem::transmute([1u16, 2u16]);
6369
}
70+
let x: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };
6471
}
Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: transmute used without annotations
2-
--> tests/ui/missing_transmute_annotations.rs:19:15
2+
--> tests/ui/missing_transmute_annotations.rs:20:15
33
|
44
LL | std::mem::transmute([1u16, 2u16])
55
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
@@ -8,37 +8,37 @@ LL | std::mem::transmute([1u16, 2u16])
88
= help: to override `-D warnings` add `#[allow(clippy::missing_transmute_annotations)]`
99

1010
error: transmute used without annotations
11-
--> tests/ui/missing_transmute_annotations.rs:24:15
11+
--> tests/ui/missing_transmute_annotations.rs:25:15
1212
|
1313
LL | std::mem::transmute::<_, _>([1u16, 2u16])
1414
| ^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
1515

1616
error: transmute used without annotations
17-
--> tests/ui/missing_transmute_annotations.rs:29:15
17+
--> tests/ui/missing_transmute_annotations.rs:30:15
1818
|
1919
LL | std::mem::transmute::<_, i32>([1u16, 2u16])
2020
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
2121

2222
error: transmute used without annotations
23-
--> tests/ui/missing_transmute_annotations.rs:34:15
23+
--> tests/ui/missing_transmute_annotations.rs:35:15
2424
|
2525
LL | std::mem::transmute::<[u16; 2], _>([1u16, 2u16])
2626
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
2727

2828
error: transmute used without annotations
29-
--> tests/ui/missing_transmute_annotations.rs:39:32
29+
--> tests/ui/missing_transmute_annotations.rs:40:32
3030
|
3131
LL | let x: i32 = bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]));
3232
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
3333

3434
error: transmute used without annotations
35-
--> tests/ui/missing_transmute_annotations.rs:41:19
35+
--> tests/ui/missing_transmute_annotations.rs:42:19
3636
|
3737
LL | bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]))
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
3939

4040
error: transmute used without annotations
41-
--> tests/ui/missing_transmute_annotations.rs:10:19
41+
--> tests/ui/missing_transmute_annotations.rs:11:19
4242
|
4343
LL | std::mem::transmute($e)
4444
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
@@ -48,5 +48,17 @@ LL | local_bad_transmute!([1u16, 2u16])
4848
|
4949
= note: this error originates in the macro `local_bad_transmute` (in Nightly builds, run with -Z macro-backtrace for more info)
5050

51-
error: aborting due to 7 previous errors
51+
error: transmute used without annotations
52+
--> tests/ui/missing_transmute_annotations.rs:57:35
53+
|
54+
LL | let x: _ = unsafe { std::mem::transmute::<_, i32>([1u16, 2u16]) };
55+
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
56+
57+
error: transmute used without annotations
58+
--> tests/ui/missing_transmute_annotations.rs:60:30
59+
|
60+
LL | let x: _ = std::mem::transmute::<_, i32>([1u16, 2u16]);
61+
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
62+
63+
error: aborting due to 9 previous errors
5264

0 commit comments

Comments
 (0)