Skip to content

Commit 083300b

Browse files
committed
float_cmp: Allow named constants by path.
1 parent 63d5498 commit 083300b

File tree

15 files changed

+202
-137
lines changed

15 files changed

+202
-137
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6045,6 +6045,9 @@ Released 2018-09-13
60456045
[`enum-variant-name-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#enum-variant-name-threshold
60466046
[`enum-variant-size-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#enum-variant-size-threshold
60476047
[`excessive-nesting-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#excessive-nesting-threshold
6048+
[`float-cmp-allowed-constants`]: https://doc.rust-lang.org/clippy/lint_configuration.html#float-cmp-allowed-constants
6049+
[`float-cmp-ignore-change-detection`]: https://doc.rust-lang.org/clippy/lint_configuration.html#float-cmp-ignore-change-detection
6050+
[`float-cmp-ignore-constant-comparisons`]: https://doc.rust-lang.org/clippy/lint_configuration.html#float-cmp-ignore-constant-comparisons
60486051
[`future-size-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#future-size-threshold
60496052
[`ignore-interior-mutability`]: https://doc.rust-lang.org/clippy/lint_configuration.html#ignore-interior-mutability
60506053
[`large-error-threshold`]: https://doc.rust-lang.org/clippy/lint_configuration.html#large-error-threshold

book/src/lint_configuration.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,57 @@ The maximum amount of nesting a block can reside in
538538
* [`excessive_nesting`](https://rust-lang.github.io/rust-clippy/master/index.html#excessive_nesting)
539539

540540

541+
## `float-cmp-allowed-constants`
542+
0;
543+
fn is_value(x: f64) -> bool {
544+
// Will warn if the config is `false`
545+
x == VALUE
546+
}
547+
```
548+
549+
**Default Value:** `[]`
550+
551+
---
552+
**Affected lints:**
553+
* [`float_cmp`](https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp)
554+
555+
556+
## `float-cmp-ignore-change-detection`
557+
#### Example
558+
```no_run
559+
fn f(x: f64) -> bool {
560+
// Will warn if the config is `false`
561+
x == x + 1.0
562+
}
563+
```
564+
565+
**Default Value:** `true`
566+
567+
---
568+
**Affected lints:**
569+
* [`float_cmp`](https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp)
570+
571+
572+
## `float-cmp-ignore-constant-comparisons`
573+
#### Example
574+
```no_run
575+
const fn f(x: f64) -> f64 {
576+
todo!()
577+
}
578+
579+
// Will warn if the config is `false`
580+
if f(1.0) == f(2.0) {
581+
// ...
582+
}
583+
```
584+
585+
**Default Value:** `true`
586+
587+
---
588+
**Affected lints:**
589+
* [`float_cmp`](https://rust-lang.github.io/rust-clippy/master/index.html#float_cmp)
590+
591+
541592
## `future-size-threshold`
542593
The maximum byte size a `Future` can have, before it triggers the `clippy::large_futures` lint
543594

clippy_config/src/conf.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ define_Conf! {
657657
/// x == VALUE
658658
/// }
659659
/// ```
660-
(float_cmp_allowed_constants: Vec<String> = true),
660+
(float_cmp_allowed_constants: Vec<String> = Vec::new()),
661661
/// Lint: FLOAT_CMP
662662
///
663663
/// Whether to ignore comparisons which have a constant result.

clippy_lints/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,7 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
816816
store.register_late_pass(move |_| Box::new(manual_rem_euclid::ManualRemEuclid::new(conf)));
817817
store.register_late_pass(move |_| Box::new(manual_retain::ManualRetain::new(conf)));
818818
store.register_late_pass(move |_| Box::new(manual_rotate::ManualRotate));
819-
store.register_late_pass(move |_| Box::new(operators::Operators::new(conf)));
819+
store.register_late_pass(move |tcx| Box::new(operators::Operators::new(tcx, conf)));
820820
store.register_late_pass(|_| Box::<std_instead_of_core::StdReexports>::default());
821821
store.register_late_pass(move |_| Box::new(instant_subtraction::InstantSubtraction::new(conf)));
822822
store.register_late_pass(|_| Box::new(partialeq_to_none::PartialeqToNone));

clippy_lints/src/operators/float_cmp.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::consts::{constant, Constant};
22
use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::visitors::{for_each_expr_without_closures, is_const_evaluatable};
5-
use clippy_utils::{get_item_name, is_expr_named_const, path_res, peel_hir_expr_while, SpanlessEq};
5+
use clippy_utils::{get_item_name, get_named_const_def_id, path_res, peel_hir_expr_while, SpanlessEq};
66
use core::ops::ControlFlow;
77
use rustc_errors::Applicability;
88
use rustc_hir::def::Res;
@@ -14,7 +14,7 @@ use super::{FloatCmpConfig, FLOAT_CMP};
1414

1515
pub(crate) fn check<'tcx>(
1616
cx: &LateContext<'tcx>,
17-
config: FloatCmpConfig,
17+
config: &FloatCmpConfig,
1818
expr: &'tcx Expr<'_>,
1919
op: BinOpKind,
2020
left: &'tcx Expr<'_>,
@@ -56,8 +56,8 @@ pub(crate) fn check<'tcx>(
5656
return;
5757
}
5858

59-
if config.ignore_named_constants
60-
&& (is_expr_named_const(cx, left_reduced) || is_expr_named_const(cx, right_reduced))
59+
if get_named_const_def_id(cx, left_reduced).is_some_and(|id| config.allowed_constants.contains(&id))
60+
|| get_named_const_def_id(cx, right_reduced).is_some_and(|id| config.allowed_constants.contains(&id))
6161
{
6262
return;
6363
}

clippy_lints/src/operators/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use clippy_utils::def_path_def_ids;
2828
use rustc_hir::def_id::DefIdSet;
2929
use rustc_hir::{Body, Expr, ExprKind, UnOp};
3030
use rustc_lint::{LateContext, LateLintPass};
31+
use rustc_middle::ty::TyCtxt;
3132
use rustc_session::impl_lint_pass;
3233

3334
declare_clippy_lint! {
@@ -789,7 +790,7 @@ pub struct Operators {
789790
float_cmp_config: FloatCmpConfig,
790791
}
791792
impl Operators {
792-
pub fn new(conf: &'static Conf) -> Self {
793+
pub fn new(tcx: TyCtxt<'_>, conf: &'static Conf) -> Self {
793794
Self {
794795
arithmetic_context: numeric_arithmetic::Context::default(),
795796
verbose_bit_mask_threshold: conf.verbose_bit_mask_threshold,
@@ -859,7 +860,7 @@ impl<'tcx> LateLintPass<'tcx> for Operators {
859860
float_equality_without_abs::check(cx, e, op.node, lhs, rhs);
860861
integer_division::check(cx, e, op.node, lhs, rhs);
861862
cmp_owned::check(cx, op.node, lhs, rhs);
862-
float_cmp::check(cx, self.float_cmp_config, e, op.node, lhs, rhs);
863+
float_cmp::check(cx, &self.float_cmp_config, e, op.node, lhs, rhs);
863864
modulo_one::check(cx, e, op.node, rhs);
864865
modulo_arithmetic::check(
865866
cx,

clippy_utils/src/lib.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -245,14 +245,15 @@ pub fn is_inside_always_const_context(tcx: TyCtxt<'_>, hir_id: HirId) -> bool {
245245
}
246246
}
247247

248-
/// Checks if the expression is path to either a constant or an associated constant.
249-
pub fn is_expr_named_const<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> bool {
250-
matches!(&e.kind, ExprKind::Path(p)
251-
if matches!(
252-
cx.qpath_res(p, e.hir_id),
253-
Res::Def(DefKind::Const | DefKind::AssocConst, _)
254-
)
255-
)
248+
/// If the expression is path to either a constant or an associated constant get the `DefId`.
249+
pub fn get_named_const_def_id<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) -> Option<DefId> {
250+
if let ExprKind::Path(p) = &e.kind
251+
&& let Res::Def(DefKind::Const | DefKind::AssocConst, id) = cx.qpath_res(p, e.hir_id)
252+
{
253+
Some(id)
254+
} else {
255+
None
256+
}
256257
}
257258

258259
/// Checks if a `Res` refers to a constructor of a `LangItem`
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
float-cmp-ignore-change-detection = false
2+
float-cmp-allowed-constants = [
3+
"core::f32::EPSILON",
4+
"f32::EPSILON",
5+
"test::F32_ARRAY",
6+
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
float-cmp-ignore-constant-comparisons = false
2+
float-cmp-allowed-constants = [
3+
"core::f32::EPSILON",
4+
"f32::EPSILON",
5+
"test::F32_ARRAY",
6+
]
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +0,0 @@
1-
float-cmp-ignore-named-constants = false

0 commit comments

Comments
 (0)