Skip to content

Commit 4f475d8

Browse files
committed
Parameterize peel_casts follow init behavior
1 parent 3f9f20f commit 4f475d8

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

compiler/rustc_lint/src/ptr_nulls.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_session::{declare_lint, declare_lint_pass};
55
use rustc_span::{Span, sym};
66

77
use crate::lints::{InvalidNullArgumentsDiag, UselessPtrNullChecksDiag};
8-
use crate::utils::peel_casts;
8+
use crate::utils::{FollowInit, peel_casts};
99
use crate::{LateContext, LateLintPass, LintContext};
1010

1111
declare_lint! {
@@ -112,7 +112,7 @@ fn useless_check<'a, 'tcx: 'a>(
112112

113113
/// Checks if the given expression is a null pointer (modulo casting)
114114
fn is_null_ptr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<Span> {
115-
let (expr, _) = peel_casts(cx, expr);
115+
let (expr, _) = peel_casts(cx, FollowInit::Yes, expr);
116116

117117
if let ExprKind::Call(path, []) = expr.kind
118118
&& let ExprKind::Path(ref qpath) = path.kind

compiler/rustc_lint/src/reference_casting.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_session::{declare_lint, declare_lint_pass};
66
use rustc_span::sym;
77

88
use crate::lints::InvalidReferenceCastingDiag;
9-
use crate::utils::peel_casts;
9+
use crate::utils::{FollowInit, peel_casts};
1010
use crate::{LateContext, LateLintPass, LintContext};
1111

1212
declare_lint! {
@@ -48,7 +48,9 @@ impl<'tcx> LateLintPass<'tcx> for InvalidReferenceCasting {
4848
// small cache to avoid recomputing needlessly computing peel_casts of init
4949
let mut peel_casts = {
5050
let mut peel_casts_cache = None;
51-
move || *peel_casts_cache.get_or_insert_with(|| peel_casts(cx, init))
51+
move || {
52+
*peel_casts_cache.get_or_insert_with(|| peel_casts(cx, FollowInit::Yes, init))
53+
}
5254
};
5355

5456
if matches!(pat, PatternKind::Borrow { mutbl: Mutability::Mut } | PatternKind::Assign)

compiler/rustc_lint/src/utils.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,20 @@ use rustc_span::sym;
33

44
use crate::LateContext;
55

6+
#[derive(Clone, Copy, PartialEq)]
7+
pub(crate) enum FollowInit {
8+
Yes,
9+
No,
10+
}
11+
612
/// Given an expression, peel all of casts (`<expr> as ...`, `<expr>.cast{,_mut,_const}()`,
713
/// `ptr::from_ref(<expr>)`, ...) and init expressions.
814
///
915
/// Returns the innermost expression and a boolean representing if one of the casts was
1016
/// `UnsafeCell::raw_get(<expr>)`
1117
pub(crate) fn peel_casts<'tcx>(
1218
cx: &LateContext<'tcx>,
19+
follow_init: FollowInit,
1320
mut e: &'tcx Expr<'tcx>,
1421
) -> (&'tcx Expr<'tcx>, bool) {
1522
let mut gone_trough_unsafe_cell_raw_get = false;
@@ -41,13 +48,15 @@ pub(crate) fn peel_casts<'tcx>(
4148
gone_trough_unsafe_cell_raw_get = true;
4249
}
4350
arg
44-
} else {
51+
} else if follow_init == FollowInit::Yes {
4552
let init = cx.expr_or_init(e);
4653
if init.hir_id != e.hir_id {
4754
init
4855
} else {
4956
break;
5057
}
58+
} else {
59+
break;
5160
};
5261
}
5362

0 commit comments

Comments
 (0)