Skip to content

Commit

Permalink
Auto merge of rust-lang#6716 - magurotuna:refactor-transmute-mod, r=f…
Browse files Browse the repository at this point in the history
…lip1995

Refactor: arrange transmute lints

This PR arranges `transmute` lints so that they can be accessed more easily.
Basically, I followed the instruction described in rust-lang#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.
  • Loading branch information
bors committed Mar 2, 2021
2 parents e50afa4 + bf00098 commit 3cd6ca0
Show file tree
Hide file tree
Showing 15 changed files with 1,062 additions and 763 deletions.
763 changes: 0 additions & 763 deletions clippy_lints/src/transmute.rs

This file was deleted.

37 changes: 37 additions & 0 deletions clippy_lints/src/transmute/crosspointer_transmute.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use super::CROSSPOINTER_TRANSMUTE;
use crate::utils::span_lint;
use rustc_hir::Expr;
use rustc_lint::LateContext;
use rustc_middle::ty::{self, Ty};

/// Checks for `crosspointer_transmute` lint.
/// Returns `true` if it's triggered, otherwise returns `false`.
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, from_ty: Ty<'tcx>, to_ty: Ty<'tcx>) -> bool {
match (&from_ty.kind(), &to_ty.kind()) {
(ty::RawPtr(from_ptr), _) if from_ptr.ty == to_ty => {
span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
e.span,
&format!(
"transmute from a type (`{}`) to the type that it points to (`{}`)",
from_ty, to_ty
),
);
true
},
(_, ty::RawPtr(to_ptr)) if to_ptr.ty == from_ty => {
span_lint(
cx,
CROSSPOINTER_TRANSMUTE,
e.span,
&format!(
"transmute from a type (`{}`) to a pointer to that type (`{}`)",
from_ty, to_ty
),
);
true
},
_ => false,
}
}
Loading

0 comments on commit 3cd6ca0

Please sign in to comment.