Auto merge of #10679 - y21:better-const-ctx-check, r=Jarcho

use `is_inside_const_context` for `in_constant` util fn

Fixes #10452.

This PR improves the `in_constant` util function to detect more cases of const contexts. Previously this function would not detect cases like expressions in array length position or expression in an inline const block `const { .. }`.

changelog: [`bool_to_int_with_if`]: recognize array length operand as being in a const context and don't suggest `usize::from` there
This commit is contained in:
bors 2023-04-23 13:33:51 +00:00
commit 496c11005c
4 changed files with 21 additions and 31 deletions

View file

@ -86,10 +86,10 @@ use rustc_hir::hir_id::{HirIdMap, HirIdSet};
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
use rustc_hir::LangItem::{OptionNone, ResultErr, ResultOk};
use rustc_hir::{
self as hir, def, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Closure, Constness, Destination,
Expr, ExprKind, FnDecl, HirId, Impl, ImplItem, ImplItemKind, ImplItemRef, IsAsync, Item, ItemKind, LangItem, Local,
self as hir, def, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Closure, Destination, Expr,
ExprKind, FnDecl, HirId, Impl, ImplItem, ImplItemKind, ImplItemRef, IsAsync, Item, ItemKind, LangItem, Local,
MatchSource, Mutability, Node, OwnerId, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind,
TraitItem, TraitItemKind, TraitItemRef, TraitRef, TyKind, UnOp,
TraitItem, TraitItemRef, TraitRef, TyKind, UnOp,
};
use rustc_lexer::{tokenize, TokenKind};
use rustc_lint::{LateContext, Level, Lint, LintContext};
@ -197,31 +197,7 @@ pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<
/// }
/// ```
pub fn in_constant(cx: &LateContext<'_>, id: HirId) -> bool {
let parent_id = cx.tcx.hir().get_parent_item(id).def_id;
match cx.tcx.hir().get_by_def_id(parent_id) {
Node::Item(&Item {
kind: ItemKind::Const(..) | ItemKind::Static(..) | ItemKind::Enum(..),
..
})
| Node::TraitItem(&TraitItem {
kind: TraitItemKind::Const(..),
..
})
| Node::ImplItem(&ImplItem {
kind: ImplItemKind::Const(..),
..
})
| Node::AnonConst(_) => true,
Node::Item(&Item {
kind: ItemKind::Fn(ref sig, ..),
..
})
| Node::ImplItem(&ImplItem {
kind: ImplItemKind::Fn(ref sig, _),
..
}) => sig.header.constness == Constness::Const,
_ => false,
}
cx.tcx.hir().is_inside_const_context(id)
}
/// Checks if a `Res` refers to a constructor of a `LangItem`

View file

@ -1,6 +1,6 @@
//@run-rustfix
#![feature(let_chains)]
#![feature(let_chains, inline_const)]
#![warn(clippy::bool_to_int_with_if)]
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
@ -79,6 +79,13 @@ fn main() {
pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
// https://github.com/rust-lang/rust-clippy/issues/10452
let should_not_lint = [(); if true { 1 } else { 0 }];
let should_not_lint = const {
if true { 1 } else { 0 }
};
some_fn(a);
}

View file

@ -1,6 +1,6 @@
//@run-rustfix
#![feature(let_chains)]
#![feature(let_chains, inline_const)]
#![warn(clippy::bool_to_int_with_if)]
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
@ -111,6 +111,13 @@ fn main() {
pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
// https://github.com/rust-lang/rust-clippy/issues/10452
let should_not_lint = [(); if true { 1 } else { 0 }];
let should_not_lint = const {
if true { 1 } else { 0 }
};
some_fn(a);
}

View file

@ -98,7 +98,7 @@ LL | | };
= note: `!b as i32` or `(!b).into()` can also be valid options
error: boolean to int conversion using if
--> $DIR/bool_to_int_with_if.rs:119:5
--> $DIR/bool_to_int_with_if.rs:126:5
|
LL | if a { 1 } else { 0 }
| ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(a)`