Skip to content

Commit a20d2ef

Browse files
committed
Improve explicitness of the impl of the useless_ptr_null_checks lint
1 parent db576c1 commit a20d2ef

File tree

3 files changed

+26
-26
lines changed

3 files changed

+26
-26
lines changed

Diff for: compiler/rustc_lint/messages.ftl

+9-9
Original file line numberDiff line numberDiff line change
@@ -680,15 +680,6 @@ lint_private_extern_crate_reexport = extern crate `{$ident}` is private and cann
680680
lint_proc_macro_derive_resolution_fallback = cannot find {$ns} `{$ident}` in this scope
681681
.label = names from parent modules are not accessible without an explicit import
682682
683-
lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
684-
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
685-
.label = expression has type `{$orig_ty}`
686-
687-
lint_ptr_null_checks_fn_ret = returned pointer of `{$fn_name}` call is never null, so checking it for null will always return false
688-
689-
lint_ptr_null_checks_ref = references are not nullable, so checking them for null will always return false
690-
.label = expression has type `{$orig_ty}`
691-
692683
lint_query_instability = using `{$query}` can result in unstable query results
693684
.note = if you believe this case to be fine, allow this lint and add a comment explaining your rationale
694685
@@ -978,6 +969,15 @@ lint_unused_result = unused result of type `{$ty}`
978969
979970
lint_use_let_underscore_ignore_suggestion = use `let _ = ...` to ignore the expression or result
980971
972+
lint_useless_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
973+
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
974+
.label = expression has type `{$orig_ty}`
975+
976+
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
977+
978+
lint_useless_ptr_null_checks_ref = references are not nullable, so checking them for null will always return false
979+
.label = expression has type `{$orig_ty}`
980+
981981
lint_uses_power_alignment = repr(C) does not follow the power alignment rule. This may affect platform C ABI compatibility for this type
982982
983983
lint_variant_size_differences =

Diff for: compiler/rustc_lint/src/lints.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -591,21 +591,21 @@ pub(crate) struct ExpectationNote {
591591

592592
// ptr_nulls.rs
593593
#[derive(LintDiagnostic)]
594-
pub(crate) enum PtrNullChecksDiag<'a> {
595-
#[diag(lint_ptr_null_checks_fn_ptr)]
596-
#[help(lint_help)]
594+
pub(crate) enum UselessPtrNullChecksDiag<'a> {
595+
#[diag(lint_useless_ptr_null_checks_fn_ptr)]
596+
#[help]
597597
FnPtr {
598598
orig_ty: Ty<'a>,
599599
#[label]
600600
label: Span,
601601
},
602-
#[diag(lint_ptr_null_checks_ref)]
602+
#[diag(lint_useless_ptr_null_checks_ref)]
603603
Ref {
604604
orig_ty: Ty<'a>,
605605
#[label]
606606
label: Span,
607607
},
608-
#[diag(lint_ptr_null_checks_fn_ret)]
608+
#[diag(lint_useless_ptr_null_checks_fn_ret)]
609609
FnRet { fn_name: Ident },
610610
}
611611

Diff for: compiler/rustc_lint/src/ptr_nulls.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::{BinOpKind, Expr, ExprKind, TyKind};
33
use rustc_session::{declare_lint, declare_lint_pass};
44
use rustc_span::sym;
55

6-
use crate::lints::PtrNullChecksDiag;
6+
use crate::lints::UselessPtrNullChecksDiag;
77
use crate::{LateContext, LateLintPass, LintContext};
88

99
declare_lint! {
@@ -38,10 +38,10 @@ declare_lint_pass!(PtrNullChecks => [USELESS_PTR_NULL_CHECKS]);
3838
/// a fn ptr, a reference, or a function call whose definition is
3939
/// annotated with `#![rustc_never_returns_null_ptr]`.
4040
/// If this situation is present, the function returns the appropriate diagnostic.
41-
fn incorrect_check<'a, 'tcx: 'a>(
41+
fn useless_check<'a, 'tcx: 'a>(
4242
cx: &'a LateContext<'tcx>,
4343
mut e: &'a Expr<'a>,
44-
) -> Option<PtrNullChecksDiag<'tcx>> {
44+
) -> Option<UselessPtrNullChecksDiag<'tcx>> {
4545
let mut had_at_least_one_cast = false;
4646
loop {
4747
e = e.peel_blocks();
@@ -50,14 +50,14 @@ fn incorrect_check<'a, 'tcx: 'a>(
5050
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)
5151
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
5252
{
53-
return Some(PtrNullChecksDiag::FnRet { fn_name });
53+
return Some(UselessPtrNullChecksDiag::FnRet { fn_name });
5454
} else if let ExprKind::Call(path, _args) = e.kind
5555
&& let ExprKind::Path(ref qpath) = path.kind
5656
&& let Some(def_id) = cx.qpath_res(qpath, path.hir_id).opt_def_id()
5757
&& cx.tcx.has_attr(def_id, sym::rustc_never_returns_null_ptr)
5858
&& let Some(fn_name) = cx.tcx.opt_item_ident(def_id)
5959
{
60-
return Some(PtrNullChecksDiag::FnRet { fn_name });
60+
return Some(UselessPtrNullChecksDiag::FnRet { fn_name });
6161
}
6262
e = if let ExprKind::Cast(expr, t) = e.kind
6363
&& let TyKind::Ptr(_) = t.kind
@@ -73,9 +73,9 @@ fn incorrect_check<'a, 'tcx: 'a>(
7373
} else if had_at_least_one_cast {
7474
let orig_ty = cx.typeck_results().expr_ty(e);
7575
return if orig_ty.is_fn() {
76-
Some(PtrNullChecksDiag::FnPtr { orig_ty, label: e.span })
76+
Some(UselessPtrNullChecksDiag::FnPtr { orig_ty, label: e.span })
7777
} else if orig_ty.is_ref() {
78-
Some(PtrNullChecksDiag::Ref { orig_ty, label: e.span })
78+
Some(UselessPtrNullChecksDiag::Ref { orig_ty, label: e.span })
7979
} else {
8080
None
8181
};
@@ -97,7 +97,7 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
9797
cx.tcx.get_diagnostic_name(def_id),
9898
Some(sym::ptr_const_is_null | sym::ptr_is_null)
9999
)
100-
&& let Some(diag) = incorrect_check(cx, arg) =>
100+
&& let Some(diag) = useless_check(cx, arg) =>
101101
{
102102
cx.emit_span_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag)
103103
}
@@ -110,18 +110,18 @@ impl<'tcx> LateLintPass<'tcx> for PtrNullChecks {
110110
cx.tcx.get_diagnostic_name(def_id),
111111
Some(sym::ptr_const_is_null | sym::ptr_is_null)
112112
)
113-
&& let Some(diag) = incorrect_check(cx, receiver) =>
113+
&& let Some(diag) = useless_check(cx, receiver) =>
114114
{
115115
cx.emit_span_lint(USELESS_PTR_NULL_CHECKS, expr.span, diag)
116116
}
117117

118118
ExprKind::Binary(op, left, right) if matches!(op.node, BinOpKind::Eq) => {
119119
let to_check: &Expr<'_>;
120-
let diag: PtrNullChecksDiag<'_>;
121-
if let Some(ddiag) = incorrect_check(cx, left) {
120+
let diag: UselessPtrNullChecksDiag<'_>;
121+
if let Some(ddiag) = useless_check(cx, left) {
122122
to_check = right;
123123
diag = ddiag;
124-
} else if let Some(ddiag) = incorrect_check(cx, right) {
124+
} else if let Some(ddiag) = useless_check(cx, right) {
125125
to_check = left;
126126
diag = ddiag;
127127
} else {

0 commit comments

Comments
 (0)