Create rule S2148: Underscores should be used to make large numbers readable (#4742)
* Add rust to rule S2148 * Update RSPEC --------- Co-authored-by: yassin-kammoun-sonarsource <yassin-kammoun-sonarsource@users.noreply.github.com> Co-authored-by: yassin-kammoun-sonarsource <yassin.kammoun@sonarsource.com>
This commit is contained in:
parent
6ca7dd2cc6
commit
ff68a34119
9
rules/S2148/rust/metadata.json
Normal file
9
rules/S2148/rust/metadata.json
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"tags": [
|
||||||
|
"convention",
|
||||||
|
"clippy"
|
||||||
|
],
|
||||||
|
"defaultQualityProfiles": [
|
||||||
|
"Sonar way"
|
||||||
|
]
|
||||||
|
}
|
35
rules/S2148/rust/rule.adoc
Normal file
35
rules/S2148/rust/rule.adoc
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
== Why is this an issue?
|
||||||
|
|
||||||
|
Long numbers, whether integral or floating-point, can be difficult to read and understand at a glance. This can lead to mistakes when reading, writing, or maintaining code. For example, it's easy to miscount the number of zeros in a large number or misplace a decimal point. These mistakes can introduce subtle bugs that are hard to detect and debug.
|
||||||
|
|
||||||
|
To improve readability, Rust allows the use of underscores as visual separators in numeric literals. By grouping digits into more manageable chunks, numbers become easier to read and understand.
|
||||||
|
|
||||||
|
This practice reduces the likelihood of errors and makes the code more maintainable.
|
||||||
|
|
||||||
|
== How to fix it
|
||||||
|
|
||||||
|
To fix this issue, use underscores to make numeric literals more explicit and easier to read.
|
||||||
|
|
||||||
|
=== Code examples
|
||||||
|
|
||||||
|
==== Noncompliant code example
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||||
|
----
|
||||||
|
let large_number = 1000000000;
|
||||||
|
let precise_float = 1234567.890123;
|
||||||
|
----
|
||||||
|
|
||||||
|
==== Compliant solution
|
||||||
|
|
||||||
|
[source,rust,diff-id=1,diff-type=compliant]
|
||||||
|
----
|
||||||
|
let large_number = 1_000_000_000;
|
||||||
|
let precise_float = 1_234_567.890_123;
|
||||||
|
----
|
||||||
|
|
||||||
|
== Resources
|
||||||
|
|
||||||
|
=== Documentation
|
||||||
|
|
||||||
|
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#unreadable_literal
|
Loading…
x
Reference in New Issue
Block a user