Skip to content

Commit 15ff9fa

Browse files
committed
Improve explicitness of the impl of the useless_ptr_null_checks lint
1 parent 5151b8c commit 15ff9fa

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

compiler/rustc_lint/messages.ftl

+9-9
Original file line numberDiff line numberDiff line change
@@ -453,15 +453,6 @@ lint_path_statement_drop = path statement drops value
453453
454454
lint_path_statement_no_effect = path statement with no effect
455455
456-
lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
457-
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
458-
.label = expression has type `{$orig_ty}`
459-
460-
lint_ptr_null_checks_fn_ret = returned pointer of `{$fn_name}` call is never null, so checking it for null will always return false
461-
462-
lint_ptr_null_checks_ref = references are not nullable, so checking them for null will always return false
463-
.label = expression has type `{$orig_ty}`
464-
465456
lint_query_instability = using `{$query}` can result in unstable query results
466457
.note = if you believe this case to be fine, allow this lint and add a comment explaining your rationale
467458
@@ -575,5 +566,14 @@ lint_unused_op = unused {$op} that must be used
575566
576567
lint_unused_result = unused result of type `{$ty}`
577568
569+
lint_useless_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
570+
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
571+
.label = expression has type `{$orig_ty}`
572+
573+
lint_useless_ptr_null_checks_fn_ret = returned pointer of `{$fn_name}` call is never null, so checking it for null will always return false
574+
575+
lint_useless_ptr_null_checks_ref = references are not nullable, so checking them for null will always return false
576+
.label = expression has type `{$orig_ty}`
577+
578578
lint_variant_size_differences =
579579
enum variant is more than three times larger ({$largest} bytes) than the next largest

compiler/rustc_lint/src/lints.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -596,21 +596,21 @@ pub struct ExpectationNote {
596596

597597
// ptr_nulls.rs
598598
#[derive(LintDiagnostic)]
599-
pub enum PtrNullChecksDiag<'a> {
600-
#[diag(lint_ptr_null_checks_fn_ptr)]
601-
#[help(lint_help)]
599+
pub enum UselessPtrNullChecksDiag<'a> {
600+
#[diag(lint_useless_ptr_null_checks_fn_ptr)]
601+
#[help]
602602
FnPtr {
603603
orig_ty: Ty<'a>,
604604
#[label]
605605
label: Span,
606606
},
607-
#[diag(lint_ptr_null_checks_ref)]
607+
#[diag(lint_useless_ptr_null_checks_ref)]
608608
Ref {
609609
orig_ty: Ty<'a>,
610610
#[label]
611611
label: Span,
612612
},
613-
#[diag(lint_ptr_null_checks_fn_ret)]
613+
#[diag(lint_useless_ptr_null_checks_fn_ret)]
614614
FnRet { fn_name: Ident },
615615
}
616616

compiler/rustc_lint/src/ptr_nulls.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{lints::PtrNullChecksDiag, LateContext, LateLintPass, LintContext};
1+
use crate::{lints::UselessPtrNullChecksDiag, LateContext, LateLintPass, LintContext};
22
use rustc_ast::LitKind;
33
use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind};
44
use rustc_session::{declare_lint, declare_lint_pass};
@@ -36,10 +36,10 @@ declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS]);
3636
/// a fn ptr, a reference, or a function call whose definition is
3737
/// annotated with `#![rustc_never_returns_null_ptr]`.
3838
/// If this situation is present, the function returns the appropriate diagnostic.
39-
fn incorrect_check<'a, 'tcx: 'a>(
39+
fn useless_check<'a, 'tcx: 'a>(
4040
cx: &'a LateContext<'tcx>,
4141
mut e: &'a Expr<'a>,
42-
) -> Option<PtrNullChecksDiag<'tcx>> {
42+
) -> Option<UselessPtrNullChecksDiag<'tcx>> {
4343
let mut had_at_least_one_cast = false;
4444
loop {
4545
e = e.peel_blocks();
@@ -48,14 +48,14 @@ fn incorrect_check<'a, 'tcx: 'a>(
4848
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)
4949
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
5050
{
51-
return Some(PtrNullChecksDiag::FnRet { fn_name });
51+
return Some(UselessPtrNullChecksDiag::FnRet { fn_name });
5252
} else if let ExprKind::Call(path, _args) = e.kind
5353
&& let ExprKind::Path(ref qpath) = path.kind
5454
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
5555
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)
5656
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
5757
{
58-
return Some(PtrNullChecksDiag::FnRet { fn_name });
58+
return Some(UselessPtrNullChecksDiag::FnRet { fn_name });
5959
}
6060
e = if let ExprKind::Cast(expr, t) = e.kind
6161
&& let TyKind::Ptr(_) = t.kind
@@ -71,9 +71,9 @@ fn incorrect_check<'a, 'tcx: 'a>(
7171
} else if had_at_least_one_cast {
7272
let orig_ty = cx.typeck_results().expr_ty(e);
7373
return if orig_ty.is_fn() {
74-
Some(PtrNullChecksDiag::FnPtr { orig_ty, label: e.span })
74+
Some(UselessPtrNullChecksDiag::FnPtr { orig_ty, label: e.span })
7575
} else if orig_ty.is_ref() {
76-
Some(PtrNullChecksDiag::Ref { orig_ty, label: e.span })
76+
Some(UselessPtrNullChecksDiag::Ref { orig_ty, label: e.span })
7777
} else {
7878
None
7979
};
@@ -95,7 +95,7 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
9595
cx.tcx.get_diagnostic_name(def_id),
9696
Some(sym::ptr_const_is_null | sym::ptr_is_null)
9797
)
98-
&& let Some(diag) = incorrect_check(cx, arg) =>
98+
&& let Some(diag) = useless_check(cx, arg) =>
9999
{
100100
cx.emit_spanned_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag)
101101
}
@@ -108,18 +108,18 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
108108
cx.tcx.get_diagnostic_name(def_id),
109109
Some(sym::ptr_const_is_null | sym::ptr_is_null)
110110
)
111-
&& let Some(diag) = incorrect_check(cx, receiver) =>
111+
&& let Some(diag) = useless_check(cx, receiver) =>
112112
{
113113
cx.emit_spanned_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag)
114114
}
115115

116116
ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => {
117117
let to_check: &Expr<'_>;
118-
let diag: PtrNullChecksDiag<'_>;
119-
if let Some(ddiag) = incorrect_check(cx, left) {
118+
let diag: UselessPtrNullChecksDiag<'_>;
119+
if let Some(ddiag) = useless_check(cx, left) {
120120
to_check = right;
121121
diag = ddiag;
122-
} else if let Some(ddiag) = incorrect_check(cx, right) {
122+
} else if let Some(ddiag) = useless_check(cx, right) {
123123
to_check = left;
124124
diag = ddiag;
125125
} else {

0 commit comments

Comments
 (0)