Skip to content

Commit 9400c23

Browse files
committed
Make NonZero a lang item
1 parent 2783fc4 commit 9400c23

File tree

8 files changed

+16
-14
lines changed

8 files changed

+16
-14
lines changed

compiler/rustc_hir/src/lang_items.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,8 @@ language_item_table! {
426426
String, sym::String, string, Target::Struct, GenericRequirement::None;
427427
CStr, sym::CStr, c_str, Target::Struct, GenericRequirement::None;
428428

429+
NonZero, sym::NonZero, non_zero, Target::Struct, GenericRequirement::Exact(1);
430+
429431
// Experimental lang items for implementing contract pre- and post-condition checking.
430432
ContractBuildCheckEnsures, sym::contract_build_check_ensures, contract_build_check_ensures_fn, Target::Fn, GenericRequirement::None;
431433
ContractCheckRequires, sym::contract_check_requires, contract_check_requires_fn, Target::Fn, GenericRequirement::None;

compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2487,7 +2487,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24872487
_ => return false,
24882488
};
24892489

2490-
if !self.tcx.is_diagnostic_item(sym::NonZero, adt.did()) {
2490+
if !self.tcx.is_lang_item(adt.did(), LangItem::NonZero) {
24912491
return false;
24922492
}
24932493

library/core/src/num/nonzero.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ impl_zeroable_primitive!(
123123
#[stable(feature = "generic_nonzero", since = "1.79.0")]
124124
#[repr(transparent)]
125125
#[rustc_nonnull_optimization_guaranteed]
126-
#[rustc_diagnostic_item = "NonZero"]
126+
#[lang = "NonZero"]
127127
pub struct NonZero<T: ZeroablePrimitive>(T::NonZeroInner);
128128

129129
macro_rules! impl_nonzero_fmt {

src/tools/clippy/clippy_lints/src/methods/useless_nonzero_new_unchecked.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::is_inside_always_const_context;
33
use clippy_utils::msrvs::{self, Msrv};
44
use clippy_utils::source::snippet_with_applicability;
5-
use clippy_utils::ty::is_type_diagnostic_item;
5+
use clippy_utils::ty::is_type_lang_item;
66
use rustc_errors::Applicability;
7-
use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, Node, QPath, UnsafeSource};
7+
use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, LangItem, Node, QPath, UnsafeSource};
88
use rustc_lint::LateContext;
99
use rustc_span::sym;
1010

@@ -15,7 +15,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, func: &Expr<'
1515
&& segment.ident.name == sym::new_unchecked
1616
&& let [init_arg] = args
1717
&& is_inside_always_const_context(cx.tcx, expr.hir_id)
18-
&& is_type_diagnostic_item(cx, cx.typeck_results().node_type(ty.hir_id), sym::NonZero)
18+
&& is_type_lang_item(cx, cx.typeck_results().node_type(ty.hir_id), LangItem::NonZero)
1919
&& msrv.meets(cx, msrvs::CONST_UNWRAP)
2020
{
2121
let mut app = Applicability::MachineApplicable;

src/tools/clippy/clippy_lints/src/non_zero_suggestions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet;
33
use clippy_utils::sym;
44
use rustc_ast::ast::BinOpKind;
55
use rustc_errors::Applicability;
6-
use rustc_hir::{Expr, ExprKind};
6+
use rustc_hir::{Expr, ExprKind, LangItem};
77
use rustc_lint::{LateContext, LateLintPass};
88
use rustc_middle::ty::{self, Ty};
99
use rustc_session::declare_lint_pass;
@@ -81,7 +81,7 @@ fn check_non_zero_conversion(cx: &LateContext<'_>, expr: &Expr<'_>, applicabilit
8181
// Check if the receiver type is a NonZero type
8282
if let ty::Adt(adt_def, _) = receiver_ty.kind()
8383
&& adt_def.is_struct()
84-
&& cx.tcx.get_diagnostic_name(adt_def.did()) == Some(sym::NonZero)
84+
&& cx.tcx.is_lang_item(adt_def.did(), LangItem::NonZero)
8585
&& let Some(target_non_zero_type) = get_target_non_zero_type(target_ty)
8686
{
8787
let arg_snippet = get_arg_snippet(cx, arg, rcv_path);

src/tools/clippy/clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use clippy_utils::diagnostics::span_lint;
55
use clippy_utils::ty::is_type_diagnostic_item;
66
use clippy_utils::{expr_or_init, is_from_proc_macro, is_lint_allowed, peel_hir_expr_refs, peel_hir_expr_unary};
77
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
8+
use rustc_hir::LangItem;
89
use rustc_lint::{LateContext, LateLintPass};
910
use rustc_middle::ty::{self, Ty};
1011
use rustc_session::impl_lint_pass;
@@ -102,7 +103,7 @@ impl ArithmeticSideEffects {
102103

103104
let ty::Adt(adt, substs) = ty.kind() else { return false };
104105

105-
if !tcx.is_diagnostic_item(sym::NonZero, adt.did()) {
106+
if !tcx.is_lang_item(adt.did(), LangItem::NonZero) {
106107
return false;
107108
}
108109

src/tools/clippy/clippy_lints/src/operators/integer_division.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::ty::is_type_diagnostic_item;
3-
use rustc_hir as hir;
2+
use clippy_utils::ty::is_type_lang_item;
3+
use rustc_hir::{self as hir, LangItem};
44
use rustc_lint::LateContext;
5-
use rustc_span::symbol::sym;
65

76
use super::INTEGER_DIVISION;
87

@@ -16,7 +15,7 @@ pub(crate) fn check<'tcx>(
1615
if op == hir::BinOpKind::Div
1716
&& cx.typeck_results().expr_ty(left).is_integral()
1817
&& let right_ty = cx.typeck_results().expr_ty(right)
19-
&& (right_ty.is_integral() || is_type_diagnostic_item(cx, right_ty, sym::NonZero))
18+
&& (right_ty.is_integral() || is_type_lang_item(cx, right_ty, LangItem::NonZero))
2019
{
2120
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
2221
span_lint_and_then(cx, INTEGER_DIVISION, expr.span, "integer division", |diag| {

src/tools/clippy/clippy_lints/src/transmute/transmute_int_to_non_zero.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::TRANSMUTE_INT_TO_NON_ZERO;
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::sugg;
44
use rustc_errors::Applicability;
5-
use rustc_hir::Expr;
5+
use rustc_hir::{Expr, LangItem};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty::{self, Ty};
88
use rustc_span::symbol::sym;
@@ -22,7 +22,7 @@ pub(super) fn check<'tcx>(
2222
return false;
2323
};
2424

25-
if !tcx.is_diagnostic_item(sym::NonZero, adt.did()) {
25+
if !tcx.is_lang_item(adt.did(), LangItem::NonZero) {
2626
return false;
2727
}
2828

0 commit comments

Comments
 (0)