Skip to content

Commit

Permalink
Correctly handle body scope
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Mar 16, 2024
1 parent 9fbabde commit 436f0a5
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
13 changes: 5 additions & 8 deletions clippy_lints/src/transmute/missing_transmute_annotations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use rustc_errors::Applicability;
use rustc_hir::{GenericArg, HirId, ItemKind, Local, Node, Path, TyKind};
use rustc_hir::{GenericArg, HirId, Local, Node, Path, TyKind};
use rustc_lint::LateContext;
use rustc_middle::lint::in_external_macro;
use rustc_middle::ty::Ty;
Expand Down Expand Up @@ -29,13 +29,10 @@ fn get_parent_local_binding_ty<'tcx>(cx: &LateContext<'tcx>, expr_hir_id: HirId)
}

fn is_function_block(cx: &LateContext<'_>, expr_hir_id: HirId) -> bool {
for (_, node) in cx.tcx.hir().parent_iter(expr_hir_id) {
if let Node::Item(item) = node
&& let ItemKind::Fn(_, _, body_id) = item.kind
{
let body = cx.tcx.hir().body(body_id);
return body.value.peel_blocks().hir_id == expr_hir_id;
}
let def_id = cx.tcx.hir().enclosing_body_owner(expr_hir_id);
if let Some(body_id) = cx.tcx.hir().maybe_body_owned_by(def_id) {
let body = cx.tcx.hir().body(body_id);
return body.value.peel_blocks().hir_id == expr_hir_id;
}
false
}
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/missing_transmute_annotations.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ unsafe fn foo1() -> i32 {
std::mem::transmute([1u16, 2u16])
}

// Should not warn!
const _: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };

#[repr(i32)]
enum Foo {
A = 0,
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/missing_transmute_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ unsafe fn foo1() -> i32 {
std::mem::transmute([1u16, 2u16])
}

// Should not warn!
const _: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };

#[repr(i32)]
enum Foo {
A = 0,
Expand Down
20 changes: 10 additions & 10 deletions tests/ui/missing_transmute_annotations.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: transmute used without annotations
--> tests/ui/missing_transmute_annotations.rs:32:19
--> tests/ui/missing_transmute_annotations.rs:35:19
|
LL | i = std::mem::transmute([1u16, 2u16]);
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
Expand All @@ -8,31 +8,31 @@ LL | i = std::mem::transmute([1u16, 2u16]);
= help: to override `-D warnings` add `#[allow(clippy::missing_transmute_annotations)]`

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations.rs:34:19
--> tests/ui/missing_transmute_annotations.rs:37:19
|
LL | i = std::mem::transmute::<_, _>([1u16, 2u16]);
| ^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations.rs:36:19
--> tests/ui/missing_transmute_annotations.rs:39:19
|
LL | i = std::mem::transmute::<_, i32>([1u16, 2u16]);
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations.rs:38:19
--> tests/ui/missing_transmute_annotations.rs:41:19
|
LL | i = std::mem::transmute::<[u16; 2], _>([1u16, 2u16]);
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations.rs:41:32
--> tests/ui/missing_transmute_annotations.rs:44:32
|
LL | let x: i32 = bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]));
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations.rs:43:19
--> tests/ui/missing_transmute_annotations.rs:46:19
|
LL | bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]));
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
Expand All @@ -49,25 +49,25 @@ LL | i = local_bad_transmute!([1u16, 2u16]);
= note: this error originates in the macro `local_bad_transmute` (in Nightly builds, run with -Z macro-backtrace for more info)

error: transmute used without annotations
--> tests/ui/missing_transmute_annotations.rs:51:19
--> tests/ui/missing_transmute_annotations.rs:54:19
|
LL | i = std::mem::transmute([0i16, 0i16]);
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[i16; 2], i32>`

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

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

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

0 comments on commit 436f0a5

Please sign in to comment.