Skip to content

Commit 6a92b85

Browse files
authored
Rollup merge of rust-lang#134285 - oli-obk:push-vwrqsqlwnuxo, r=Urgau
Add some convenience helper methods on `hir::Safety` Makes a lot of call sites simpler and should make any refactorings needed for rust-lang#134090 (comment) simpler, as fewer sites have to be touched in case we end up storing some information in the variants of `hir::Safety`
2 parents 4cddd0e + acf9177 commit 6a92b85

12 files changed

+25
-25
lines changed

clippy_lints/src/derive.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_errors::Applicability;
77
use rustc_hir::def_id::DefId;
88
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr, walk_fn, walk_item};
99
use rustc_hir::{
10-
self as hir, BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, Impl, Item, ItemKind, Safety, UnsafeSource,
10+
self as hir, BlockCheckMode, BodyId, Expr, ExprKind, FnDecl, Impl, Item, ItemKind, UnsafeSource,
1111
};
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_middle::hir::nested_filter;
@@ -421,7 +421,7 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
421421
id: LocalDefId,
422422
) -> Self::Result {
423423
if let Some(header) = kind.header()
424-
&& header.safety == Safety::Unsafe
424+
&& header.safety.is_unsafe()
425425
{
426426
ControlFlow::Break(())
427427
} else {

clippy_lints/src/eta_reduction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ fn check_inputs(
281281
}
282282

283283
fn check_sig<'tcx>(closure_sig: FnSig<'tcx>, call_sig: FnSig<'tcx>) -> bool {
284-
call_sig.safety == Safety::Safe && !has_late_bound_to_non_late_bound_regions(closure_sig, call_sig)
284+
call_sig.safety.is_safe() && !has_late_bound_to_non_late_bound_regions(closure_sig, call_sig)
285285
}
286286

287287
/// This walks through both signatures and checks for any time a late-bound region is expected by an

clippy_lints/src/functions/misnamed_getters.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
33
use rustc_errors::Applicability;
44
use rustc_hir::intravisit::FnKind;
5-
use rustc_hir::{Body, ExprKind, FnDecl, ImplicitSelfKind, Safety};
5+
use rustc_hir::{Body, ExprKind, FnDecl, ImplicitSelfKind};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty;
88
use rustc_span::Span;
@@ -34,7 +34,7 @@ pub fn check_fn(cx: &LateContext<'_>, kind: FnKind<'_>, decl: &FnDecl<'_>, body:
3434
ImplicitSelfKind::None => return,
3535
};
3636

37-
let name = if sig.header.safety == Safety::Unsafe {
37+
let name = if sig.header.safety.is_unsafe() {
3838
name.strip_suffix("_unchecked").unwrap_or(name)
3939
} else {
4040
name

clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn check_raw_ptr<'tcx>(
4242
body: &'tcx hir::Body<'tcx>,
4343
def_id: LocalDefId,
4444
) {
45-
if safety == hir::Safety::Safe && cx.effective_visibilities.is_exported(def_id) {
45+
if safety.is_safe() && cx.effective_visibilities.is_exported(def_id) {
4646
let raw_ptrs = iter_input_pats(decl, body)
4747
.filter_map(|arg| raw_ptr_arg(cx, arg))
4848
.collect::<HirIdSet>();
@@ -58,7 +58,7 @@ fn check_raw_ptr<'tcx>(
5858
},
5959
hir::ExprKind::MethodCall(_, recv, args, _) => {
6060
let def_id = typeck.type_dependent_def_id(e.hir_id).unwrap();
61-
if cx.tcx.fn_sig(def_id).skip_binder().skip_binder().safety == hir::Safety::Unsafe {
61+
if cx.tcx.fn_sig(def_id).skip_binder().skip_binder().safety.is_unsafe() {
6262
check_arg(cx, &raw_ptrs, recv);
6363
for arg in args {
6464
check_arg(cx, &raw_ptrs, arg);

clippy_lints/src/inherent_to_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::{implements_trait, is_type_lang_item};
33
use clippy_utils::{return_ty, trait_ref_of_method};
4-
use rustc_hir::{GenericParamKind, ImplItem, ImplItemKind, LangItem, Safety};
4+
use rustc_hir::{GenericParamKind, ImplItem, ImplItemKind, LangItem};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
77
use rustc_span::sym;
@@ -95,7 +95,7 @@ impl<'tcx> LateLintPass<'tcx> for InherentToString {
9595
if let ImplItemKind::Fn(ref signature, _) = impl_item.kind
9696
// #11201
9797
&& let header = signature.header
98-
&& header.safety == Safety::Safe
98+
&& header.safety.is_safe()
9999
&& header.abi == Abi::Rust
100100
&& impl_item.ident.name == sym::to_string
101101
&& let decl = signature.decl

clippy_lints/src/multiple_unsafe_ops_per_block.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::visitors::{Descend, Visitable, for_each_expr};
33
use core::ops::ControlFlow::Continue;
44
use hir::def::{DefKind, Res};
5-
use hir::{BlockCheckMode, ExprKind, QPath, Safety, UnOp};
5+
use hir::{BlockCheckMode, ExprKind, QPath, UnOp};
66
use rustc_ast::Mutability;
77
use rustc_hir as hir;
88
use rustc_lint::{LateContext, LateLintPass};
@@ -133,7 +133,7 @@ fn collect_unsafe_exprs<'tcx>(
133133
ty::FnPtr(sig_tys, hdr) => sig_tys.with(hdr),
134134
_ => return Continue(Descend::Yes),
135135
};
136-
if sig.safety() == Safety::Unsafe {
136+
if sig.safety().is_unsafe() {
137137
unsafe_ops.push(("unsafe function call occurs here", expr.span));
138138
}
139139
},
@@ -144,7 +144,7 @@ fn collect_unsafe_exprs<'tcx>(
144144
.type_dependent_def_id(expr.hir_id)
145145
.map(|def_id| cx.tcx.fn_sig(def_id))
146146
{
147-
if sig.skip_binder().safety() == Safety::Unsafe {
147+
if sig.skip_binder().safety().is_unsafe() {
148148
unsafe_ops.push(("unsafe method call occurs here", expr.span));
149149
}
150150
}

clippy_lints/src/new_without_default.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ impl<'tcx> LateLintPass<'tcx> for NewWithoutDefault {
7575
if let hir::ImplItemKind::Fn(ref sig, _) = impl_item.kind {
7676
let name = impl_item.ident.name;
7777
let id = impl_item.owner_id;
78-
if sig.header.safety == hir::Safety::Unsafe {
78+
if sig.header.safety.is_unsafe() {
7979
// can't be implemented for unsafe new
8080
return;
8181
}

clippy_lints/src/ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir::hir_id::{HirId, HirIdMap};
88
use rustc_hir::intravisit::{Visitor, walk_expr};
99
use rustc_hir::{
1010
self as hir, AnonConst, BinOpKind, BindingMode, Body, Expr, ExprKind, FnRetTy, FnSig, GenericArg, ImplItemKind,
11-
ItemKind, Lifetime, Mutability, Node, Param, PatKind, QPath, Safety, TraitFn, TraitItem, TraitItemKind, TyKind,
11+
ItemKind, Lifetime, Mutability, Node, Param, PatKind, QPath, TraitFn, TraitItem, TraitItemKind, TyKind,
1212
};
1313
use rustc_infer::infer::TyCtxtInferExt;
1414
use rustc_infer::traits::{Obligation, ObligationCause};
@@ -541,7 +541,7 @@ fn check_mut_from_ref<'tcx>(cx: &LateContext<'tcx>, sig: &FnSig<'_>, body: Optio
541541
.collect();
542542
if let Some(args) = args
543543
&& !args.is_empty()
544-
&& body.is_none_or(|body| sig.header.safety == Safety::Unsafe || contains_unsafe_block(cx, body.value))
544+
&& body.is_none_or(|body| sig.header.safety.is_unsafe() || contains_unsafe_block(cx, body.value))
545545
{
546546
span_lint_and_then(
547547
cx,

clippy_lints/src/undocumented_unsafe_blocks.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
203203
let item_has_safety_comment = item_has_safety_comment(cx, item);
204204
match (&item.kind, item_has_safety_comment) {
205205
// lint unsafe impl without safety comment
206-
(ItemKind::Impl(impl_), HasSafetyComment::No) if impl_.safety == hir::Safety::Unsafe => {
206+
(ItemKind::Impl(impl_), HasSafetyComment::No) if impl_.safety.is_unsafe() => {
207207
if !is_lint_allowed(cx, UNDOCUMENTED_UNSAFE_BLOCKS, item.hir_id())
208208
&& !is_unsafe_from_proc_macro(cx, item.span)
209209
{
@@ -227,7 +227,7 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
227227
}
228228
},
229229
// lint safe impl with unnecessary safety comment
230-
(ItemKind::Impl(impl_), HasSafetyComment::Yes(pos)) if impl_.safety == hir::Safety::Safe => {
230+
(ItemKind::Impl(impl_), HasSafetyComment::Yes(pos)) if impl_.safety.is_safe() => {
231231
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, item.hir_id()) {
232232
let (span, help_span) = mk_spans(pos);
233233

clippy_utils/src/check_proc_macro.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ fn ty_search_pat(ty: &Ty<'_>) -> (Pat, Pat) {
373373
TyKind::Ptr(MutTy { ty, .. }) => (Pat::Str("*"), ty_search_pat(ty).1),
374374
TyKind::Ref(_, MutTy { ty, .. }) => (Pat::Str("&"), ty_search_pat(ty).1),
375375
TyKind::BareFn(bare_fn) => (
376-
if bare_fn.safety == Safety::Unsafe {
376+
if bare_fn.safety.is_unsafe() {
377377
Pat::Str("unsafe")
378378
} else if bare_fn.abi != Abi::Rust {
379379
Pat::Str("extern")

clippy_utils/src/ty.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
1111
use rustc_hir::def_id::DefId;
12-
use rustc_hir::{Expr, FnDecl, LangItem, Safety, TyKind};
12+
use rustc_hir::{Expr, FnDecl, LangItem, TyKind};
1313
use rustc_infer::infer::TyCtxtInferExt;
1414
use rustc_lint::LateContext;
1515
use rustc_middle::mir::ConstValue;
@@ -531,7 +531,7 @@ pub fn peel_mid_ty_refs_is_mutable(ty: Ty<'_>) -> (Ty<'_>, usize, Mutability) {
531531
/// Returns `true` if the given type is an `unsafe` function.
532532
pub fn type_is_unsafe_function<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
533533
match ty.kind() {
534-
ty::FnDef(..) | ty::FnPtr(..) => ty.fn_sig(cx.tcx).safety() == Safety::Unsafe,
534+
ty::FnDef(..) | ty::FnPtr(..) => ty.fn_sig(cx.tcx).safety().is_unsafe(),
535535
_ => false,
536536
}
537537
}

clippy_utils/src/visitors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_hir::def::{CtorKind, DefKind, Res};
77
use rustc_hir::intravisit::{self, Visitor, walk_block, walk_expr};
88
use rustc_hir::{
99
AnonConst, Arm, Block, BlockCheckMode, Body, BodyId, Expr, ExprKind, HirId, ItemId, ItemKind, LetExpr, Pat, QPath,
10-
Safety, Stmt, UnOp, UnsafeSource, StructTailExpr,
10+
Stmt, UnOp, UnsafeSource, StructTailExpr,
1111
};
1212
use rustc_lint::LateContext;
1313
use rustc_middle::hir::nested_filter;
@@ -426,15 +426,15 @@ pub fn is_expr_unsafe<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool {
426426
.cx
427427
.typeck_results()
428428
.type_dependent_def_id(e.hir_id)
429-
.is_some_and(|id| self.cx.tcx.fn_sig(id).skip_binder().safety() == Safety::Unsafe) =>
429+
.is_some_and(|id| self.cx.tcx.fn_sig(id).skip_binder().safety().is_unsafe()) =>
430430
{
431431
ControlFlow::Break(())
432432
},
433433
ExprKind::Call(func, _) => match *self.cx.typeck_results().expr_ty(func).peel_refs().kind() {
434-
ty::FnDef(id, _) if self.cx.tcx.fn_sig(id).skip_binder().safety() == Safety::Unsafe => {
434+
ty::FnDef(id, _) if self.cx.tcx.fn_sig(id).skip_binder().safety().is_unsafe() => {
435435
ControlFlow::Break(())
436436
},
437-
ty::FnPtr(_, hdr) if hdr.safety == Safety::Unsafe => ControlFlow::Break(()),
437+
ty::FnPtr(_, hdr) if hdr.safety.is_unsafe() => ControlFlow::Break(()),
438438
_ => walk_expr(self, e),
439439
},
440440
ExprKind::Path(ref p)
@@ -458,7 +458,7 @@ pub fn is_expr_unsafe<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool {
458458
}
459459
fn visit_nested_item(&mut self, id: ItemId) -> Self::Result {
460460
if let ItemKind::Impl(i) = &self.cx.tcx.hir().item(id).kind
461-
&& i.safety == Safety::Unsafe
461+
&& i.safety.is_unsafe()
462462
{
463463
ControlFlow::Break(())
464464
} else {

0 commit comments

Comments
 (0)