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:
github-actions[bot] 2025-03-19 14:37:35 +01:00 committed by GitHub
parent 6ca7dd2cc6
commit ff68a34119
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 44 additions and 0 deletions

View File

@ -0,0 +1,9 @@
{
"tags": [
"convention",
"clippy"
],
"defaultQualityProfiles": [
"Sonar way"
]
}

View 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