diff --git a/rules/S6164/cfamily/metadata.json b/rules/S6164/cfamily/metadata.json index 730b341938..536598314f 100644 --- a/rules/S6164/cfamily/metadata.json +++ b/rules/S6164/cfamily/metadata.json @@ -1,35 +1,8 @@ { - "title": "Mathematical constants should not be hardcoded", - "type": "CODE_SMELL", - "code": { - "impacts": { - "MAINTAINABILITY": "MEDIUM" - }, - "attribute": "CLEAR" - }, - "status": "ready", - "remediation": { - "func": "Constant\/Issue", - "constantCost": "5min" - }, "tags": [ "since-c++20", "clumsy" ], - "extra": { - "replacementRules": [ - - ], - "legacyKeys": [ - - ] - }, - "defaultSeverity": "Major", - "ruleSpecification": "RSPEC-6164", - "sqKey": "S6164", - "scope": "All", - "defaultQualityProfiles": [ - "Sonar way" - ], "quickfix": "unknown" } + \ No newline at end of file diff --git a/rules/S6164/metadata.json b/rules/S6164/metadata.json index 2c63c08510..16cf484a94 100644 --- a/rules/S6164/metadata.json +++ b/rules/S6164/metadata.json @@ -1,2 +1,34 @@ { + "title": "Mathematical constants should not be hardcoded", + "type": "CODE_SMELL", + "code": { + "impacts": { + "MAINTAINABILITY": "MEDIUM" + }, + "attribute": "CLEAR" + }, + "status": "ready", + "remediation": { + "func": "Constant\/Issue", + "constantCost": "5min" + }, + "tags": [ + "clumsy" + ], + "extra": { + "replacementRules": [ + + ], + "legacyKeys": [ + + ] + }, + "defaultSeverity": "Major", + "ruleSpecification": "RSPEC-6164", + "sqKey": "S6164", + "scope": "All", + "defaultQualityProfiles": [ + "Sonar way" + ], + "quickfix": "unknown" } diff --git a/rules/S6164/rust/metadata.json b/rules/S6164/rust/metadata.json new file mode 100644 index 0000000000..d5b290c212 --- /dev/null +++ b/rules/S6164/rust/metadata.json @@ -0,0 +1,7 @@ +{ + "tags": [ + "clippy", + "clumsy" + ], + "quickfix": "unknown" +} diff --git a/rules/S6164/rust/rule.adoc b/rules/S6164/rust/rule.adoc new file mode 100644 index 0000000000..0a7f9276b1 --- /dev/null +++ b/rules/S6164/rust/rule.adoc @@ -0,0 +1,40 @@ +== Why is this an issue? + +Floating-point literals that approximate constants defined in `std::f32::consts` or `std::f64::consts` should be replaced with the predefined constants. Using the standard library constants ensures higher precision and avoids potential rounding errors that can occur when manually approximating these values. It also improves code readability and maintainability, as it clearly indicates the intended constant value. + +=== Noncompliant code example + +[source,rust,diff-id=1,diff-type=noncompliant] +---- +let x = 3.14; // Noncompliant: Approximates PI +let y = 1_f64 / 3.1415926535; // Noncompliant: Approximates FRAC_1_PI +---- + +=== Compliant solution + +[source,rust,diff-id=1,diff-type=compliant] +---- +use std::f32::consts::PI; +use std::f64::consts::FRAC_1_PI; + +let x = PI; // Compliant: Uses the predefined PI constant +let y = FRAC_1_PI; // Compliant: Uses the predefined FRAC_1_PI constant +---- + +== Resources + +=== Documentation + +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#approx_constant + + +ifdef::env-github,rspecator-view[] +''' +== Comments And Links +(visible only on this page) + +=== is related to: S2904 + +=== is related to: S109 + +endif::env-github,rspecator-view[]