From d17e141be353b4094c40cdbb95ba4c8661064d3a Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 19 Mar 2025 14:09:07 +0000 Subject: [PATCH] Create rule S2198: Unnecessary mathematical comparisons should not be made (#4692) * Add rust to rule S2198 * Add rule description * Update rule.adoc --------- Co-authored-by: sallaigy Co-authored-by: Gyula Sallai --- rules/S2198/rust/metadata.json | 7 +++++++ rules/S2198/rust/rule.adoc | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 rules/S2198/rust/metadata.json create mode 100644 rules/S2198/rust/rule.adoc diff --git a/rules/S2198/rust/metadata.json b/rules/S2198/rust/metadata.json new file mode 100644 index 0000000000..c4eea4b786 --- /dev/null +++ b/rules/S2198/rust/metadata.json @@ -0,0 +1,7 @@ +{ + "title": "Unnecessary mathematical comparisons should not be made", + "tags": [ + "clippy", + "suspicious" + ] +} \ No newline at end of file diff --git a/rules/S2198/rust/rule.adoc b/rules/S2198/rust/rule.adoc new file mode 100644 index 0000000000..41c117314e --- /dev/null +++ b/rules/S2198/rust/rule.adoc @@ -0,0 +1,18 @@ +== Why is this an issue? + +An expression like `min <= x` may misleadingly imply that it is possible for `x` to be less than the minimum. Expressions like `max < x` are likely mistakes, as they can never be true. These comparisons involving minimum or maximum values for their type with integer and boolean types are often unintentional and can lead to confusion and potentially incorrect program logic. + +=== Noncompliant code example + +[source,rust] +---- +let vec: Vec = Vec::new(); +if vec.len() <= 0 {} // Noncompliant: 0 is the minimum value for usize, this is always true. + +if 100 > i32::MAX {} // Noncompliant: i32::MAX is the maximum value for i32, this is always false. +---- + +== Resources +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#absurd_extreme_comparisons