Skip to content

Commit ffc391c

Browse files
committed
Auto merge of #13231 - Jarcho:no_tree_walk_in_const, r=Alexendoo
Don't walk the HIR tree when checking for a const context changelog: none
2 parents 5ccf543 + 16633a2 commit ffc391c

24 files changed

+71
-67
lines changed

clippy_lints/src/bool_to_int_with_if.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::sugg::Sugg;
3-
use clippy_utils::{in_constant, is_else_clause};
3+
use clippy_utils::{is_else_clause, is_in_const_context};
44
use rustc_ast::LitKind;
55
use rustc_errors::Applicability;
66
use rustc_hir::{Expr, ExprKind};
@@ -52,7 +52,7 @@ impl<'tcx> LateLintPass<'tcx> for BoolToIntWithIf {
5252
&& let Some(else_lit) = as_int_bool_lit(else_)
5353
&& then_lit != else_lit
5454
&& !expr.span.from_expansion()
55-
&& !in_constant(cx, expr.hir_id)
55+
&& !is_in_const_context(cx)
5656
{
5757
let ty = cx.typeck_results().expr_ty(then);
5858
let mut applicability = Applicability::MachineApplicable;

clippy_lints/src/casts/cast_lossless.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_config::msrvs::{self, Msrv};
22
use clippy_utils::diagnostics::span_lint_and_then;
3-
use clippy_utils::in_constant;
3+
use clippy_utils::is_in_const_context;
44
use clippy_utils::source::snippet_opt;
55
use clippy_utils::sugg::Sugg;
66
use clippy_utils::ty::is_isize_or_usize;
@@ -21,7 +21,7 @@ pub(super) fn check(
2121
cast_to_hir: &rustc_hir::Ty<'_>,
2222
msrv: &Msrv,
2323
) {
24-
if !should_lint(cx, expr, cast_from, cast_to, msrv) {
24+
if !should_lint(cx, cast_from, cast_to, msrv) {
2525
return;
2626
}
2727

@@ -70,9 +70,9 @@ pub(super) fn check(
7070
);
7171
}
7272

73-
fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
73+
fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
7474
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
75-
if in_constant(cx, expr.hir_id) {
75+
if is_in_const_context(cx) {
7676
return false;
7777
}
7878

clippy_lints/src/casts/zero_ptr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_opt;
3-
use clippy_utils::{in_constant, is_integer_literal, std_or_core};
3+
use clippy_utils::{is_in_const_context, is_integer_literal, std_or_core};
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, Mutability, Ty, TyKind};
66
use rustc_lint::LateContext;
@@ -10,7 +10,7 @@ use super::ZERO_PTR;
1010
pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) {
1111
if let TyKind::Ptr(ref mut_ty) = to.kind
1212
&& is_integer_literal(from, 0)
13-
&& !in_constant(cx, from.hir_id)
13+
&& !is_in_const_context(cx)
1414
&& let Some(std_or_core) = std_or_core(cx)
1515
{
1616
let (msg, sugg_fn) = match mut_ty.mutbl {

clippy_lints/src/checked_conversions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_config::msrvs::{self, Msrv};
44
use clippy_config::Conf;
55
use clippy_utils::diagnostics::span_lint_and_sugg;
66
use clippy_utils::source::snippet_with_applicability;
7-
use clippy_utils::{in_constant, is_integer_literal, SpanlessEq};
7+
use clippy_utils::{is_in_const_context, is_integer_literal, SpanlessEq};
88
use rustc_errors::Applicability;
99
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, TyKind};
1010
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -67,7 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for CheckedConversions {
6767
_ => return,
6868
}
6969
&& !in_external_macro(cx.sess(), item.span)
70-
&& !in_constant(cx, item.hir_id)
70+
&& !is_in_const_context(cx)
7171
&& self.msrv.meets(msrvs::TRY_FROM)
7272
&& let Some(cv) = match op2 {
7373
// todo: check for case signed -> larger unsigned == only x >= 0

clippy_lints/src/comparison_chain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::ty::implements_trait;
3-
use clippy_utils::{if_sequence, in_constant, is_else_clause, SpanlessEq};
3+
use clippy_utils::{if_sequence, is_else_clause, is_in_const_context, SpanlessEq};
44
use rustc_hir::{BinOpKind, Expr, ExprKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
@@ -68,7 +68,7 @@ impl<'tcx> LateLintPass<'tcx> for ComparisonChain {
6868
return;
6969
}
7070

71-
if in_constant(cx, expr.hir_id) {
71+
if is_in_const_context(cx) {
7272
return;
7373
}
7474

clippy_lints/src/doc/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::diagnostics::{span_lint, span_lint_and_help};
55
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
66
use clippy_utils::ty::is_type_diagnostic_item;
77
use clippy_utils::visitors::Visitable;
8-
use clippy_utils::{in_constant, is_entrypoint_fn, is_trait_impl_item, method_chain_args};
8+
use clippy_utils::{is_entrypoint_fn, is_trait_impl_item, method_chain_args};
99
use pulldown_cmark::Event::{
1010
Code, DisplayMath, End, FootnoteReference, HardBreak, Html, InlineHtml, InlineMath, Rule, SoftBreak, Start,
1111
TaskListMarker, Text,
@@ -858,7 +858,7 @@ impl<'a, 'tcx> Visitor<'tcx> for FindPanicUnwrap<'a, 'tcx> {
858858
"assert" | "assert_eq" | "assert_ne"
859859
)
860860
{
861-
self.is_const = in_constant(self.cx, expr.hir_id);
861+
self.is_const = self.cx.tcx.hir().is_inside_const_context(expr.hir_id);
862862
self.panic_span = Some(macro_call.span);
863863
}
864864
}

clippy_lints/src/floating_point_arithmetic.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use clippy_utils::consts::Constant::{Int, F32, F64};
22
use clippy_utils::consts::{constant, constant_simple, Constant};
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::{
5-
eq_expr_value, get_parent_expr, higher, in_constant, is_inherent_method_call, is_no_std_crate, numeric_literal,
6-
peel_blocks, sugg,
5+
eq_expr_value, get_parent_expr, higher, is_in_const_context, is_inherent_method_call, is_no_std_crate,
6+
numeric_literal, peel_blocks, sugg,
77
};
88
use rustc_errors::Applicability;
99
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
@@ -753,7 +753,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
753753
impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
754754
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
755755
// All of these operations are currently not const and are in std.
756-
if in_constant(cx, expr.hir_id) {
756+
if is_in_const_context(cx) {
757757
return;
758758
}
759759

clippy_lints/src/from_str_radix_10.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::sugg::Sugg;
33
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
4-
use clippy_utils::{in_constant, is_integer_literal};
4+
use clippy_utils::{is_in_const_context, is_integer_literal};
55
use rustc_errors::Applicability;
66
use rustc_hir::{def, Expr, ExprKind, LangItem, PrimTy, QPath, TyKind};
77
use rustc_lint::{LateContext, LateLintPass};
@@ -63,7 +63,7 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
6363

6464
// do not lint in constant context, because the suggestion won't work.
6565
// NB: keep this check until a new `const_trait_impl` is available and stabilized.
66-
&& !in_constant(cx, exp.hir_id)
66+
&& !is_in_const_context(cx)
6767
{
6868
let expr = if let ExprKind::AddrOf(_, _, expr) = &src.kind {
6969
let ty = cx.typeck_results().expr_ty(expr);

clippy_lints/src/if_then_some_else_none.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
55
use clippy_utils::source::snippet_with_context;
66
use clippy_utils::sugg::Sugg;
7-
use clippy_utils::{contains_return, higher, in_constant, is_else_clause, is_res_lang_ctor, path_res, peel_blocks};
7+
use clippy_utils::{
8+
contains_return, higher, is_else_clause, is_in_const_context, is_res_lang_ctor, path_res, peel_blocks,
9+
};
810
use rustc_errors::Applicability;
911
use rustc_hir::LangItem::{OptionNone, OptionSome};
1012
use rustc_hir::{Expr, ExprKind};
@@ -76,7 +78,7 @@ impl<'tcx> LateLintPass<'tcx> for IfThenSomeElseNone {
7678
&& is_res_lang_ctor(cx, path_res(cx, then_call), OptionSome)
7779
&& is_res_lang_ctor(cx, path_res(cx, peel_blocks(els)), OptionNone)
7880
&& !is_else_clause(cx.tcx, expr)
79-
&& !in_constant(cx, expr.hir_id)
81+
&& !is_in_const_context(cx)
8082
&& !in_external_macro(cx.sess(), expr.span)
8183
&& self.msrv.meets(msrvs::BOOL_THEN)
8284
&& !contains_return(then_block.stmts)

clippy_lints/src/manual_clamp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use clippy_utils::sugg::Sugg;
77
use clippy_utils::ty::implements_trait;
88
use clippy_utils::visitors::is_const_evaluatable;
99
use clippy_utils::{
10-
eq_expr_value, in_constant, is_diag_trait_item, is_trait_method, path_res, path_to_local_id, peel_blocks,
10+
eq_expr_value, is_diag_trait_item, is_in_const_context, is_trait_method, path_res, path_to_local_id, peel_blocks,
1111
peel_blocks_with_stmt, MaybePath,
1212
};
1313
use itertools::Itertools;
@@ -146,7 +146,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp {
146146
if !self.msrv.meets(msrvs::CLAMP) {
147147
return;
148148
}
149-
if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
149+
if !expr.span.from_expansion() && !is_in_const_context(cx) {
150150
let suggestion = is_if_elseif_else_pattern(cx, expr)
151151
.or_else(|| is_max_min_pattern(cx, expr))
152152
.or_else(|| is_call_max_min_pattern(cx, expr))
@@ -159,7 +159,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualClamp {
159159
}
160160

161161
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
162-
if !self.msrv.meets(msrvs::CLAMP) || in_constant(cx, block.hir_id) {
162+
if !self.msrv.meets(msrvs::CLAMP) || is_in_const_context(cx) {
163163
return;
164164
}
165165
for suggestion in is_two_if_pattern(cx, block) {

0 commit comments

Comments
 (0)