Skip to content

Commit 436f0a5

Browse files
Correctly handle body scope
1 parent 9fbabde commit 436f0a5

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

clippy_lints/src/transmute/missing_transmute_annotations.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use rustc_errors::Applicability;
3-
use rustc_hir::{GenericArg, HirId, ItemKind, Local, Node, Path, TyKind};
3+
use rustc_hir::{GenericArg, HirId, Local, Node, Path, TyKind};
44
use rustc_lint::LateContext;
55
use rustc_middle::lint::in_external_macro;
66
use rustc_middle::ty::Ty;
@@ -29,13 +29,10 @@ fn get_parent_local_binding_ty<'tcx>(cx: &LateContext<'tcx>, expr_hir_id: HirId)
2929
}
3030

3131
fn is_function_block(cx: &LateContext<'_>, expr_hir_id: HirId) -> bool {
32-
for (_, node) in cx.tcx.hir().parent_iter(expr_hir_id) {
33-
if let Node::Item(item) = node
34-
&& let ItemKind::Fn(_, _, body_id) = item.kind
35-
{
36-
let body = cx.tcx.hir().body(body_id);
37-
return body.value.peel_blocks().hir_id == expr_hir_id;
38-
}
32+
let def_id = cx.tcx.hir().enclosing_body_owner(expr_hir_id);
33+
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(def_id) {
34+
let body = cx.tcx.hir().body(body_id);
35+
return body.value.peel_blocks().hir_id == expr_hir_id;
3936
}
4037
false
4138
}

tests/ui/missing_transmute_annotations.fixed

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ unsafe fn foo1() -> i32 {
2222
std::mem::transmute([1u16, 2u16])
2323
}
2424

25+
// Should not warn!
26+
const _: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };
27+
2528
#[repr(i32)]
2629
enum Foo {
2730
A = 0,

tests/ui/missing_transmute_annotations.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ unsafe fn foo1() -> i32 {
2222
std::mem::transmute([1u16, 2u16])
2323
}
2424

25+
// Should not warn!
26+
const _: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };
27+
2528
#[repr(i32)]
2629
enum Foo {
2730
A = 0,

tests/ui/missing_transmute_annotations.stderr

Lines changed: 10 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:32:19
2+
--> tests/ui/missing_transmute_annotations.rs:35:19
33
|
44
LL | i = std::mem::transmute([1u16, 2u16]);
55
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
@@ -8,31 +8,31 @@ LL | i = 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:34:19
11+
--> tests/ui/missing_transmute_annotations.rs:37:19
1212
|
1313
LL | i = 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:36:19
17+
--> tests/ui/missing_transmute_annotations.rs:39:19
1818
|
1919
LL | i = 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:38:19
23+
--> tests/ui/missing_transmute_annotations.rs:41:19
2424
|
2525
LL | i = 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:41:32
29+
--> tests/ui/missing_transmute_annotations.rs:44: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:43:19
35+
--> tests/ui/missing_transmute_annotations.rs:46:19
3636
|
3737
LL | bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]));
3838
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
@@ -49,25 +49,25 @@ LL | i = 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:51:19
52+
--> tests/ui/missing_transmute_annotations.rs:54:19
5353
|
5454
LL | i = std::mem::transmute([0i16, 0i16]);
5555
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[i16; 2], i32>`
5656

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

6363
error: transmute used without annotations
64-
--> tests/ui/missing_transmute_annotations.rs:61:35
64+
--> tests/ui/missing_transmute_annotations.rs:64:35
6565
|
6666
LL | let x: _ = unsafe { std::mem::transmute::<_, i32>([1u16, 2u16]) };
6767
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
6868

6969
error: transmute used without annotations
70-
--> tests/ui/missing_transmute_annotations.rs:64:30
70+
--> tests/ui/missing_transmute_annotations.rs:67:30
7171
|
7272
LL | let x: _ = std::mem::transmute::<_, i32>([1u16, 2u16]);
7373
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`

0 commit comments

Comments
 (0)