Skip to content

Commit 7ec721d

Browse files
committed
Make NonZero a lang item
1 parent 7878a91 commit 7ec721d

File tree

8 files changed

+15
-10
lines changed

8 files changed

+15
-10
lines changed

compiler/rustc_hir/src/lang_items.rs

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

431+
NonZero, sym::NonZero, non_zero, Target::Struct, GenericRequirement::Exact(1);
432+
431433
// Experimental lang items for implementing contract pre- and post-condition checking.
432434
ContractBuildCheckEnsures, sym::contract_build_check_ensures, contract_build_check_ensures_fn, Target::Fn, GenericRequirement::None;
433435
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
@@ -2581,7 +2581,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
25812581
_ => return false,
25822582
};
25832583

2584-
if !self.tcx.is_diagnostic_item(sym::NonZero, adt.did()) {
2584+
if !self.tcx.is_lang_item(adt.did(), LangItem::NonZero) {
25852585
return false;
25862586
}
25872587

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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_errors::Applicability;
77
use rustc_hir::{Block, BlockCheckMode, Expr, ExprKind, Node, QPath, UnsafeSource};
88
use rustc_lint::LateContext;
99
use rustc_span::sym;
10+
use rustc_hir::LangItem;
1011

1112
use super::USELESS_NONZERO_NEW_UNCHECKED;
1213

@@ -15,7 +16,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, func: &Expr<'
1516
&& segment.ident.name == sym::new_unchecked
1617
&& let [init_arg] = args
1718
&& is_inside_always_const_context(cx.tcx, expr.hir_id)
18-
&& cx.typeck_results().node_type(ty.hir_id).is_diag_item(cx, sym::NonZero)
19+
&& cx.typeck_results().node_type(ty.hir_id).is_lang_item(cx, LangItem::NonZero)
1920
&& msrv.meets(cx, msrvs::CONST_UNWRAP)
2021
{
2122
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::res::MaybeDef;
66
use clippy_utils::{expr_or_init, is_from_proc_macro, is_lint_allowed, peel_hir_expr_refs, peel_hir_expr_unary, sym};
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;
@@ -90,7 +91,7 @@ impl ArithmeticSideEffects {
9091

9192
fn is_non_zero_u(cx: &LateContext<'_>, ty: Ty<'_>) -> bool {
9293
if let ty::Adt(adt, substs) = ty.kind()
93-
&& cx.tcx.is_diagnostic_item(sym::NonZero, adt.did())
94+
&& cx.tcx.is_lang_item(adt.did(),LangItem::NonZero)
9495
&& let int_type = substs.type_at(0)
9596
&& matches!(int_type.kind(), ty::Uint(_))
9697
{

src/tools/clippy/clippy_lints/src/operators/integer_division.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::res::MaybeDef;
33
use rustc_hir as hir;
44
use rustc_lint::LateContext;
5-
use rustc_span::symbol::sym;
5+
use rustc_hir::LangItem;
66

77
use super::INTEGER_DIVISION;
88

@@ -16,7 +16,7 @@ pub(crate) fn check<'tcx>(
1616
if op == hir::BinOpKind::Div
1717
&& cx.typeck_results().expr_ty(left).is_integral()
1818
&& let right_ty = cx.typeck_results().expr_ty(right)
19-
&& (right_ty.is_integral() || right_ty.is_diag_item(cx, sym::NonZero))
19+
&& (right_ty.is_integral() || right_ty.is_lang_item(cx, LangItem::NonZero))
2020
{
2121
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]
2222
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ use super::TRANSMUTE_INT_TO_NON_ZERO;
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::sugg::Sugg;
44
use rustc_errors::Applicability;
5-
use rustc_hir::Expr;
5+
use rustc_hir::{Expr};
66
use rustc_lint::LateContext;
77
use rustc_middle::ty::{self, Ty};
88
use rustc_span::symbol::sym;
9+
use rustc_hir::LangItem;
910

1011
/// Checks for `transmute_int_to_non_zero` lint.
1112
/// Returns `true` if it's triggered, otherwise returns `false`.
@@ -18,7 +19,7 @@ pub(super) fn check<'tcx>(
1819
) -> bool {
1920
if let ty::Int(_) | ty::Uint(_) = from_ty.kind()
2021
&& let ty::Adt(adt, substs) = to_ty.kind()
21-
&& cx.tcx.is_diagnostic_item(sym::NonZero, adt.did())
22+
&& cx.tcx.is_lang_item( adt.did(), LangItem::NonZero)
2223
&& let int_ty = substs.type_at(0)
2324
&& from_ty == int_ty
2425
{

0 commit comments

Comments
 (0)