mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-12-18 09:03:18 +00:00
Correctly handle transmute
as return value from block
and let var: _ = transmute
This commit is contained in:
parent
8e0496170d
commit
ffa12798c0
4 changed files with 64 additions and 15 deletions
|
@ -1,5 +1,5 @@
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{GenericArg, HirId, Node, Path, TyKind};
|
use rustc_hir::{GenericArg, HirId, Local, Node, Path, TyKind};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
use rustc_middle::ty::Ty;
|
use rustc_middle::ty::Ty;
|
||||||
|
@ -8,6 +8,27 @@ use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
|
|
||||||
use crate::transmute::MISSING_TRANSMUTE_ANNOTATIONS;
|
use crate::transmute::MISSING_TRANSMUTE_ANNOTATIONS;
|
||||||
|
|
||||||
|
fn get_parent_local_binding_ty<'tcx>(cx: &LateContext<'tcx>, expr_hir_id: HirId) -> Option<Local<'tcx>> {
|
||||||
|
let mut parent_iter = cx.tcx.hir().parent_iter(expr_hir_id);
|
||||||
|
if let Some((_, node)) = parent_iter.next() {
|
||||||
|
match node {
|
||||||
|
Node::Local(local) => Some(*local),
|
||||||
|
Node::Block(_) => {
|
||||||
|
if let Some((parent_hir_id, Node::Expr(expr))) = parent_iter.next()
|
||||||
|
&& matches!(expr.kind, rustc_hir::ExprKind::Block(_, _))
|
||||||
|
{
|
||||||
|
get_parent_local_binding_ty(cx, parent_hir_id)
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) fn check<'tcx>(
|
pub(super) fn check<'tcx>(
|
||||||
cx: &LateContext<'tcx>,
|
cx: &LateContext<'tcx>,
|
||||||
path: &Path<'tcx>,
|
path: &Path<'tcx>,
|
||||||
|
@ -33,12 +54,14 @@ pub(super) fn check<'tcx>(
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
// If it's being set as a local variable value...
|
// If it's being set as a local variable value...
|
||||||
if let Some((_, node)) = cx.tcx.hir().parent_iter(expr_hir_id).next()
|
if let Some(local) = get_parent_local_binding_ty(cx, expr_hir_id)
|
||||||
&& let Node::Local(local) = node
|
|
||||||
// ... which does have type annotations.
|
// ... which does have type annotations.
|
||||||
&& local.ty.is_some()
|
&& let Some(ty) = local.ty
|
||||||
{
|
{
|
||||||
return false;
|
// If this is a `let x: _ =`, we shouldn't lint.
|
||||||
|
if !matches!(ty.kind, TyKind::Infer) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
span_lint_and_sugg(
|
span_lint_and_sugg(
|
||||||
cx,
|
cx,
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
//@aux-build:macro_rules.rs
|
//@aux-build:macro_rules.rs
|
||||||
|
|
||||||
#![warn(clippy::missing_transmute_annotations)]
|
#![warn(clippy::missing_transmute_annotations)]
|
||||||
|
#![allow(clippy::let_with_type_underscore)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate macro_rules;
|
extern crate macro_rules;
|
||||||
|
@ -68,7 +69,12 @@ unsafe fn foo9() -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let x: _ = unsafe { std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]) };
|
||||||
|
//~^ ERROR: transmute used without annotations
|
||||||
unsafe {
|
unsafe {
|
||||||
|
let x: _ = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
||||||
|
//~^ ERROR: transmute used without annotations
|
||||||
|
|
||||||
// Should not warn.
|
// Should not warn.
|
||||||
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
||||||
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
||||||
|
@ -76,4 +82,5 @@ fn main() {
|
||||||
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
|
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
|
||||||
let x: i32 = std::mem::transmute([1u16, 2u16]);
|
let x: i32 = std::mem::transmute([1u16, 2u16]);
|
||||||
}
|
}
|
||||||
|
let x: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
//@aux-build:macro_rules.rs
|
//@aux-build:macro_rules.rs
|
||||||
|
|
||||||
#![warn(clippy::missing_transmute_annotations)]
|
#![warn(clippy::missing_transmute_annotations)]
|
||||||
|
#![allow(clippy::let_with_type_underscore)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate macro_rules;
|
extern crate macro_rules;
|
||||||
|
@ -68,7 +69,12 @@ unsafe fn foo9() -> i32 {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
let x: _ = unsafe { std::mem::transmute::<_, i32>([1u16, 2u16]) };
|
||||||
|
//~^ ERROR: transmute used without annotations
|
||||||
unsafe {
|
unsafe {
|
||||||
|
let x: _ = std::mem::transmute::<_, i32>([1u16, 2u16]);
|
||||||
|
//~^ ERROR: transmute used without annotations
|
||||||
|
|
||||||
// Should not warn.
|
// Should not warn.
|
||||||
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
||||||
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
let x = std::mem::transmute::<[u16; 2], i32>([1u16, 2u16]);
|
||||||
|
@ -76,4 +82,5 @@ fn main() {
|
||||||
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
|
let x: i32 = std::mem::transmute::<_, i32>([1u16, 2u16]);
|
||||||
let x: i32 = std::mem::transmute([1u16, 2u16]);
|
let x: i32 = std::mem::transmute([1u16, 2u16]);
|
||||||
}
|
}
|
||||||
|
let x: i32 = unsafe { std::mem::transmute([1u16, 2u16]) };
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
error: transmute used without annotations
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:19:15
|
--> tests/ui/missing_transmute_annotations.rs:20:15
|
||||||
|
|
|
|
||||||
LL | std::mem::transmute([1u16, 2u16])
|
LL | std::mem::transmute([1u16, 2u16])
|
||||||
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
||||||
|
@ -8,37 +8,37 @@ LL | std::mem::transmute([1u16, 2u16])
|
||||||
= help: to override `-D warnings` add `#[allow(clippy::missing_transmute_annotations)]`
|
= help: to override `-D warnings` add `#[allow(clippy::missing_transmute_annotations)]`
|
||||||
|
|
||||||
error: transmute used without annotations
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:24:15
|
--> tests/ui/missing_transmute_annotations.rs:25:15
|
||||||
|
|
|
|
||||||
LL | std::mem::transmute::<_, _>([1u16, 2u16])
|
LL | std::mem::transmute::<_, _>([1u16, 2u16])
|
||||||
| ^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
| ^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
||||||
|
|
||||||
error: transmute used without annotations
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:29:15
|
--> tests/ui/missing_transmute_annotations.rs:30:15
|
||||||
|
|
|
|
||||||
LL | std::mem::transmute::<_, i32>([1u16, 2u16])
|
LL | std::mem::transmute::<_, i32>([1u16, 2u16])
|
||||||
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
||||||
|
|
||||||
error: transmute used without annotations
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:34:15
|
--> tests/ui/missing_transmute_annotations.rs:35:15
|
||||||
|
|
|
|
||||||
LL | std::mem::transmute::<[u16; 2], _>([1u16, 2u16])
|
LL | std::mem::transmute::<[u16; 2], _>([1u16, 2u16])
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
||||||
|
|
||||||
error: transmute used without annotations
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:39:32
|
--> tests/ui/missing_transmute_annotations.rs:40:32
|
||||||
|
|
|
|
||||||
LL | let x: i32 = bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]));
|
LL | let x: i32 = bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]));
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
||||||
|
|
||||||
error: transmute used without annotations
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:41:19
|
--> tests/ui/missing_transmute_annotations.rs:42:19
|
||||||
|
|
|
|
||||||
LL | bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]))
|
LL | bar(std::mem::transmute::<[u16; 2], _>([1u16, 2u16]))
|
||||||
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
||||||
|
|
||||||
error: transmute used without annotations
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:10:19
|
--> tests/ui/missing_transmute_annotations.rs:11:19
|
||||||
|
|
|
|
||||||
LL | std::mem::transmute($e)
|
LL | std::mem::transmute($e)
|
||||||
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
||||||
|
@ -49,16 +49,28 @@ LL | 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)
|
= 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
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:61:15
|
--> tests/ui/missing_transmute_annotations.rs:62:15
|
||||||
|
|
|
|
||||||
LL | std::mem::transmute(0i32)
|
LL | std::mem::transmute(0i32)
|
||||||
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<i32, Foo>`
|
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<i32, Foo>`
|
||||||
|
|
||||||
error: transmute used without annotations
|
error: transmute used without annotations
|
||||||
--> tests/ui/missing_transmute_annotations.rs:66:15
|
--> tests/ui/missing_transmute_annotations.rs:67:15
|
||||||
|
|
|
|
||||||
LL | std::mem::transmute(Foo::A)
|
LL | std::mem::transmute(Foo::A)
|
||||||
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<Foo, i32>`
|
| ^^^^^^^^^ help: consider adding missing annotations: `transmute::<Foo, i32>`
|
||||||
|
|
||||||
error: aborting due to 9 previous errors
|
error: transmute used without annotations
|
||||||
|
--> tests/ui/missing_transmute_annotations.rs:72: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:75:30
|
||||||
|
|
|
||||||
|
LL | let x: _ = std::mem::transmute::<_, i32>([1u16, 2u16]);
|
||||||
|
| ^^^^^^^^^^^^^^^^^^^ help: consider adding missing annotations: `transmute::<[u16; 2], i32>`
|
||||||
|
|
||||||
|
error: aborting due to 11 previous errors
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue