Skip to content

Commit 68a3ada

Browse files
committed
other clean-up
1 parent af7fa53 commit 68a3ada

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-18
lines changed

clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::ty;
44
use rustc_span::def_id::LocalDefId;
55

66
use clippy_utils::diagnostics::span_lint;
7-
use clippy_utils::ty::type_is_unsafe_function;
7+
use clippy_utils::ty::is_unsafe_fn;
88
use clippy_utils::visitors::for_each_expr;
99
use clippy_utils::{iter_input_pats, path_to_local};
1010

@@ -51,7 +51,7 @@ fn check_raw_ptr<'tcx>(
5151
let typeck = cx.tcx.typeck_body(body.id());
5252
let _: Option<!> = for_each_expr(cx, body.value, |e| {
5353
match e.kind {
54-
hir::ExprKind::Call(f, args) if type_is_unsafe_function(cx, typeck.expr_ty(f)) => {
54+
hir::ExprKind::Call(f, args) if is_unsafe_fn(cx, typeck.expr_ty(f)) => {
5555
for arg in args {
5656
check_arg(cx, &raw_ptrs, arg);
5757
}

clippy_lints/src/matches/manual_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::map_unit_fn::OPTION_MAP_UNIT_FN;
22
use crate::matches::MATCH_AS_REF;
33
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
44
use clippy_utils::sugg::Sugg;
5-
use clippy_utils::ty::{is_copy, is_type_diagnostic_item, peel_mid_ty_refs_is_mutable, type_is_unsafe_function};
5+
use clippy_utils::ty::{is_copy, is_type_diagnostic_item, is_unsafe_fn, peel_mid_ty_refs_is_mutable};
66
use clippy_utils::{
77
CaptureKind, can_move_expr_to_closure, expr_requires_coercion, is_else_clause, is_lint_allowed, is_res_lang_ctor,
88
path_res, path_to_local_id, peel_blocks, peel_hir_expr_refs, peel_hir_expr_while,
@@ -191,7 +191,7 @@ fn can_pass_as_func<'tcx>(cx: &LateContext<'tcx>, binding: HirId, expr: &'tcx Ex
191191
ExprKind::Call(func, [arg])
192192
if path_to_local_id(arg, binding)
193193
&& cx.typeck_results().expr_adjustments(arg).is_empty()
194-
&& !type_is_unsafe_function(cx, cx.typeck_results().expr_ty(func).peel_refs()) =>
194+
&& !is_unsafe_fn(cx, cx.typeck_results().expr_ty(func).peel_refs()) =>
195195
{
196196
Some(func)
197197
},

clippy_utils/src/ty/mod.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,8 @@ pub use type_certainty::expr_type_is_certain;
4343
/// Lower a [`hir::Ty`] to a [`rustc_middle::ty::Ty`].
4444
pub fn ty_from_hir_ty<'tcx>(cx: &LateContext<'tcx>, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
4545
cx.maybe_typeck_results()
46-
.and_then(|results| {
47-
if results.hir_owner == hir_ty.hir_id.owner {
48-
results.node_type_opt(hir_ty.hir_id)
49-
} else {
50-
None
51-
}
52-
})
46+
.filter(|results| results.hir_owner == hir_ty.hir_id.owner)
47+
.and_then(|results| results.node_type_opt(hir_ty.hir_id))
5348
.unwrap_or_else(|| lower_ty(cx.tcx, hir_ty))
5449
}
5550

@@ -475,6 +470,11 @@ pub fn needs_ordered_drop<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
475470
needs_ordered_drop_inner(cx, ty, &mut FxHashSet::default())
476471
}
477472

473+
/// Returns `true` if `ty` denotes an `unsafe fn`.
474+
pub fn is_unsafe_fn<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
475+
ty.is_fn() && ty.fn_sig(cx.tcx).safety().is_unsafe()
476+
}
477+
478478
/// Peels off all references on the type. Returns the underlying type, the number of references
479479
/// removed, and whether the pointer is ultimately mutable or not.
480480
pub fn peel_mid_ty_refs_is_mutable(ty: Ty<'_>) -> (Ty<'_>, usize, Mutability) {
@@ -488,15 +488,10 @@ pub fn peel_mid_ty_refs_is_mutable(ty: Ty<'_>) -> (Ty<'_>, usize, Mutability) {
488488
f(ty, 0, Mutability::Mut)
489489
}
490490

491-
/// Returns `true` if the given type is an `unsafe` function.
492-
pub fn type_is_unsafe_function<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
493-
ty.is_fn() && ty.fn_sig(cx.tcx).safety().is_unsafe()
494-
}
495-
496491
/// Returns the base type for HIR references and pointers.
497492
pub fn walk_ptrs_hir_ty<'tcx>(ty: &'tcx hir::Ty<'tcx>) -> &'tcx hir::Ty<'tcx> {
498-
match ty.kind {
499-
TyKind::Ptr(ref mut_ty) | TyKind::Ref(_, ref mut_ty) => walk_ptrs_hir_ty(mut_ty.ty),
493+
match &ty.kind {
494+
TyKind::Ptr(mut_ty) | TyKind::Ref(_, mut_ty) => walk_ptrs_hir_ty(mut_ty.ty),
500495
_ => ty,
501496
}
502497
}

0 commit comments

Comments
 (0)