Skip to content

Commit 3cd6ca0

Browse files
committed
Auto merge of #6716 - magurotuna:refactor-transmute-mod, r=flip1995
Refactor: arrange transmute lints This PR arranges `transmute` lints so that they can be accessed more easily. Basically, I followed the instruction described in #6680 as to how to do the refactoring. - `declare_clippy_lint!` and `impl LintPass` is placed in `transmute/mod.rs` - Uitlity functions is placed in `transmute/utils.rs` - Each lint function about `transmute` is moved into its own module, like `transmute/useless_transmute.rs` For ease of review, I refactored step by step, keeping each commit small. For instance, all I did in 2451781 was to move `useless_transmute` into its own module. --- changelog: Refactor `transmute.rs` file into `transmute` module.
2 parents e50afa4 + bf00098 commit 3cd6ca0

15 files changed

+1062
-763
lines changed

clippy_lints/src/transmute.rs

-763
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
use super::CROSSPOINTER_TRANSMUTE;
2+
use crate::utils::span_lint;
3+
use rustc_hir::Expr;
4+
use rustc_lint::LateContext;
5+
use rustc_middle::ty::{self, Ty};
6+
7+
/// Checks for `crosspointer_transmute` lint.
8+
/// Returns `true` if it's triggered, otherwise returns `false`.
9+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
10+
match (&from_ty.kind(), &to_ty.kind()) {
11+
(ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => {
12+
span_lint(
13+
cx,
14+
CROSSPOINTER_TRANSMUTE,
15+
e.span,
16+
&format!(
17+
"transmute from a type (`{}`) to the type that it points to (`{}`)",
18+
from_ty, to_ty
19+
),
20+
);
21+
true
22+
},
23+
(_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => {
24+
span_lint(
25+
cx,
26+
CROSSPOINTER_TRANSMUTE,
27+
e.span,
28+
&format!(
29+
"transmute from a type (`{}`) to a pointer to that type (`{}`)",
30+
from_ty, to_ty
31+
),
32+
);
33+
true
34+
},
35+
_ => false,
36+
}
37+
}

0 commit comments

Comments
 (0)