Skip to content

Commit d6d5ecd

Browse files
committed
Auto merge of rust-lang#9571 - rust-lang:refactor-lit-ints, r=Jarcho
use `is_integer_literal` more I noticed that we have the `is_integer_literal` function in our `clippy_utils`, yet almost everywhere people still match int literal expressions manually. So I searched for instances to replace and shorten the code a bit. --- changelog: none
2 parents 5d837b5 + eef5d47 commit d6d5ecd

11 files changed

+52
-122
lines changed

clippy_lints/src/bool_to_int_with_if.rs

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::{Block, ExprKind};
33
use rustc_lint::{LateContext, LateLintPass};
44
use rustc_session::{declare_lint_pass, declare_tool_lint};
55

6-
use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, sugg::Sugg};
6+
use clippy_utils::{diagnostics::span_lint_and_then, is_else_clause, is_integer_literal, sugg::Sugg};
77
use rustc_errors::Applicability;
88

99
declare_clippy_lint! {
@@ -56,13 +56,9 @@ fn check_if_else<'tcx>(ctx: &LateContext<'tcx>, expr: &'tcx rustc_hir::Expr<'tcx
5656
&& let Some(then_lit) = int_literal(then)
5757
&& let Some(else_lit) = int_literal(else_)
5858
{
59-
let inverted = if
60-
check_int_literal_equals_val(then_lit, 1)
61-
&& check_int_literal_equals_val(else_lit, 0) {
59+
let inverted = if is_integer_literal(then_lit, 1) && is_integer_literal(else_lit, 0) {
6260
false
63-
} else if
64-
check_int_literal_equals_val(then_lit, 0)
65-
&& check_int_literal_equals_val(else_lit, 1) {
61+
} else if is_integer_literal(then_lit, 0) && is_integer_literal(else_lit, 1) {
6662
true
6763
} else {
6864
// Expression isn't boolean, exit
@@ -123,14 +119,3 @@ fn int_literal<'tcx>(expr: &'tcx rustc_hir::Expr<'tcx>) -> Option<&'tcx rustc_hi
123119
None
124120
}
125121
}
126-
127-
fn check_int_literal_equals_val<'tcx>(expr: &'tcx rustc_hir::Expr<'tcx>, expected_value: u128) -> bool {
128-
if let ExprKind::Lit(lit) = &expr.kind
129-
&& let LitKind::Int(val, _) = lit.node
130-
&& val == expected_value
131-
{
132-
true
133-
} else {
134-
false
135-
}
136-
}

clippy_lints/src/checked_conversions.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::source::snippet_with_applicability;
5-
use clippy_utils::{in_constant, meets_msrv, msrvs, SpanlessEq};
5+
use clippy_utils::{in_constant, is_integer_literal, meets_msrv, msrvs, SpanlessEq};
66
use if_chain::if_chain;
7-
use rustc_ast::ast::LitKind;
87
use rustc_errors::Applicability;
98
use rustc_hir::{BinOp, BinOpKind, Expr, ExprKind, QPath, TyKind};
109
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -223,16 +222,7 @@ fn check_lower_bound<'tcx>(expr: &'tcx Expr<'tcx>) -> Option<Conversion<'tcx>> {
223222

224223
/// Check for `expr >= 0`
225224
fn check_lower_bound_zero<'a>(candidate: &'a Expr<'_>, check: &'a Expr<'_>) -> Option<Conversion<'a>> {
226-
if_chain! {
227-
if let ExprKind::Lit(ref lit) = &check.kind;
228-
if let LitKind::Int(0, _) = &lit.node;
229-
230-
then {
231-
Some(Conversion::new_any(candidate))
232-
} else {
233-
None
234-
}
235-
}
225+
is_integer_literal(check, 0).then(|| Conversion::new_any(candidate))
236226
}
237227

238228
/// Check for `expr >= (to_type::MIN as from_type)`

clippy_lints/src/from_str_radix_10.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2+
use clippy_utils::is_integer_literal;
23
use clippy_utils::sugg::Sugg;
34
use clippy_utils::ty::is_type_diagnostic_item;
45
use if_chain::if_chain;
@@ -60,8 +61,7 @@ impl<'tcx> LateLintPass<'tcx> for FromStrRadix10 {
6061
if pathseg.ident.name.as_str() == "from_str_radix";
6162

6263
// check if the second argument is a primitive `10`
63-
if let ExprKind::Lit(lit) = &radix.kind;
64-
if let rustc_ast::ast::LitKind::Int(10, _) = lit.node;
64+
if is_integer_literal(radix, 10);
6565

6666
then {
6767
let expr = if let ExprKind::AddrOf(_, _, expr) = &src.kind {

clippy_lints/src/implicit_saturating_sub.rs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::{higher, peel_blocks_with_stmt, SpanlessEq};
2+
use clippy_utils::{higher, is_integer_literal, peel_blocks_with_stmt, SpanlessEq};
33
use if_chain::if_chain;
44
use rustc_ast::ast::LitKind;
55
use rustc_errors::Applicability;
@@ -131,17 +131,8 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitSaturatingSub {
131131
fn subtracts_one<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a Expr<'a>> {
132132
match peel_blocks_with_stmt(expr).kind {
133133
ExprKind::AssignOp(ref op1, target, value) => {
134-
if_chain! {
135-
if BinOpKind::Sub == op1.node;
136-
// Check if literal being subtracted is one
137-
if let ExprKind::Lit(ref lit1) = value.kind;
138-
if let LitKind::Int(1, _) = lit1.node;
139-
then {
140-
Some(target)
141-
} else {
142-
None
143-
}
144-
}
134+
// Check if literal being subtracted is one
135+
(BinOpKind::Sub == op1.node && is_integer_literal(value, 1)).then_some(target)
145136
},
146137
ExprKind::Assign(target, value, _) => {
147138
if_chain! {
@@ -150,8 +141,7 @@ fn subtracts_one<'a>(cx: &LateContext<'_>, expr: &'a Expr<'a>) -> Option<&'a Exp
150141

151142
if SpanlessEq::new(cx).eq_expr(left1, target);
152143

153-
if let ExprKind::Lit(ref lit1) = right1.kind;
154-
if let LitKind::Int(1, _) = lit1.node;
144+
if is_integer_literal(right1, 1);
155145
then {
156146
Some(target)
157147
} else {

clippy_lints/src/methods/get_last_with_len.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_applicability;
3-
use clippy_utils::SpanlessEq;
4-
use rustc_ast::LitKind;
3+
use clippy_utils::{is_integer_literal, SpanlessEq};
54
use rustc_errors::Applicability;
65
use rustc_hir::{BinOpKind, Expr, ExprKind};
76
use rustc_lint::LateContext;
@@ -26,8 +25,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, recv: &Expr<'_>, arg:
2625
&& lhs_path.ident.name == sym::len
2726

2827
// RHS of subtraction is 1
29-
&& let ExprKind::Lit(rhs_lit) = &rhs.kind
30-
&& let LitKind::Int(1, ..) = rhs_lit.node
28+
&& is_integer_literal(rhs, 1)
3129

3230
// check that recv == lhs_recv `recv.get(lhs_recv.len() - 1)`
3331
&& SpanlessEq::new(cx).eq_expr(recv, lhs_recv)

clippy_lints/src/misc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_hir_and_then};
22
use clippy_utils::source::{snippet, snippet_opt};
33
use if_chain::if_chain;
4-
use rustc_ast::ast::LitKind;
54
use rustc_errors::Applicability;
65
use rustc_hir::intravisit::FnKind;
76
use rustc_hir::{
@@ -15,7 +14,7 @@ use rustc_span::hygiene::DesugaringKind;
1514
use rustc_span::source_map::{ExpnKind, Span};
1615

1716
use clippy_utils::sugg::Sugg;
18-
use clippy_utils::{get_parent_expr, in_constant, iter_input_pats, last_path_segment, SpanlessEq};
17+
use clippy_utils::{get_parent_expr, in_constant, is_integer_literal, iter_input_pats, last_path_segment, SpanlessEq};
1918

2019
declare_clippy_lint! {
2120
/// ### What it does
@@ -314,8 +313,7 @@ fn non_macro_local(cx: &LateContext<'_>, res: def::Res) -> bool {
314313
fn check_cast(cx: &LateContext<'_>, span: Span, e: &Expr<'_>, ty: &hir::Ty<'_>) {
315314
if_chain! {
316315
if let TyKind::Ptr(ref mut_ty) = ty.kind;
317-
if let ExprKind::Lit(ref lit) = e.kind;
318-
if let LitKind::Int(0, _) = lit.node;
316+
if is_integer_literal(e, 0);
319317
if !in_constant(cx, e.hir_id);
320318
then {
321319
let (msg, sugg_fn) = match mut_ty.mutbl {

clippy_lints/src/operators/arithmetic_side_effects.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,19 @@ impl ArithmeticSideEffects {
4444
/// Assuming that `expr` is a literal integer, checks operators (+=, -=, *, /) in a
4545
/// non-constant environment that won't overflow.
4646
fn has_valid_op(op: &Spanned<hir::BinOpKind>, expr: &hir::Expr<'_>) -> bool {
47-
if let hir::BinOpKind::Add | hir::BinOpKind::Sub = op.node
48-
&& let hir::ExprKind::Lit(ref lit) = expr.kind
49-
&& let ast::LitKind::Int(0, _) = lit.node
47+
if let hir::ExprKind::Lit(ref lit) = expr.kind &&
48+
let ast::LitKind::Int(value, _) = lit.node
5049
{
51-
return true;
52-
}
53-
if let hir::BinOpKind::Div | hir::BinOpKind::Rem = op.node
54-
&& let hir::ExprKind::Lit(ref lit) = expr.kind
55-
&& !matches!(lit.node, ast::LitKind::Int(0, _))
56-
{
57-
return true;
58-
}
59-
if let hir::BinOpKind::Mul = op.node
60-
&& let hir::ExprKind::Lit(ref lit) = expr.kind
61-
&& let ast::LitKind::Int(0 | 1, _) = lit.node
62-
{
63-
return true;
50+
match (&op.node, value) {
51+
(hir::BinOpKind::Add | hir::BinOpKind::Sub, 0) |
52+
(hir::BinOpKind::Mul, 0 | 1) => true,
53+
(hir::BinOpKind::Div | hir::BinOpKind::Rem, 0) => false,
54+
(hir::BinOpKind::Div | hir::BinOpKind::Rem, _) => true,
55+
_ => false,
56+
}
57+
} else {
58+
false
6459
}
65-
false
6660
}
6761

6862
/// Checks if the given `expr` has any of the inner `allowed` elements.

clippy_lints/src/operators/numeric_arithmetic.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::consts::constant_simple;
22
use clippy_utils::diagnostics::span_lint;
3+
use clippy_utils::is_integer_literal;
34
use rustc_hir as hir;
45
use rustc_lint::LateContext;
56
use rustc_span::source_map::Span;
@@ -50,11 +51,9 @@ impl Context {
5051
hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
5152
hir::ExprKind::Lit(_lit) => (),
5253
hir::ExprKind::Unary(hir::UnOp::Neg, expr) => {
53-
if let hir::ExprKind::Lit(lit) = &expr.kind {
54-
if let rustc_ast::ast::LitKind::Int(1, _) = lit.node {
55-
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
56-
self.expr_id = Some(expr.hir_id);
57-
}
54+
if is_integer_literal(expr, 1) {
55+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
56+
self.expr_id = Some(expr.hir_id);
5857
}
5958
},
6059
_ => {

clippy_lints/src/slow_vector_initialization.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::sugg::Sugg;
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{get_enclosing_block, is_expr_path_def_path, path_to_local, path_to_local_id, paths, SpanlessEq};
4+
use clippy_utils::{
5+
get_enclosing_block, is_expr_path_def_path, is_integer_literal, path_to_local, path_to_local_id, paths, SpanlessEq,
6+
};
57
use if_chain::if_chain;
6-
use rustc_ast::ast::LitKind;
78
use rustc_errors::Applicability;
89
use rustc_hir::intravisit::{walk_block, walk_expr, walk_stmt, Visitor};
910
use rustc_hir::{BindingAnnotation, Block, Expr, ExprKind, HirId, PatKind, QPath, Stmt, StmtKind};
@@ -219,8 +220,7 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
219220
&& path_to_local_id(self_arg, self.vec_alloc.local_id)
220221
&& path.ident.name == sym!(resize)
221222
// Check that is filled with 0
222-
&& let ExprKind::Lit(ref lit) = fill_arg.kind
223-
&& let LitKind::Int(0, _) = lit.node {
223+
&& is_integer_literal(fill_arg, 0) {
224224
// Check that len expression is equals to `with_capacity` expression
225225
if SpanlessEq::new(self.cx).eq_expr(len_arg, self.vec_alloc.len_expr) {
226226
self.slow_expression = Some(InitializationType::Resize(expr));
@@ -255,9 +255,7 @@ impl<'a, 'tcx> VectorInitializationVisitor<'a, 'tcx> {
255255
if_chain! {
256256
if let ExprKind::Call(fn_expr, [repeat_arg]) = expr.kind;
257257
if is_expr_path_def_path(self.cx, fn_expr, &paths::ITER_REPEAT);
258-
if let ExprKind::Lit(ref lit) = repeat_arg.kind;
259-
if let LitKind::Int(0, _) = lit.node;
260-
258+
if is_integer_literal(repeat_arg, 0);
261259
then {
262260
true
263261
} else {

clippy_lints/src/transmute/transmuting_null.rs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use clippy_utils::consts::{constant_context, Constant};
22
use clippy_utils::diagnostics::span_lint;
3-
use clippy_utils::is_path_diagnostic_item;
4-
use if_chain::if_chain;
5-
use rustc_ast::LitKind;
3+
use clippy_utils::{is_integer_literal, is_path_diagnostic_item};
64
use rustc_hir::{Expr, ExprKind};
75
use rustc_lint::LateContext;
86
use rustc_middle::ty::Ty;
@@ -19,37 +17,28 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, arg: &'t
1917

2018
// Catching transmute over constants that resolve to `null`.
2119
let mut const_eval_context = constant_context(cx, cx.typeck_results());
22-
if_chain! {
23-
if let ExprKind::Path(ref _qpath) = arg.kind;
24-
if let Some(Constant::RawPtr(x)) = const_eval_context.expr(arg);
25-
if x == 0;
26-
then {
27-
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG);
28-
return true;
29-
}
20+
if let ExprKind::Path(ref _qpath) = arg.kind &&
21+
let Some(Constant::RawPtr(x)) = const_eval_context.expr(arg) &&
22+
x == 0
23+
{
24+
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG);
25+
return true;
3026
}
3127

3228
// Catching:
3329
// `std::mem::transmute(0 as *const i32)`
34-
if_chain! {
35-
if let ExprKind::Cast(inner_expr, _cast_ty) = arg.kind;
36-
if let ExprKind::Lit(ref lit) = inner_expr.kind;
37-
if let LitKind::Int(0, _) = lit.node;
38-
then {
39-
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG);
40-
return true;
41-
}
30+
if let ExprKind::Cast(inner_expr, _cast_ty) = arg.kind && is_integer_literal(inner_expr, 0) {
31+
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG);
32+
return true;
4233
}
4334

4435
// Catching:
4536
// `std::mem::transmute(std::ptr::null::<i32>())`
46-
if_chain! {
47-
if let ExprKind::Call(func1, []) = arg.kind;
48-
if is_path_diagnostic_item(cx, func1, sym::ptr_null);
49-
then {
50-
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG);
51-
return true;
52-
}
37+
if let ExprKind::Call(func1, []) = arg.kind &&
38+
is_path_diagnostic_item(cx, func1, sym::ptr_null)
39+
{
40+
span_lint(cx, TRANSMUTING_NULL, expr.span, LINT_MSG);
41+
return true;
5342
}
5443

5544
// FIXME:

clippy_lints/src/uninit_vec.rs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_then};
22
use clippy_utils::higher::{get_vec_init_kind, VecInitKind};
33
use clippy_utils::ty::{is_type_diagnostic_item, is_uninit_value_valid_for_ty};
4-
use clippy_utils::{is_lint_allowed, path_to_local_id, peel_hir_expr_while, SpanlessEq};
5-
use rustc_ast::ast::LitKind;
4+
use clippy_utils::{is_integer_literal, is_lint_allowed, path_to_local_id, peel_hir_expr_while, SpanlessEq};
65
use rustc_hir::{Block, Expr, ExprKind, HirId, PatKind, PathSegment, Stmt, StmtKind};
76
use rustc_lint::{LateContext, LateLintPass};
87
use rustc_middle::lint::in_external_macro;
@@ -216,7 +215,7 @@ fn extract_set_len_self<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Opt
216215
let self_type = cx.typeck_results().expr_ty(self_expr).peel_refs();
217216
if is_type_diagnostic_item(cx, self_type, sym::Vec)
218217
&& path.ident.name.as_str() == "set_len"
219-
&& !is_literal_zero(arg)
218+
&& !is_integer_literal(arg, 0)
220219
{
221220
Some((self_expr, expr.span))
222221
} else {
@@ -226,13 +225,3 @@ fn extract_set_len_self<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Opt
226225
_ => None,
227226
}
228227
}
229-
230-
fn is_literal_zero(arg: &Expr<'_>) -> bool {
231-
if let ExprKind::Lit(lit) = &arg.kind
232-
&& let LitKind::Int(0, _) = lit.node
233-
{
234-
true
235-
} else {
236-
false
237-
}
238-
}

0 commit comments

Comments
 (0)