Create rule S7440: Formatting trait implementations should not be recursive (#4783)
* Create rule S7440 * 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
314d4b0ed2
commit
6919fdfd79
2
rules/S7440/metadata.json
Normal file
2
rules/S7440/metadata.json
Normal file
@ -0,0 +1,2 @@
|
||||
{
|
||||
}
|
24
rules/S7440/rust/metadata.json
Normal file
24
rules/S7440/rust/metadata.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"title": "Formatting trait implementations should not be recursive",
|
||||
"type": "BUG",
|
||||
"status": "ready",
|
||||
"remediation": {
|
||||
"func": "Constant\/Issue",
|
||||
"constantCost": "5min"
|
||||
},
|
||||
"tags": [
|
||||
"clippy"
|
||||
],
|
||||
"defaultSeverity": "Major",
|
||||
"ruleSpecification": "RSPEC-7440",
|
||||
"sqKey": "S7440",
|
||||
"scope": "All",
|
||||
"defaultQualityProfiles": ["Sonar way"],
|
||||
"quickfix": "unknown",
|
||||
"code": {
|
||||
"impacts": {
|
||||
"RELIABILITY": "MEDIUM"
|
||||
},
|
||||
"attribute": "LOGICAL"
|
||||
}
|
||||
}
|
38
rules/S7440/rust/rule.adoc
Normal file
38
rules/S7440/rust/rule.adoc
Normal file
@ -0,0 +1,38 @@
|
||||
== Why is this an issue?
|
||||
|
||||
Recursive calls in formatting trait implementations (e.g., `Display`) will lead to infinite recursion and a stack overflow.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,rust,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
use std::fmt;
|
||||
|
||||
struct Structure(i32);
|
||||
impl fmt::Display for Structure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.to_string()) // Noncompliant
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,rust,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
use std::fmt;
|
||||
|
||||
struct Structure(i32);
|
||||
impl fmt::Display for Structure {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.0) // Compliant
|
||||
}
|
||||
}
|
||||
----
|
||||
|
||||
== Resources
|
||||
=== Documentation
|
||||
|
||||
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#recursive_format_impl
|
Loading…
x
Reference in New Issue
Block a user