Skip to content

Commit f6df266

Browse files
committed
Auto merge of rust-lang#134353 - oli-obk:safe-target-feature-unsafe-by-default, r=wesleywiser
Treat safe target_feature functions as unsafe by default [less invasive variant] This unblocks * rust-lang#134090 As I stated in rust-lang#134090 (comment) I think the previous impl was too easy to get wrong, as by default it treated safe target feature functions as safe and had to add additional checks for when they weren't. Now the logic is inverted. By default they are unsafe and you have to explicitly handle safe target feature functions. This is the less (imo) invasive variant of rust-lang#134317, as it doesn't require changing the Safety enum, so it only affects FnDefs and nothing else, as it should.
2 parents 1361c1f + 44560cb commit f6df266

8 files changed

+10
-10
lines changed

clippy_lints/src/derive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<'tcx> Visitor<'tcx> for UnsafeVisitor<'_, 'tcx> {
419419
id: LocalDefId,
420420
) -> Self::Result {
421421
if let Some(header) = kind.header()
422-
&& header.safety.is_unsafe()
422+
&& header.is_unsafe()
423423
{
424424
ControlFlow::Break(())
425425
} else {

clippy_lints/src/doc/missing_headers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub fn check(
3232
}
3333

3434
let span = cx.tcx.def_span(owner_id);
35-
match (headers.safety, sig.header.safety) {
35+
match (headers.safety, sig.header.safety()) {
3636
(false, Safety::Unsafe) => span_lint(
3737
cx,
3838
MISSING_SAFETY_DOC,

clippy_lints/src/functions/misnamed_getters.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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.is_unsafe() {
37+
let name = if sig.header.is_unsafe() {
3838
name.strip_suffix("_unchecked").unwrap_or(name)
3939
} else {
4040
name

clippy_lints/src/functions/not_unsafe_ptr_arg_deref.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ pub(super) fn check_fn<'tcx>(
2020
def_id: LocalDefId,
2121
) {
2222
let safety = match kind {
23-
intravisit::FnKind::ItemFn(_, _, hir::FnHeader { safety, .. }) => safety,
24-
intravisit::FnKind::Method(_, sig) => sig.header.safety,
23+
intravisit::FnKind::ItemFn(_, _, header) => header.safety(),
24+
intravisit::FnKind::Method(_, sig) => sig.header.safety(),
2525
intravisit::FnKind::Closure => return,
2626
};
2727

@@ -31,7 +31,7 @@ pub(super) fn check_fn<'tcx>(
3131
pub(super) fn check_trait_item<'tcx>(cx: &LateContext<'tcx>, item: &'tcx hir::TraitItem<'_>) {
3232
if let hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(eid)) = item.kind {
3333
let body = cx.tcx.hir().body(eid);
34-
check_raw_ptr(cx, sig.header.safety, sig.decl, body, item.owner_id.def_id);
34+
check_raw_ptr(cx, sig.header.safety(), sig.decl, body, item.owner_id.def_id);
3535
}
3636
}
3737

clippy_lints/src/inherent_to_string.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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.is_safe()
98+
&& header.is_safe()
9999
&& header.abi == Abi::Rust
100100
&& impl_item.ident.name == sym::to_string
101101
&& let decl = signature.decl

clippy_lints/src/methods/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5309,7 +5309,7 @@ fn lint_binary_expr_with_method_call(cx: &LateContext<'_>, info: &mut BinaryExpr
53095309
}
53105310

53115311
const FN_HEADER: hir::FnHeader = hir::FnHeader {
5312-
safety: hir::Safety::Safe,
5312+
safety: hir::HeaderSafety::Normal(hir::Safety::Safe),
53135313
constness: hir::Constness::NotConst,
53145314
asyncness: hir::IsAsync::NotAsync,
53155315
abi: rustc_target::spec::abi::Abi::Rust,

clippy_lints/src/new_without_default.rs

+1-1
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.is_unsafe() {
78+
if sig.header.is_unsafe() {
7979
// can't be implemented for unsafe new
8080
return;
8181
}

clippy_lints/src/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -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.is_unsafe() || contains_unsafe_block(cx, body.value))
544+
&& body.is_none_or(|body| sig.header.is_unsafe() || contains_unsafe_block(cx, body.value))
545545
{
546546
span_lint_and_then(
547547
cx,

0 commit comments

Comments
 (0)