Skip to content

Commit 5351170

Browse files
committed
Slightly refactor constant evaluation and add detection for empty macro expansion and cfged statements.
1 parent 0b3c2ed commit 5351170

32 files changed

+163
-126
lines changed

Diff for: clippy_lints/src/assertions_on_constants.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl<'tcx> LateLintPass<'tcx> for AssertionsOnConstants {
3838
_ => return,
3939
};
4040
let Some((condition, panic_expn)) = find_assert_args(cx, e, macro_call.expn) else { return };
41-
let Some((Constant::Bool(val), _)) = constant(cx, cx.typeck_results(), condition) else { return };
41+
let Some(Constant::Bool(val)) = constant(cx, cx.typeck_results(), condition) else { return };
4242
if val {
4343
span_lint_and_help(
4444
cx,

Diff for: clippy_lints/src/casts/cast_nan_to_int.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
2121

2222
fn is_known_nan(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
2323
match constant(cx, cx.typeck_results(), e) {
24-
Some((Constant::F64(n), _)) => n.is_nan(),
25-
Some((Constant::F32(n), _)) => n.is_nan(),
24+
Some(Constant::F64(n)) => n.is_nan(),
25+
Some(Constant::F32(n)) => n.is_nan(),
2626
_ => false,
2727
}
2828
}

Diff for: clippy_lints/src/casts/cast_possible_truncation.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_target::abi::IntegerType;
1515
use super::{utils, CAST_ENUM_TRUNCATION, CAST_POSSIBLE_TRUNCATION};
1616

1717
fn constant_int(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<u128> {
18-
if let Some((Constant::Int(c), _)) = constant(cx, cx.typeck_results(), expr) {
18+
if let Some(Constant::Int(c)) = constant(cx, cx.typeck_results(), expr) {
1919
Some(c)
2020
} else {
2121
None

Diff for: clippy_lints/src/casts/cast_sign_loss.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ fn should_lint(cx: &LateContext<'_>, cast_op: &Expr<'_>, cast_from: Ty<'_>, cast
2929
// Don't lint for positive constants.
3030
let const_val = constant(cx, cx.typeck_results(), cast_op);
3131
if_chain! {
32-
if let Some((Constant::Int(n), _)) = const_val;
32+
if let Some(Constant::Int(n)) = const_val;
3333
if let ty::Int(ity) = *cast_from.kind();
3434
if sext(cx.tcx, n, ity) >= 0;
3535
then {

Diff for: clippy_lints/src/floating_point_arithmetic.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ declare_lint_pass!(FloatingPointArithmetic => [
114114
// Returns the specialized log method for a given base if base is constant
115115
// and is one of 2, 10 and e
116116
fn get_specialized_log_method(cx: &LateContext<'_>, base: &Expr<'_>) -> Option<&'static str> {
117-
if let Some((value, _)) = constant(cx, cx.typeck_results(), base) {
117+
if let Some(value) = constant(cx, cx.typeck_results(), base) {
118118
if F32(2.0) == value || F64(2.0) == value {
119119
return Some("log2");
120120
} else if F32(10.0) == value || F64(10.0) == value {
@@ -193,8 +193,8 @@ fn check_ln1p(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>) {
193193
constant(cx, cx.typeck_results(), lhs),
194194
constant(cx, cx.typeck_results(), rhs),
195195
) {
196-
(Some((value, _)), _) if F32(1.0) == value || F64(1.0) == value => rhs,
197-
(_, Some((value, _))) if F32(1.0) == value || F64(1.0) == value => lhs,
196+
(Some(value), _) if F32(1.0) == value || F64(1.0) == value => rhs,
197+
(_, Some(value)) if F32(1.0) == value || F64(1.0) == value => lhs,
198198
_ => return,
199199
};
200200

@@ -237,7 +237,7 @@ fn get_integer_from_float_constant(value: &Constant) -> Option<i32> {
237237

238238
fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>]) {
239239
// Check receiver
240-
if let Some((value, _)) = constant(cx, cx.typeck_results(), receiver) {
240+
if let Some(value) = constant(cx, cx.typeck_results(), receiver) {
241241
if let Some(method) = if F32(f32_consts::E) == value || F64(f64_consts::E) == value {
242242
Some("exp")
243243
} else if F32(2.0) == value || F64(2.0) == value {
@@ -258,7 +258,7 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
258258
}
259259

260260
// Check argument
261-
if let Some((value, _)) = constant(cx, cx.typeck_results(), &args[0]) {
261+
if let Some(value) = constant(cx, cx.typeck_results(), &args[0]) {
262262
let (lint, help, suggestion) = if F32(1.0 / 2.0) == value || F64(1.0 / 2.0) == value {
263263
(
264264
SUBOPTIMAL_FLOPS,
@@ -298,7 +298,7 @@ fn check_powf(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args:
298298
}
299299

300300
fn check_powi(cx: &LateContext<'_>, expr: &Expr<'_>, receiver: &Expr<'_>, args: &[Expr<'_>]) {
301-
if let Some((value, _)) = constant(cx, cx.typeck_results(), &args[0]) {
301+
if let Some(value) = constant(cx, cx.typeck_results(), &args[0]) {
302302
if value == Int(2) {
303303
if let Some(parent) = get_parent_expr(cx, expr) {
304304
if let Some(grandparent) = get_parent_expr(cx, parent) {
@@ -384,8 +384,8 @@ fn detect_hypot(cx: &LateContext<'_>, receiver: &Expr<'_>) -> Option<String> {
384384
_
385385
) = &add_rhs.kind;
386386
if lmethod_name.as_str() == "powi" && rmethod_name.as_str() == "powi";
387-
if let Some((lvalue, _)) = constant(cx, cx.typeck_results(), largs_1);
388-
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), rargs_1);
387+
if let Some(lvalue) = constant(cx, cx.typeck_results(), largs_1);
388+
if let Some(rvalue) = constant(cx, cx.typeck_results(), rargs_1);
389389
if Int(2) == lvalue && Int(2) == rvalue;
390390
then {
391391
return Some(format!("{}.hypot({})", Sugg::hir(cx, largs_0, "..").maybe_par(), Sugg::hir(cx, rargs_0, "..")));
@@ -416,7 +416,7 @@ fn check_expm1(cx: &LateContext<'_>, expr: &Expr<'_>) {
416416
if_chain! {
417417
if let ExprKind::Binary(Spanned { node: BinOpKind::Sub, .. }, lhs, rhs) = expr.kind;
418418
if cx.typeck_results().expr_ty(lhs).is_floating_point();
419-
if let Some((value, _)) = constant(cx, cx.typeck_results(), rhs);
419+
if let Some(value) = constant(cx, cx.typeck_results(), rhs);
420420
if F32(1.0) == value || F64(1.0) == value;
421421
if let ExprKind::MethodCall(path, self_arg, ..) = &lhs.kind;
422422
if cx.typeck_results().expr_ty(self_arg).is_floating_point();
@@ -669,8 +669,8 @@ fn check_radians(cx: &LateContext<'_>, expr: &Expr<'_>) {
669669
mul_lhs,
670670
mul_rhs,
671671
) = &div_lhs.kind;
672-
if let Some((rvalue, _)) = constant(cx, cx.typeck_results(), div_rhs);
673-
if let Some((lvalue, _)) = constant(cx, cx.typeck_results(), mul_rhs);
672+
if let Some(rvalue) = constant(cx, cx.typeck_results(), div_rhs);
673+
if let Some(lvalue) = constant(cx, cx.typeck_results(), mul_rhs);
674674
then {
675675
// TODO: also check for constant values near PI/180 or 180/PI
676676
if (F32(f32_consts::PI) == rvalue || F64(f64_consts::PI) == rvalue) &&

Diff for: clippy_lints/src/fn_null_check.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,7 @@ impl<'tcx> LateLintPass<'tcx> for FnNullCheck {
8989

9090
// Catching:
9191
// (fn_ptr as *<const/mut> <ty>) == <const that evaluates to null_ptr>
92-
_ if matches!(
93-
constant(cx, cx.typeck_results(), to_check),
94-
Some((Constant::RawPtr(0), _))
95-
) =>
96-
{
92+
_ if matches!(constant(cx, cx.typeck_results(), to_check), Some(Constant::RawPtr(0))) => {
9793
lint_expr(cx, expr);
9894
},
9995

Diff for: clippy_lints/src/implicit_saturating_add.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,10 @@ fn get_int_max(ty: Ty<'_>) -> Option<u128> {
101101
fn get_const<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> Option<(u128, BinOpKind, &'tcx Expr<'tcx>)> {
102102
if let ExprKind::Binary(op, l, r) = expr.kind {
103103
let tr = cx.typeck_results();
104-
if let Some((Constant::Int(c), _)) = constant(cx, tr, r) {
104+
if let Some(Constant::Int(c)) = constant(cx, tr, r) {
105105
return Some((c, op.node, l));
106106
};
107-
if let Some((Constant::Int(c), _)) = constant(cx, tr, l) {
107+
if let Some(Constant::Int(c)) = constant(cx, tr, l) {
108108
return Some((c, invert_op(op.node)?, r));
109109
}
110110
}

Diff for: clippy_lints/src/index_refutable_slice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ impl<'a, 'tcx> Visitor<'tcx> for SliceIndexLintingVisitor<'a, 'tcx> {
254254
let parent_id = map.parent_id(expr.hir_id);
255255
if let Some(hir::Node::Expr(parent_expr)) = map.find(parent_id);
256256
if let hir::ExprKind::Index(_, index_expr) = parent_expr.kind;
257-
if let Some((Constant::Int(index_value), _)) = constant(cx, cx.typeck_results(), index_expr);
257+
if let Some(Constant::Int(index_value)) = constant(cx, cx.typeck_results(), index_expr);
258258
if let Ok(index_value) = index_value.try_into();
259259
if index_value < max_suggested_slice;
260260

Diff for: clippy_lints/src/indexing_slicing.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -191,18 +191,14 @@ impl<'tcx> LateLintPass<'tcx> for IndexingSlicing {
191191
/// Returns a tuple of options with the start and end (exclusive) values of
192192
/// the range. If the start or end is not constant, None is returned.
193193
fn to_const_range(cx: &LateContext<'_>, range: higher::Range<'_>, array_size: u128) -> (Option<u128>, Option<u128>) {
194-
let s = range
195-
.start
196-
.map(|expr| constant(cx, cx.typeck_results(), expr).map(|(c, _)| c));
194+
let s = range.start.map(|expr| constant(cx, cx.typeck_results(), expr));
197195
let start = match s {
198196
Some(Some(Constant::Int(x))) => Some(x),
199197
Some(_) => None,
200198
None => Some(0),
201199
};
202200

203-
let e = range
204-
.end
205-
.map(|expr| constant(cx, cx.typeck_results(), expr).map(|(c, _)| c));
201+
let e = range.end.map(|expr| constant(cx, cx.typeck_results(), expr));
206202
let end = match e {
207203
Some(Some(Constant::Int(x))) => {
208204
if range.limits == RangeLimits::Closed {

Diff for: clippy_lints/src/manual_strip.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ fn len_arg<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx E
144144

145145
// Returns the length of the `expr` if it's a constant string or char.
146146
fn constant_length(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<u128> {
147-
let (value, _) = constant(cx, cx.typeck_results(), expr)?;
147+
let value = constant(cx, cx.typeck_results(), expr)?;
148148
match value {
149149
Constant::Str(value) => Some(value.len() as u128),
150150
Constant::Char(value) => Some(value.len_utf8() as u128),

Diff for: clippy_lints/src/matches/overlapping_arms.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
3434
if let Arm { pat, guard: None, .. } = *arm {
3535
if let PatKind::Range(ref lhs, ref rhs, range_end) = pat.kind {
3636
let lhs_const = match lhs {
37-
Some(lhs) => constant(cx, cx.typeck_results(), lhs)?.0,
37+
Some(lhs) => constant(cx, cx.typeck_results(), lhs)?,
3838
None => {
3939
let min_val_const = ty.numeric_min_val(cx.tcx)?;
4040
let min_constant = mir::ConstantKind::from_value(
@@ -45,7 +45,7 @@ fn all_ranges<'tcx>(cx: &LateContext<'tcx>, arms: &'tcx [Arm<'_>], ty: Ty<'tcx>)
4545
},
4646
};
4747
let rhs_const = match rhs {
48-
Some(rhs) => constant(cx, cx.typeck_results(), rhs)?.0,
48+
Some(rhs) => constant(cx, cx.typeck_results(), rhs)?,
4949
None => {
5050
let max_val_const = ty.numeric_max_val(cx.tcx)?;
5151
let max_constant = mir::ConstantKind::from_value(

Diff for: clippy_lints/src/methods/iter_nth_zero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::ITER_NTH_ZERO;
1313
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, recv: &hir::Expr<'_>, arg: &hir::Expr<'_>) {
1414
if_chain! {
1515
if is_trait_method(cx, expr, sym::Iterator);
16-
if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), arg);
16+
if let Some(Constant::Int(0)) = constant(cx, cx.typeck_results(), arg);
1717
then {
1818
let mut applicability = Applicability::MachineApplicable;
1919
span_lint_and_sugg(

Diff for: clippy_lints/src/methods/iterator_step_by_zero.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::ITERATOR_STEP_BY_ZERO;
99

1010
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, arg: &'tcx hir::Expr<'_>) {
1111
if is_trait_method(cx, expr, sym::Iterator) {
12-
if let Some((Constant::Int(0), _)) = constant(cx, cx.typeck_results(), arg) {
12+
if let Some(Constant::Int(0)) = constant(cx, cx.typeck_results(), arg) {
1313
span_lint(
1414
cx,
1515
ITERATOR_STEP_BY_ZERO,

Diff for: clippy_lints/src/methods/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3770,13 +3770,13 @@ impl Methods {
37703770
unnecessary_sort_by::check(cx, expr, recv, arg, true);
37713771
},
37723772
("splitn" | "rsplitn", [count_arg, pat_arg]) => {
3773-
if let Some((Constant::Int(count), _)) = constant(cx, cx.typeck_results(), count_arg) {
3773+
if let Some(Constant::Int(count)) = constant(cx, cx.typeck_results(), count_arg) {
37743774
suspicious_splitn::check(cx, name, expr, recv, count);
37753775
str_splitn::check(cx, name, expr, recv, pat_arg, count, &self.msrv);
37763776
}
37773777
},
37783778
("splitn_mut" | "rsplitn_mut", [count_arg, _]) => {
3779-
if let Some((Constant::Int(count), _)) = constant(cx, cx.typeck_results(), count_arg) {
3779+
if let Some(Constant::Int(count)) = constant(cx, cx.typeck_results(), count_arg) {
37803780
suspicious_splitn::check(cx, name, expr, recv, count);
37813781
}
37823782
},

Diff for: clippy_lints/src/methods/repeat_once.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::consts::{constant_context, Constant};
1+
use clippy_utils::consts::{constant, Constant};
22
use clippy_utils::diagnostics::span_lint_and_sugg;
33
use clippy_utils::source::snippet;
44
use clippy_utils::ty::is_type_lang_item;
@@ -14,7 +14,7 @@ pub(super) fn check<'tcx>(
1414
recv: &'tcx Expr<'_>,
1515
repeat_arg: &'tcx Expr<'_>,
1616
) {
17-
if constant_context(cx, cx.typeck_results()).expr(repeat_arg) == Some(Constant::Int(1)) {
17+
if constant(cx, cx.typeck_results(), repeat_arg) == Some(Constant::Int(1)) {
1818
let ty = cx.typeck_results().expr_ty(recv).peel_refs();
1919
if ty.is_str() {
2020
span_lint_and_sugg(

Diff for: clippy_lints/src/methods/str_splitn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ fn parse_iter_usage<'tcx>(
316316
};
317317
},
318318
("nth" | "skip", [idx_expr]) if cx.tcx.trait_of_item(did) == Some(iter_id) => {
319-
if let Some((Constant::Int(idx), _)) = constant(cx, cx.typeck_results(), idx_expr) {
319+
if let Some(Constant::Int(idx)) = constant(cx, cx.typeck_results(), idx_expr) {
320320
let span = if name.ident.as_str() == "nth" {
321321
e.span
322322
} else {

Diff for: clippy_lints/src/operators/absurd_extreme_comparisons.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ fn detect_absurd_comparison<'tcx>(
121121
fn detect_extreme_expr<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<ExtremeExpr<'tcx>> {
122122
let ty = cx.typeck_results().expr_ty(expr);
123123

124-
let cv = constant(cx, cx.typeck_results(), expr)?.0;
124+
let cv = constant(cx, cx.typeck_results(), expr)?;
125125

126126
let which = match (ty.kind(), cv) {
127127
(&ty::Bool, Constant::Bool(false)) | (&ty::Uint(_), Constant::Int(0)) => ExtremeType::Minimum,

Diff for: clippy_lints/src/operators/arithmetic_side_effects.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl ArithmeticSideEffects {
113113
if let hir::ExprKind::Lit(lit) = actual.kind && let ast::LitKind::Int(n, _) = lit.node {
114114
return Some(n)
115115
}
116-
if let Some((Constant::Int(n), _)) = constant(cx, cx.typeck_results(), expr) {
116+
if let Some(Constant::Int(n)) = constant(cx, cx.typeck_results(), expr) {
117117
return Some(n);
118118
}
119119
None

Diff for: clippy_lints/src/operators/bit_mask.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ fn check_ineffective_gt(cx: &LateContext<'_>, span: Span, m: u128, c: u128, op:
166166
}
167167

168168
fn fetch_int_literal(cx: &LateContext<'_>, lit: &Expr<'_>) -> Option<u128> {
169-
match constant(cx, cx.typeck_results(), lit)?.0 {
169+
match constant(cx, cx.typeck_results(), lit)? {
170170
Constant::Int(n) => Some(n),
171171
_ => None,
172172
}

Diff for: clippy_lints/src/operators/cmp_nan.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(super) fn check(cx: &LateContext<'_>, e: &Expr<'_>, op: BinOpKind, lhs: &Exp
1818
}
1919

2020
fn is_nan(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
21-
if let Some((value, _)) = constant(cx, cx.typeck_results(), e) {
21+
if let Some(value) = constant(cx, cx.typeck_results(), e) {
2222
match value {
2323
Constant::F32(num) => num.is_nan(),
2424
Constant::F64(num) => num.is_nan(),

Diff for: clippy_lints/src/operators/duration_subsec.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ pub(crate) fn check<'tcx>(
1919
if op == BinOpKind::Div
2020
&& let ExprKind::MethodCall(method_path, self_arg, [], _) = left.kind
2121
&& is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(self_arg).peel_refs(), sym::Duration)
22-
&& let Some((Constant::Int(divisor), _)) = constant(cx, cx.typeck_results(), right)
22+
&& let Some(Constant::Int(divisor)) = constant(cx, cx.typeck_results(), right)
2323
{
2424
let suggested_fn = match (method_path.ident.as_str(), divisor) {
2525
("subsec_micros", 1_000) | ("subsec_nanos", 1_000_000) => "subsec_millis",

Diff for: clippy_lints/src/operators/float_cmp.rs

+25-32
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::consts::{constant, Constant};
1+
use clippy_utils::consts::{constant_with_source, Constant};
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::get_item_name;
44
use clippy_utils::sugg::Sugg;
@@ -18,9 +18,16 @@ pub(crate) fn check<'tcx>(
1818
right: &'tcx Expr<'_>,
1919
) {
2020
if (op == BinOpKind::Eq || op == BinOpKind::Ne) && (is_float(cx, left) || is_float(cx, right)) {
21-
if is_allowed(cx, left) || is_allowed(cx, right) {
22-
return;
23-
}
21+
let left_is_local = match constant_with_source(cx, cx.typeck_results(), left) {
22+
Some((c, s)) if !is_allowed(&c) => s.is_local(),
23+
Some(_) => return,
24+
None => true,
25+
};
26+
let right_is_local = match constant_with_source(cx, cx.typeck_results(), right) {
27+
Some((c, s)) if !is_allowed(&c) => s.is_local(),
28+
Some(_) => return,
29+
None => true,
30+
};
2431

2532
// Allow comparing the results of signum()
2633
if is_signum(cx, left) && is_signum(cx, right) {
@@ -34,10 +41,7 @@ pub(crate) fn check<'tcx>(
3441
}
3542
}
3643
let is_comparing_arrays = is_array(cx, left) || is_array(cx, right);
37-
let (lint, msg) = get_lint_and_message(
38-
is_named_constant(cx, left) || is_named_constant(cx, right),
39-
is_comparing_arrays,
40-
);
44+
let (lint, msg) = get_lint_and_message(left_is_local && right_is_local, is_comparing_arrays);
4145
span_lint_and_then(cx, lint, expr.span, msg, |diag| {
4246
let lhs = Sugg::hir(cx, left, "..");
4347
let rhs = Sugg::hir(cx, right, "..");
@@ -59,44 +63,33 @@ pub(crate) fn check<'tcx>(
5963
}
6064
}
6165

62-
fn get_lint_and_message(
63-
is_comparing_constants: bool,
64-
is_comparing_arrays: bool,
65-
) -> (&'static rustc_lint::Lint, &'static str) {
66-
if is_comparing_constants {
66+
fn get_lint_and_message(is_local: bool, is_comparing_arrays: bool) -> (&'static rustc_lint::Lint, &'static str) {
67+
if is_local {
6768
(
68-
FLOAT_CMP_CONST,
69+
FLOAT_CMP,
6970
if is_comparing_arrays {
70-
"strict comparison of `f32` or `f64` constant arrays"
71+
"strict comparison of `f32` or `f64` arrays"
7172
} else {
72-
"strict comparison of `f32` or `f64` constant"
73+
"strict comparison of `f32` or `f64`"
7374
},
7475
)
7576
} else {
7677
(
77-
FLOAT_CMP,
78+
FLOAT_CMP_CONST,
7879
if is_comparing_arrays {
79-
"strict comparison of `f32` or `f64` arrays"
80+
"strict comparison of `f32` or `f64` constant arrays"
8081
} else {
81-
"strict comparison of `f32` or `f64`"
82+
"strict comparison of `f32` or `f64` constant"
8283
},
8384
)
8485
}
8586
}
8687

87-
fn is_named_constant<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
88-
if let Some((_, res)) = constant(cx, cx.typeck_results(), expr) {
89-
res
90-
} else {
91-
false
92-
}
93-
}
94-
95-
fn is_allowed<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> bool {
96-
match constant(cx, cx.typeck_results(), expr) {
97-
Some((Constant::F32(f), _)) => f == 0.0 || f.is_infinite(),
98-
Some((Constant::F64(f), _)) => f == 0.0 || f.is_infinite(),
99-
Some((Constant::Vec(vec), _)) => vec.iter().all(|f| match f {
88+
fn is_allowed(val: &Constant) -> bool {
89+
match val {
90+
&Constant::F32(f) => f == 0.0 || f.is_infinite(),
91+
&Constant::F64(f) => f == 0.0 || f.is_infinite(),
92+
Constant::Vec(vec) => vec.iter().all(|f| match f {
10093
Constant::F32(f) => *f == 0.0 || (*f).is_infinite(),
10194
Constant::F64(f) => *f == 0.0 || (*f).is_infinite(),
10295
_ => false,

0 commit comments

Comments
 (0)