Skip to content

Commit 8cfa4fe

Browse files
committed
Add #[rustc_never_returns_null_ptr]
And look for it in the useless_ptr_null_checks lint
1 parent e173a8e commit 8cfa4fe

File tree

4 files changed

+9
-1
lines changed

4 files changed

+9
-1
lines changed

compiler/rustc_feature/src/builtin_attrs.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,10 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
698698
rustc_pass_by_value, Normal, template!(Word), ErrorFollowing,
699699
"#[rustc_pass_by_value] is used to mark types that must be passed by value instead of reference."
700700
),
701+
rustc_attr!(
702+
rustc_never_returns_null_ptr, Normal, template!(Word), ErrorFollowing,
703+
"#[rustc_never_returns_null_ptr] is used to mark functions returning non-null pointers."
704+
),
701705
rustc_attr!(
702706
rustc_coherence_is_core, AttributeType::CrateLevel, template!(Word), ErrorFollowing, @only_local: true,
703707
"#![rustc_coherence_is_core] allows inherent methods on builtin types, only intended to be used in `core`."

compiler/rustc_lint/src/ptr_nulls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn ptr_cast_chain<'a>(cx: &'a LateContext<'_>, mut e: &'a Expr<'a>) -> Option<&'
4949
} else if let ExprKind::Call(path, [arg]) = e.kind
5050
&& let ExprKind::Path(ref qpath) = path.kind
5151
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
52-
&& matches!(cx.tcx.get_diagnostic_name(def_id), Some(sym::ptr_from_ref | sym::ptr_from_mut)) {
52+
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr) {
5353
had_at_least_one_cast = true;
5454
arg
5555
} else if had_at_least_one_cast {

compiler/rustc_passes/src/check_attr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ impl CheckAttrVisitor<'_> {
139139
self.check_rustc_std_internal_symbol(&attr, span, target)
140140
}
141141
sym::naked => self.check_naked(hir_id, attr, span, target),
142+
sym::rustc_never_returns_null_ptr => {
143+
self.check_applied_to_fn_or_method(hir_id, attr, span, target)
144+
}
142145
sym::rustc_legacy_const_generics => {
143146
self.check_rustc_legacy_const_generics(hir_id, &attr, span, target, item)
144147
}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1311,6 +1311,7 @@ symbols! {
13111311
rustc_main,
13121312
rustc_mir,
13131313
rustc_must_implement_one_of,
1314+
rustc_never_returns_null_ptr,
13141315
rustc_nonnull_optimization_guaranteed,
13151316
rustc_nounwind,
13161317
rustc_object_lifetime_default,

0 commit comments

Comments
 (0)