Skip to content

Commit

Permalink
Auto merge of #10679 - y21:better-const-ctx-check, r=Jarcho
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Apr 23, 2023
2 parents 316d83a + 654d12f commit 496c110
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 31 deletions.
32 changes: 4 additions & 28 deletions clippy_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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`
Expand Down
9 changes: 8 additions & 1 deletion tests/ui/bool_to_int_with_if.fixed
Original file line number Diff line number Diff line change
@@ -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)]

Expand Down Expand Up @@ -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);
}

Expand Down
9 changes: 8 additions & 1 deletion tests/ui/bool_to_int_with_if.rs
Original file line number Diff line number Diff line change
@@ -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)]

Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/bool_to_int_with_if.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down

0 comments on commit 496c110

Please sign in to comment.