Create rule S7430: splitn should not be used with a limit of 0 or 1 (#4771)

* Create rule S7430

* 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 13:06:55 +00:00 committed by GitHub
parent fd50acb2e3
commit ef9ace8117
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 56 additions and 0 deletions

View File

@ -0,0 +1,2 @@
{
}

View File

@ -0,0 +1,24 @@
{
"title": "`splitn` should not be used with a limit of 0 or 1",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7430",
"sqKey": "S7430",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,30 @@
== Why is this an issue?
When using `splitn` with zero or one splits, the function does not actually split the value, which is likely not the intended behavior.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
let s = "";
for x in s.splitn(1, ":") { // Noncompliant
// ..
}
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
let s = "";
for x in s.splitn(2, ":") { // Compliant
// ..
}
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_splitn