Replace in_constant
with is_in_const_context
. Use only the state in LateContext
to avoid walking the HIR tree.
This commit is contained in:
parent
5ead90f13a
commit
16633a2d7b
@ -1,6 +1,6 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_then;
|
use clippy_utils::diagnostics::span_lint_and_then;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::{in_constant, is_else_clause};
|
use clippy_utils::{is_else_clause, is_in_const_context};
|
||||||
use rustc_ast::LitKind;
|
use rustc_ast::LitKind;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Expr, ExprKind};
|
use rustc_hir::{Expr, ExprKind};
|
||||||
@ -52,7 +52,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
|||||||
&& let Some(else_lit) = as_int_bool_lit(else_)
|
&& let Some(else_lit) = as_int_bool_lit(else_)
|
||||||
&& then_lit != else_lit
|
&& then_lit != else_lit
|
||||||
&& !expr.span.from_expansion()
|
&& !expr.span.from_expansion()
|
||||||
&& !in_constant(cx, expr.hir_id)
|
&& !is_in_const_context(cx)
|
||||||
{
|
{
|
||||||
let ty = cx.typeck_results().expr_ty(then);
|
let ty = cx.typeck_results().expr_ty(then);
|
||||||
let mut applicability = Applicability::MachineApplicable;
|
let mut applicability = Applicability::MachineApplicable;
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use clippy_config::msrvs::{self, Msrv};
|
use clippy_config::msrvs::{self, Msrv};
|
||||||
use clippy_utils::diagnostics::span_lint_and_then;
|
use clippy_utils::diagnostics::span_lint_and_then;
|
||||||
use clippy_utils::in_constant;
|
use clippy_utils::is_in_const_context;
|
||||||
use clippy_utils::source::snippet_opt;
|
use clippy_utils::source::snippet_opt;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::ty::is_isize_or_usize;
|
use clippy_utils::ty::is_isize_or_usize;
|
||||||
@ -21,7 +21,7 @@ pub(super) fn check(
|
|||||||
cast_to_hir: &rustc_hir::Ty<'_>,
|
cast_to_hir: &rustc_hir::Ty<'_>,
|
||||||
msrv: &Msrv,
|
msrv: &Msrv,
|
||||||
) {
|
) {
|
||||||
if !should_lint(cx, expr, cast_from, cast_to, msrv) {
|
if !should_lint(cx, cast_from, cast_to, msrv) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -70,9 +70,9 @@ pub(super) fn check(
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn should_lint(cx: &LateContext<'_>, expr: &Expr<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
|
fn should_lint(cx: &LateContext<'_>, cast_from: Ty<'_>, cast_to: Ty<'_>, msrv: &Msrv) -> bool {
|
||||||
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
|
// Do not suggest using From in consts/statics until it is valid to do so (see #2267).
|
||||||
if in_constant(cx, expr.hir_id) {
|
if is_in_const_context(cx) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::source::snippet_opt;
|
use clippy_utils::source::snippet_opt;
|
||||||
use clippy_utils::{in_constant, is_integer_literal, std_or_core};
|
use clippy_utils::{is_in_const_context, is_integer_literal, std_or_core};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{Expr, Mutability, Ty, TyKind};
|
use rustc_hir::{Expr, Mutability, Ty, TyKind};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
@ -10,7 +10,7 @@
|
|||||||
pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) {
|
pub fn check(cx: &LateContext<'_>, expr: &Expr<'_>, from: &Expr<'_>, to: &Ty<'_>) {
|
||||||
if let TyKind::Ptr(ref mut_ty) = to.kind
|
if let TyKind::Ptr(ref mut_ty) = to.kind
|
||||||
&& is_integer_literal(from, 0)
|
&& is_integer_literal(from, 0)
|
||||||
&& !in_constant(cx, from.hir_id)
|
&& !is_in_const_context(cx)
|
||||||
&& let Some(std_or_core) = std_or_core(cx)
|
&& let Some(std_or_core) = std_or_core(cx)
|
||||||
{
|
{
|
||||||
let (msg, sugg_fn) = match mut_ty.mutbl {
|
let (msg, sugg_fn) = match mut_ty.mutbl {
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
use clippy_config::Conf;
|
use clippy_config::Conf;
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::source::snippet_with_applicability;
|
use clippy_utils::source::snippet_with_applicability;
|
||||||
use clippy_utils::{in_constant, is_integer_literal, SpanlessEq};
|
use clippy_utils::{is_in_const_context, is_integer_literal, SpanlessEq};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, TyKind};
|
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, TyKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||||
@ -67,7 +67,7 @@ fn check_expr(&mut self, cx: &LateContext<'_>, item: &Expr<'_>) {
|
|||||||
_ => return,
|
_ => return,
|
||||||
}
|
}
|
||||||
&& !in_external_macro(cx.sess(), item.span)
|
&& !in_external_macro(cx.sess(), item.span)
|
||||||
&& !in_constant(cx, item.hir_id)
|
&& !is_in_const_context(cx)
|
||||||
&& self.msrv.meets(msrvs::TRY_FROM)
|
&& self.msrv.meets(msrvs::TRY_FROM)
|
||||||
&& let Some(cv) = match op2 {
|
&& let Some(cv) = match op2 {
|
||||||
// todo: check for case signed -> larger unsigned == only x >= 0
|
// todo: check for case signed -> larger unsigned == only x >= 0
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_help;
|
use clippy_utils::diagnostics::span_lint_and_help;
|
||||||
use clippy_utils::ty::implements_trait;
|
use clippy_utils::ty::implements_trait;
|
||||||
use clippy_utils::{if_sequence, in_constant, is_else_clause, SpanlessEq};
|
use clippy_utils::{if_sequence, is_else_clause, is_in_const_context, SpanlessEq};
|
||||||
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::declare_lint_pass;
|
use rustc_session::declare_lint_pass;
|
||||||
@ -68,7 +68,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if in_constant(cx, expr.hir_id) {
|
if is_in_const_context(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@
|
|||||||
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
|
use clippy_utils::macros::{is_panic, root_macro_call_first_node};
|
||||||
use clippy_utils::ty::is_type_diagnostic_item;
|
use clippy_utils::ty::is_type_diagnostic_item;
|
||||||
use clippy_utils::visitors::Visitable;
|
use clippy_utils::visitors::Visitable;
|
||||||
use clippy_utils::{in_constant, is_entrypoint_fn, is_trait_impl_item, method_chain_args};
|
use clippy_utils::{is_entrypoint_fn, is_trait_impl_item, method_chain_args};
|
||||||
use pulldown_cmark::Event::{
|
use pulldown_cmark::Event::{
|
||||||
Code, DisplayMath, End, FootnoteReference, HardBreak, Html, InlineHtml, InlineMath, Rule, SoftBreak, Start,
|
Code, DisplayMath, End, FootnoteReference, HardBreak, Html, InlineHtml, InlineMath, Rule, SoftBreak, Start,
|
||||||
TaskListMarker, Text,
|
TaskListMarker, Text,
|
||||||
@ -858,7 +858,7 @@ fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
|
|||||||
"assert" | "assert_eq" | "assert_ne"
|
"assert" | "assert_eq" | "assert_ne"
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
self.is_const = in_constant(self.cx, expr.hir_id);
|
self.is_const = self.cx.tcx.hir().is_inside_const_context(expr.hir_id);
|
||||||
self.panic_span = Some(macro_call.span);
|
self.panic_span = Some(macro_call.span);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,8 @@
|
|||||||
use clippy_utils::consts::{constant, constant_simple, Constant};
|
use clippy_utils::consts::{constant, constant_simple, Constant};
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::{
|
use clippy_utils::{
|
||||||
eq_expr_value, get_parent_expr, higher, in_constant, is_inherent_method_call, is_no_std_crate, numeric_literal,
|
eq_expr_value, get_parent_expr, higher, is_in_const_context, is_inherent_method_call, is_no_std_crate,
|
||||||
peel_blocks, sugg,
|
numeric_literal, peel_blocks, sugg,
|
||||||
};
|
};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
|
use rustc_hir::{BinOpKind, Expr, ExprKind, PathSegment, UnOp};
|
||||||
@ -753,7 +753,7 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
|
|||||||
impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
|
impl<'tcx> LateLintPass<'tcx> for FloatingPointArithmetic {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||||
// All of these operations are currently not const and are in std.
|
// All of these operations are currently not const and are in std.
|
||||||
if in_constant(cx, expr.hir_id) {
|
if is_in_const_context(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
|
use clippy_utils::ty::{is_type_diagnostic_item, is_type_lang_item};
|
||||||
use clippy_utils::{in_constant, is_integer_literal};
|
use clippy_utils::{is_in_const_context, is_integer_literal};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{def, Expr, ExprKind, LangItem, PrimTy, QPath, TyKind};
|
use rustc_hir::{def, Expr, ExprKind, LangItem, PrimTy, QPath, TyKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
@ -63,7 +63,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, exp: &Expr<'tcx>) {
|
|||||||
|
|
||||||
// do not lint in constant context, because the suggestion won't work.
|
// do not lint in constant context, because the suggestion won't work.
|
||||||
// NB: keep this check until a new `const_trait_impl` is available and stabilized.
|
// NB: keep this check until a new `const_trait_impl` is available and stabilized.
|
||||||
&& !in_constant(cx, exp.hir_id)
|
&& !is_in_const_context(cx)
|
||||||
{
|
{
|
||||||
let expr = if let ExprKind::AddrOf(_, _, expr) = &src.kind {
|
let expr = if let ExprKind::AddrOf(_, _, expr) = &src.kind {
|
||||||
let ty = cx.typeck_results().expr_ty(expr);
|
let ty = cx.typeck_results().expr_ty(expr);
|
||||||
|
@ -4,7 +4,9 @@
|
|||||||
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
|
use clippy_utils::eager_or_lazy::switch_to_eager_eval;
|
||||||
use clippy_utils::source::snippet_with_context;
|
use clippy_utils::source::snippet_with_context;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::{contains_return, higher, in_constant, is_else_clause, is_res_lang_ctor, path_res, peel_blocks};
|
use clippy_utils::{
|
||||||
|
contains_return, higher, is_else_clause, is_in_const_context, is_res_lang_ctor, path_res, peel_blocks,
|
||||||
|
};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
use rustc_hir::LangItem::{OptionNone, OptionSome};
|
||||||
use rustc_hir::{Expr, ExprKind};
|
use rustc_hir::{Expr, ExprKind};
|
||||||
@ -76,7 +78,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
|||||||
&& is_res_lang_ctor(cx, path_res(cx, then_call), OptionSome)
|
&& is_res_lang_ctor(cx, path_res(cx, then_call), OptionSome)
|
||||||
&& is_res_lang_ctor(cx, path_res(cx, peel_blocks(els)), OptionNone)
|
&& is_res_lang_ctor(cx, path_res(cx, peel_blocks(els)), OptionNone)
|
||||||
&& !is_else_clause(cx.tcx, expr)
|
&& !is_else_clause(cx.tcx, expr)
|
||||||
&& !in_constant(cx, expr.hir_id)
|
&& !is_in_const_context(cx)
|
||||||
&& !in_external_macro(cx.sess(), expr.span)
|
&& !in_external_macro(cx.sess(), expr.span)
|
||||||
&& self.msrv.meets(msrvs::BOOL_THEN)
|
&& self.msrv.meets(msrvs::BOOL_THEN)
|
||||||
&& !contains_return(then_block.stmts)
|
&& !contains_return(then_block.stmts)
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
use clippy_utils::ty::implements_trait;
|
use clippy_utils::ty::implements_trait;
|
||||||
use clippy_utils::visitors::is_const_evaluatable;
|
use clippy_utils::visitors::is_const_evaluatable;
|
||||||
use clippy_utils::{
|
use clippy_utils::{
|
||||||
eq_expr_value, in_constant, is_diag_trait_item, is_trait_method, path_res, path_to_local_id, peel_blocks,
|
eq_expr_value, is_diag_trait_item, is_in_const_context, is_trait_method, path_res, path_to_local_id, peel_blocks,
|
||||||
peel_blocks_with_stmt, MaybePath,
|
peel_blocks_with_stmt, MaybePath,
|
||||||
};
|
};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@ -146,7 +146,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
|||||||
if !self.msrv.meets(msrvs::CLAMP) {
|
if !self.msrv.meets(msrvs::CLAMP) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if !expr.span.from_expansion() && !in_constant(cx, expr.hir_id) {
|
if !expr.span.from_expansion() && !is_in_const_context(cx) {
|
||||||
let suggestion = is_if_elseif_else_pattern(cx, expr)
|
let suggestion = is_if_elseif_else_pattern(cx, expr)
|
||||||
.or_else(|| is_max_min_pattern(cx, expr))
|
.or_else(|| is_max_min_pattern(cx, expr))
|
||||||
.or_else(|| is_call_max_min_pattern(cx, expr))
|
.or_else(|| is_call_max_min_pattern(cx, expr))
|
||||||
@ -159,7 +159,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
|
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
|
||||||
if !self.msrv.meets(msrvs::CLAMP) || in_constant(cx, block.hir_id) {
|
if !self.msrv.meets(msrvs::CLAMP) || is_in_const_context(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
for suggestion in is_two_if_pattern(cx, block) {
|
for suggestion in is_two_if_pattern(cx, block) {
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_then;
|
use clippy_utils::diagnostics::span_lint_and_then;
|
||||||
use clippy_utils::macros::matching_root_macro_call;
|
use clippy_utils::macros::matching_root_macro_call;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::{higher, in_constant, path_to_local, peel_ref_operators};
|
use clippy_utils::{higher, is_in_const_context, path_to_local, peel_ref_operators};
|
||||||
use rustc_ast::ast::RangeLimits;
|
use rustc_ast::ast::RangeLimits;
|
||||||
use rustc_ast::LitKind::{Byte, Char};
|
use rustc_ast::LitKind::{Byte, Char};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
@ -95,7 +95,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if in_constant(cx, expr.hir_id) && !self.msrv.meets(msrvs::IS_ASCII_DIGIT_CONST) {
|
if is_in_const_context(cx) && !self.msrv.meets(msrvs::IS_ASCII_DIGIT_CONST) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use clippy_utils::consts::{constant_full_int, FullInt};
|
use clippy_utils::consts::{constant_full_int, FullInt};
|
||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::source::snippet_with_context;
|
use clippy_utils::source::snippet_with_context;
|
||||||
use clippy_utils::{in_constant, path_to_local};
|
use clippy_utils::{is_in_const_context, path_to_local};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind};
|
use rustc_hir::{BinOpKind, Expr, ExprKind, Node, TyKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||||
@ -62,7 +62,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
&& add_rhs.span.ctxt() == ctxt
|
&& add_rhs.span.ctxt() == ctxt
|
||||||
&& !in_external_macro(cx.sess(), expr.span)
|
&& !in_external_macro(cx.sess(), expr.span)
|
||||||
&& self.msrv.meets(msrvs::REM_EUCLID)
|
&& self.msrv.meets(msrvs::REM_EUCLID)
|
||||||
&& (self.msrv.meets(msrvs::REM_EUCLID_CONST) || !in_constant(cx, expr.hir_id))
|
&& (self.msrv.meets(msrvs::REM_EUCLID_CONST) || !is_in_const_context(cx))
|
||||||
&& let Some(const1) = check_for_unsigned_int_constant(cx, rem_rhs)
|
&& let Some(const1) = check_for_unsigned_int_constant(cx, rem_rhs)
|
||||||
&& let Some((const2, add_other)) = check_for_either_unsigned_int_constant(cx, add_lhs, add_rhs)
|
&& let Some((const2, add_other)) = check_for_either_unsigned_int_constant(cx, add_lhs, add_rhs)
|
||||||
&& let ExprKind::Binary(rem2_op, rem2_lhs, rem2_rhs) = add_other.kind
|
&& let ExprKind::Binary(rem2_op, rem2_lhs, rem2_rhs) = add_other.kind
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::source::snippet_with_context;
|
use clippy_utils::source::snippet_with_context;
|
||||||
use clippy_utils::{expr_or_init, in_constant, std_or_core};
|
use clippy_utils::{expr_or_init, is_in_const_context, std_or_core};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
use rustc_hir::{BinOpKind, Expr, ExprKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
@ -44,7 +44,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
|||||||
&& BinOpKind::Mul == op.node
|
&& BinOpKind::Mul == op.node
|
||||||
&& !expr.span.from_expansion()
|
&& !expr.span.from_expansion()
|
||||||
// Does not apply inside const because size_of_val is not cost in stable.
|
// Does not apply inside const because size_of_val is not cost in stable.
|
||||||
&& !in_constant(cx, expr.hir_id)
|
&& !is_in_const_context(cx)
|
||||||
&& let Some(receiver) = simplify(cx, left, right)
|
&& let Some(receiver) = simplify(cx, left, right)
|
||||||
{
|
{
|
||||||
let ctxt = expr.span.ctxt();
|
let ctxt = expr.span.ctxt();
|
||||||
|
@ -10,7 +10,7 @@
|
|||||||
use clippy_utils::higher::IfLetOrMatch;
|
use clippy_utils::higher::IfLetOrMatch;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::ty::implements_trait;
|
use clippy_utils::ty::implements_trait;
|
||||||
use clippy_utils::{in_constant, is_default_equivalent, peel_blocks, span_contains_comment};
|
use clippy_utils::{is_default_equivalent, is_in_const_context, peel_blocks, span_contains_comment};
|
||||||
|
|
||||||
declare_clippy_lint! {
|
declare_clippy_lint! {
|
||||||
/// ### What it does
|
/// ### What it does
|
||||||
@ -174,7 +174,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualUnwrapOrDefault {
|
|||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
|
||||||
if let Some(if_let_or_match) = IfLetOrMatch::parse(cx, expr)
|
if let Some(if_let_or_match) = IfLetOrMatch::parse(cx, expr)
|
||||||
&& !expr.span.from_expansion()
|
&& !expr.span.from_expansion()
|
||||||
&& !in_constant(cx, expr.hir_id)
|
&& !is_in_const_context(cx)
|
||||||
{
|
{
|
||||||
handle(cx, if_let_or_match, expr);
|
handle(cx, if_let_or_match, expr);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
use clippy_utils::macros::{is_panic, root_macro_call};
|
use clippy_utils::macros::{is_panic, root_macro_call};
|
||||||
use clippy_utils::ty::is_type_diagnostic_item;
|
use clippy_utils::ty::is_type_diagnostic_item;
|
||||||
use clippy_utils::visitors::is_local_used;
|
use clippy_utils::visitors::is_local_used;
|
||||||
use clippy_utils::{in_constant, is_wild, peel_blocks_with_stmt};
|
use clippy_utils::{is_in_const_context, is_wild, peel_blocks_with_stmt};
|
||||||
use rustc_hir::{Arm, Expr, PatKind};
|
use rustc_hir::{Arm, Expr, PatKind};
|
||||||
use rustc_lint::LateContext;
|
use rustc_lint::LateContext;
|
||||||
use rustc_span::symbol::{kw, sym};
|
use rustc_span::symbol::{kw, sym};
|
||||||
@ -11,7 +11,7 @@
|
|||||||
|
|
||||||
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'tcx>]) {
|
pub(crate) fn check<'tcx>(cx: &LateContext<'tcx>, ex: &Expr<'tcx>, arms: &[Arm<'tcx>]) {
|
||||||
// `unwrap`/`expect` is not (yet) const, so we want to allow this in const contexts for now
|
// `unwrap`/`expect` is not (yet) const, so we want to allow this in const contexts for now
|
||||||
if in_constant(cx, ex.hir_id) {
|
if is_in_const_context(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@
|
|||||||
use clippy_config::msrvs::{self, Msrv};
|
use clippy_config::msrvs::{self, Msrv};
|
||||||
use clippy_config::Conf;
|
use clippy_config::Conf;
|
||||||
use clippy_utils::source::walk_span_to_context;
|
use clippy_utils::source::walk_span_to_context;
|
||||||
use clippy_utils::{higher, in_constant, is_direct_expn_of, is_span_match, span_contains_cfg};
|
use clippy_utils::{higher, is_direct_expn_of, is_in_const_context, is_span_match, span_contains_cfg};
|
||||||
use rustc_hir::{Arm, Expr, ExprKind, LetStmt, MatchSource, Pat, PatKind};
|
use rustc_hir::{Arm, Expr, ExprKind, LetStmt, MatchSource, Pat, PatKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
use rustc_lint::{LateContext, LateLintPass, LintContext};
|
||||||
use rustc_middle::lint::in_external_macro;
|
use rustc_middle::lint::in_external_macro;
|
||||||
@ -1069,7 +1069,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
match_str_case_mismatch::check(cx, ex, arms);
|
match_str_case_mismatch::check(cx, ex, arms);
|
||||||
redundant_guards::check(cx, arms, &self.msrv);
|
redundant_guards::check(cx, arms, &self.msrv);
|
||||||
|
|
||||||
if !in_constant(cx, expr.hir_id) {
|
if !is_in_const_context(cx) {
|
||||||
manual_unwrap_or::check_match(cx, expr, ex, arms);
|
manual_unwrap_or::check_match(cx, expr, ex, arms);
|
||||||
manual_map::check_match(cx, expr, ex, arms);
|
manual_map::check_match(cx, expr, ex, arms);
|
||||||
manual_filter::check_match(cx, ex, arms, expr);
|
manual_filter::check_match(cx, ex, arms, expr);
|
||||||
@ -1098,7 +1098,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|||||||
else_expr,
|
else_expr,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
if !in_constant(cx, expr.hir_id) {
|
if !is_in_const_context(cx) {
|
||||||
manual_unwrap_or::check_if_let(
|
manual_unwrap_or::check_if_let(
|
||||||
cx,
|
cx,
|
||||||
expr,
|
expr,
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use clippy_utils::macros::matching_root_macro_call;
|
use clippy_utils::macros::matching_root_macro_call;
|
||||||
use clippy_utils::source::snippet;
|
use clippy_utils::source::snippet;
|
||||||
use clippy_utils::visitors::{for_each_expr_without_closures, is_local_used};
|
use clippy_utils::visitors::{for_each_expr_without_closures, is_local_used};
|
||||||
use clippy_utils::{in_constant, path_to_local};
|
use clippy_utils::{is_in_const_context, path_to_local};
|
||||||
use rustc_ast::{BorrowKind, LitKind};
|
use rustc_ast::{BorrowKind, LitKind};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::def::{DefKind, Res};
|
use rustc_hir::def::{DefKind, Res};
|
||||||
@ -116,7 +116,7 @@ fn check_method_calls<'tcx>(
|
|||||||
// `s if s.is_empty()` becomes ""
|
// `s if s.is_empty()` becomes ""
|
||||||
// `arr if arr.is_empty()` becomes []
|
// `arr if arr.is_empty()` becomes []
|
||||||
|
|
||||||
if ty.is_str() && !in_constant(cx, if_expr.hir_id) {
|
if ty.is_str() && !is_in_const_context(cx) {
|
||||||
r#""""#.into()
|
r#""""#.into()
|
||||||
} else if slice_like {
|
} else if slice_like {
|
||||||
"[]".into()
|
"[]".into()
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
use clippy_config::Conf;
|
use clippy_config::Conf;
|
||||||
use clippy_utils::diagnostics::span_lint_and_then;
|
use clippy_utils::diagnostics::span_lint_and_then;
|
||||||
use clippy_utils::in_constant;
|
use clippy_utils::is_in_const_context;
|
||||||
use clippy_utils::macros::macro_backtrace;
|
use clippy_utils::macros::macro_backtrace;
|
||||||
use clippy_utils::ty::{implements_trait, InteriorMut};
|
use clippy_utils::ty::{implements_trait, InteriorMut};
|
||||||
use rustc_hir::def::{DefKind, Res};
|
use rustc_hir::def::{DefKind, Res};
|
||||||
@ -406,7 +406,7 @@ fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<
|
|||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||||
if let ExprKind::Path(qpath) = &expr.kind {
|
if let ExprKind::Path(qpath) = &expr.kind {
|
||||||
// Only lint if we use the const item inside a function.
|
// Only lint if we use the const item inside a function.
|
||||||
if in_constant(cx, expr.hir_id) {
|
if is_in_const_context(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
use clippy_utils::diagnostics::span_lint_and_sugg;
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::{
|
use clippy_utils::{
|
||||||
can_move_expr_to_closure, eager_or_lazy, higher, in_constant, is_else_clause, is_res_lang_ctor, peel_blocks,
|
can_move_expr_to_closure, eager_or_lazy, higher, is_else_clause, is_in_const_context, is_res_lang_ctor,
|
||||||
peel_hir_expr_while, CaptureKind,
|
peel_blocks, peel_hir_expr_while, CaptureKind,
|
||||||
};
|
};
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::def::Res;
|
use rustc_hir::def::Res;
|
||||||
@ -294,7 +294,7 @@ fn is_none_or_err_arm(cx: &LateContext<'_>, arm: &Arm<'_>) -> bool {
|
|||||||
impl<'tcx> LateLintPass<'tcx> for OptionIfLetElse {
|
impl<'tcx> LateLintPass<'tcx> for OptionIfLetElse {
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
|
||||||
// Don't lint macros and constants
|
// Don't lint macros and constants
|
||||||
if expr.span.from_expansion() || in_constant(cx, expr.hir_id) {
|
if expr.span.from_expansion() || is_in_const_context(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,7 +7,7 @@
|
|||||||
use clippy_utils::source::snippet_with_applicability;
|
use clippy_utils::source::snippet_with_applicability;
|
||||||
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
|
use clippy_utils::ty::{implements_trait, is_type_diagnostic_item};
|
||||||
use clippy_utils::{
|
use clippy_utils::{
|
||||||
eq_expr_value, higher, in_constant, is_else_clause, is_lint_allowed, is_path_lang_item, is_res_lang_ctor,
|
eq_expr_value, higher, is_else_clause, is_in_const_context, is_lint_allowed, is_path_lang_item, is_res_lang_ctor,
|
||||||
pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
|
pat_and_expr_can_be_question_mark, path_to_local, path_to_local_id, peel_blocks, peel_blocks_with_stmt,
|
||||||
span_contains_comment,
|
span_contains_comment,
|
||||||
};
|
};
|
||||||
@ -346,15 +346,13 @@ fn check_stmt(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'_>) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if !self.inside_try_block() && !in_constant(cx, stmt.hir_id) {
|
if !self.inside_try_block() && !is_in_const_context(cx) {
|
||||||
check_let_some_else_return_none(cx, stmt);
|
check_let_some_else_return_none(cx, stmt);
|
||||||
}
|
}
|
||||||
self.check_manual_let_else(cx, stmt);
|
self.check_manual_let_else(cx, stmt);
|
||||||
}
|
}
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
||||||
if !self.inside_try_block()
|
if !self.inside_try_block() && !is_in_const_context(cx) && is_lint_allowed(cx, QUESTION_MARK_USED, expr.hir_id)
|
||||||
&& !in_constant(cx, expr.hir_id)
|
|
||||||
&& is_lint_allowed(cx, QUESTION_MARK_USED, expr.hir_id)
|
|
||||||
{
|
{
|
||||||
check_is_none_or_err_and_early_return(cx, expr);
|
check_is_none_or_err_and_early_return(cx, expr);
|
||||||
check_if_let_some_or_err_and_early_return(cx, expr);
|
check_if_let_some_or_err_and_early_return(cx, expr);
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
|
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
|
||||||
use clippy_utils::source::{snippet, snippet_with_applicability, SpanRangeExt};
|
use clippy_utils::source::{snippet, snippet_with_applicability, SpanRangeExt};
|
||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::{get_parent_expr, higher, in_constant, is_integer_const, path_to_local};
|
use clippy_utils::{get_parent_expr, higher, is_in_const_context, is_integer_const, path_to_local};
|
||||||
use rustc_ast::ast::RangeLimits;
|
use rustc_ast::ast::RangeLimits;
|
||||||
use rustc_errors::Applicability;
|
use rustc_errors::Applicability;
|
||||||
use rustc_hir::{BinOpKind, Expr, ExprKind, HirId};
|
use rustc_hir::{BinOpKind, Expr, ExprKind, HirId};
|
||||||
@ -202,7 +202,7 @@ fn check_possible_range_contains(
|
|||||||
expr: &Expr<'_>,
|
expr: &Expr<'_>,
|
||||||
span: Span,
|
span: Span,
|
||||||
) {
|
) {
|
||||||
if in_constant(cx, expr.hir_id) {
|
if is_in_const_context(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
use clippy_utils::sugg::Sugg;
|
use clippy_utils::sugg::Sugg;
|
||||||
use clippy_utils::ty::is_type_diagnostic_item;
|
use clippy_utils::ty::is_type_diagnostic_item;
|
||||||
|
|
||||||
use clippy_utils::{can_mut_borrow_both, eq_expr_value, in_constant, std_or_core};
|
use clippy_utils::{can_mut_borrow_both, eq_expr_value, is_in_const_context, std_or_core};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
|
|
||||||
use rustc_hir::intravisit::{walk_expr, Visitor};
|
use rustc_hir::intravisit::{walk_expr, Visitor};
|
||||||
@ -170,7 +170,7 @@ fn generate_swap_warning<'tcx>(
|
|||||||
|
|
||||||
/// Implementation of the `MANUAL_SWAP` lint.
|
/// Implementation of the `MANUAL_SWAP` lint.
|
||||||
fn check_manual_swap<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
|
fn check_manual_swap<'tcx>(cx: &LateContext<'tcx>, block: &'tcx Block<'tcx>) {
|
||||||
if in_constant(cx, block.hir_id) {
|
if is_in_const_context(cx) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
use clippy_config::msrvs::Msrv;
|
use clippy_config::msrvs::Msrv;
|
||||||
use clippy_config::Conf;
|
use clippy_config::Conf;
|
||||||
use clippy_utils::in_constant;
|
use clippy_utils::is_in_const_context;
|
||||||
use rustc_hir::{Expr, ExprKind, QPath};
|
use rustc_hir::{Expr, ExprKind, QPath};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::{LateContext, LateLintPass};
|
||||||
use rustc_session::impl_lint_pass;
|
use rustc_session::impl_lint_pass;
|
||||||
@ -595,7 +595,7 @@ fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
|||||||
// - from/to bits (https://github.com/rust-lang/rust/issues/73736)
|
// - from/to bits (https://github.com/rust-lang/rust/issues/73736)
|
||||||
// - dereferencing raw pointers (https://github.com/rust-lang/rust/issues/51911)
|
// - dereferencing raw pointers (https://github.com/rust-lang/rust/issues/51911)
|
||||||
// - char conversions (https://github.com/rust-lang/rust/issues/89259)
|
// - char conversions (https://github.com/rust-lang/rust/issues/89259)
|
||||||
let const_context = in_constant(cx, e.hir_id);
|
let const_context = is_in_const_context(cx);
|
||||||
|
|
||||||
let (from_ty, from_ty_adjusted) = match cx.typeck_results().expr_adjustments(arg) {
|
let (from_ty, from_ty_adjusted) = match cx.typeck_results().expr_adjustments(arg) {
|
||||||
[] => (cx.typeck_results().expr_ty(arg), false),
|
[] => (cx.typeck_results().expr_ty(arg), false),
|
||||||
|
@ -210,20 +210,24 @@ pub fn local_is_initialized(cx: &LateContext<'_>, local: HirId) -> bool {
|
|||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the given `HirId` is inside a constant context.
|
/// Checks if we are currently in a const context (e.g. `const fn`, `static`/`const` initializer).
|
||||||
///
|
///
|
||||||
/// This is the same as `is_inside_always_const_context`, but also includes
|
/// The current context is determined based on the current body which is set before calling a lint's
|
||||||
/// `const fn`.
|
/// entry point (any function on `LateLintPass`). If you need to check in a different context use
|
||||||
|
/// `tcx.hir().is_inside_const_context(_)`.
|
||||||
///
|
///
|
||||||
/// # Example
|
/// Do not call this unless the `LateContext` has an enclosing body. For release build this case
|
||||||
///
|
/// will safely return `false`, but debug builds will ICE. Note that `check_expr`, `check_block`,
|
||||||
/// ```rust,ignore
|
/// `check_pat` and a few other entry points will always have an enclosing body. Some entry points
|
||||||
/// if in_constant(cx, expr.hir_id) {
|
/// like `check_path` or `check_ty` may or may not have one.
|
||||||
/// // Do something
|
pub fn is_in_const_context(cx: &LateContext<'_>) -> bool {
|
||||||
/// }
|
debug_assert!(cx.enclosing_body.is_some(), "`LateContext` has no enclosing body");
|
||||||
/// ```
|
cx.enclosing_body.is_some_and(|id| {
|
||||||
pub fn in_constant(cx: &LateContext<'_>, id: HirId) -> bool {
|
cx.tcx
|
||||||
cx.tcx.hir().is_inside_const_context(id)
|
.hir()
|
||||||
|
.body_const_context(cx.tcx.hir().body_owner_def_id(id))
|
||||||
|
.is_some()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the given `HirId` is inside an always constant context.
|
/// Returns `true` if the given `HirId` is inside an always constant context.
|
||||||
|
Loading…
Reference in New Issue
Block a user