Skip to content

Commit 496c110

Browse files
committed
Auto merge of rust-lang#10679 - y21:better-const-ctx-check, r=Jarcho
use `is_inside_const_context` for `in_constant` util fn Fixes rust-lang#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
2 parents 316d83a + 654d12f commit 496c110

File tree

4 files changed

+21
-31
lines changed

4 files changed

+21
-31
lines changed

clippy_utils/src/lib.rs

+4-28
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,10 @@ use rustc_hir::hir_id::{HirIdMap, HirIdSet};
8686
use rustc_hir::intravisit::{walk_expr, FnKind, Visitor};
8787
use rustc_hir::LangItem::{OptionNone, ResultErr, ResultOk};
8888
use rustc_hir::{
89-
self as hir, def, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Closure, Constness, Destination,
90-
Expr, ExprKind, FnDecl, HirId, Impl, ImplItem, ImplItemKind, ImplItemRef, IsAsync, Item, ItemKind, LangItem, Local,
89+
self as hir, def, Arm, ArrayLen, BindingAnnotation, Block, BlockCheckMode, Body, Closure, Destination, Expr,
90+
ExprKind, FnDecl, HirId, Impl, ImplItem, ImplItemKind, ImplItemRef, IsAsync, Item, ItemKind, LangItem, Local,
9191
MatchSource, Mutability, Node, OwnerId, Param, Pat, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind,
92-
TraitItem, TraitItemKind, TraitItemRef, TraitRef, TyKind, UnOp,
92+
TraitItem, TraitItemRef, TraitRef, TyKind, UnOp,
9393
};
9494
use rustc_lexer::{tokenize, TokenKind};
9595
use rustc_lint::{LateContext, Level, Lint, LintContext};
@@ -197,31 +197,7 @@ pub fn find_binding_init<'tcx>(cx: &LateContext<'tcx>, hir_id: HirId) -> Option<
197197
/// }
198198
/// ```
199199
pub fn in_constant(cx: &LateContext<'_>, id: HirId) -> bool {
200-
let parent_id = cx.tcx.hir().get_parent_item(id).def_id;
201-
match cx.tcx.hir().get_by_def_id(parent_id) {
202-
Node::Item(&Item {
203-
kind: ItemKind::Const(..) | ItemKind::Static(..) | ItemKind::Enum(..),
204-
..
205-
})
206-
| Node::TraitItem(&TraitItem {
207-
kind: TraitItemKind::Const(..),
208-
..
209-
})
210-
| Node::ImplItem(&ImplItem {
211-
kind: ImplItemKind::Const(..),
212-
..
213-
})
214-
| Node::AnonConst(_) => true,
215-
Node::Item(&Item {
216-
kind: ItemKind::Fn(ref sig, ..),
217-
..
218-
})
219-
| Node::ImplItem(&ImplItem {
220-
kind: ImplItemKind::Fn(ref sig, _),
221-
..
222-
}) => sig.header.constness == Constness::Const,
223-
_ => false,
224-
}
200+
cx.tcx.hir().is_inside_const_context(id)
225201
}
226202

227203
/// Checks if a `Res` refers to a constructor of a `LangItem`

tests/ui/bool_to_int_with_if.fixed

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22

3-
#![feature(let_chains)]
3+
#![feature(let_chains, inline_const)]
44
#![warn(clippy::bool_to_int_with_if)]
55
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
66

@@ -79,6 +79,13 @@ fn main() {
7979

8080
pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
8181

82+
// https://github.com/rust-lang/rust-clippy/issues/10452
83+
let should_not_lint = [(); if true { 1 } else { 0 }];
84+
85+
let should_not_lint = const {
86+
if true { 1 } else { 0 }
87+
};
88+
8289
some_fn(a);
8390
}
8491

tests/ui/bool_to_int_with_if.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//@run-rustfix
22

3-
#![feature(let_chains)]
3+
#![feature(let_chains, inline_const)]
44
#![warn(clippy::bool_to_int_with_if)]
55
#![allow(unused, dead_code, clippy::unnecessary_operation, clippy::no_effect)]
66

@@ -111,6 +111,13 @@ fn main() {
111111

112112
pub const SHOULD_NOT_LINT: usize = if true { 1 } else { 0 };
113113

114+
// https://github.com/rust-lang/rust-clippy/issues/10452
115+
let should_not_lint = [(); if true { 1 } else { 0 }];
116+
117+
let should_not_lint = const {
118+
if true { 1 } else { 0 }
119+
};
120+
114121
some_fn(a);
115122
}
116123

tests/ui/bool_to_int_with_if.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ LL | | };
9898
= note: `!b as i32` or `(!b).into()` can also be valid options
9999

100100
error: boolean to int conversion using if
101-
--> $DIR/bool_to_int_with_if.rs:119:5
101+
--> $DIR/bool_to_int_with_if.rs:126:5
102102
|
103103
LL | if a { 1 } else { 0 }
104104
| ^^^^^^^^^^^^^^^^^^^^^ help: replace with from: `u8::from(a)`

0 commit comments

Comments
 (0)