Skip to content

Commit ffa1279

Browse files
Correctly handle transmute as return value from block and let var: _ = transmute
1 parent 8e04961 commit ffa1279

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
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;
@@ -68,12 +69,18 @@ unsafe fn foo9() -> i32 {
6869
}
6970

7071
fn main() {
72+
let x: _ = unsafe { std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]) };
73+
//~^ ERROR: transmute used without annotations
7174
unsafe {
75+
let x: _ = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
76+
//~^ ERROR: transmute used without annotations
77+
7278
// Should not warn.
7379
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
7480
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
7581
let x: i32 = std::mem::transmute::<[u16; 2], _>([1u16, 2u16]);
7682
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
7783
let x: i32 = std::mem::transmute([1u16, 2u16]);
7884
}
85+
let x: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };
7986
}

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;
@@ -68,12 +69,18 @@ unsafe fn foo9() -> i32 {
6869
}
6970

7071
fn main() {
72+
let x: _ = unsafe { std::mem::transmute::<_, i32>([1u16, 2u16]) };
73+
//~^ ERROR: transmute used without annotations
7174
unsafe {
75+
let x: _ = std::mem::transmute::<_, i32>([1u16, 2u16]);
76+
//~^ ERROR: transmute used without annotations
77+
7278
// Should not warn.
7379
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
7480
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
7581
let x: i32 = std::mem::transmute::<[u16; 2], _>([1u16, 2u16]);
7682
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
7783
let x: i32 = std::mem::transmute([1u16, 2u16]);
7884
}
85+
let x: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };
7986
}
Lines changed: 22 additions & 10 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>`
@@ -49,16 +49,28 @@ LL | local_bad_transmute!([1u16, 2u16])
4949
= note: this error originates in the macro `local_bad_transmute` (in Nightly builds, run with -Z macro-backtrace for more info)
5050

5151
error: transmute used without annotations
52-
--> tests/ui/missing_transmute_annotations.rs:61:15
52+
--> tests/ui/missing_transmute_annotations.rs:62:15
5353
|
5454
LL | std::mem::transmute(0i32)
5555
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<i32, Foo>`
5656

5757
error: transmute used without annotations
58-
--> tests/ui/missing_transmute_annotations.rs:66:15
58+
--> tests/ui/missing_transmute_annotations.rs:67:15
5959
|
6060
LL | std::mem::transmute(Foo::A)
6161
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<Foo, i32>`
6262

63-
error: aborting due to 9 previous errors
63+
error: transmute used without annotations
64+
--> tests/ui/missing_transmute_annotations.rs:72:35
65+
|
66+
LL | let x: _ = unsafe { std::mem::transmute::<_, i32>([1u16, 2u16]) };
67+
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
68+
69+
error: transmute used without annotations
70+
--> tests/ui/missing_transmute_annotations.rs:75:30
71+
|
72+
LL | let x: _ = std::mem::transmute::<_, i32>([1u16, 2u16]);
73+
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
74+
75+
error: aborting due to 11 previous errors
6476

0 commit comments

Comments
 (0)