Compare commits

...

3 Commits

Author SHA1 Message Date
Gyula Sallai
1a5228300d
Update metadata.json 2025-03-26 14:09:47 +01:00
Gyula Sallai
ed785116ab
Update rule.adoc 2025-03-26 14:07:09 +01:00
sallaigy
2c65238661 Create rule S7458 2025-03-26 12:42:15 +00:00
3 changed files with 72 additions and 0 deletions

View File

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

View File

@ -0,0 +1,24 @@
{
"title": "Inherent shadowing definitions of to_string should be avoided",
"type": "BUG",
"status": "ready",
"remediation": {
"func": "Constant\/Issue",
"constantCost": "5min"
},
"tags": [
"clippy"
],
"defaultSeverity": "Major",
"ruleSpecification": "RSPEC-7458",
"sqKey": "S7458",
"scope": "All",
"defaultQualityProfiles": ["Sonar way"],
"quickfix": "unknown",
"code": {
"impacts": {
"RELIABILITY": "MEDIUM"
},
"attribute": "LOGICAL"
}
}

View File

@ -0,0 +1,46 @@
== Why is this an issue?
Defining an inherent `to_string(&self)` method on a type that also implements the `Display` trait can lead to confusion, as this inherent method will overshadow the `to_string` method automatically provided by the `Display` trait. The inherent method is less versatile, preventing proper use of the `Display` trait's feature.
=== Code examples
==== Noncompliant code example
[source,rust,diff-id=1,diff-type=noncompliant]
----
use std::fmt;
pub struct A;
impl A {
pub fn to_string(&self) -> String {
"I am A".to_string() // Noncompliant: Inherent method shadows `Display::to_string`.
}
}
impl fmt::Display for A {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "I am A, too")
}
}
----
==== Compliant solution
[source,rust,diff-id=1,diff-type=compliant]
----
use std::fmt;
pub struct A;
impl fmt::Display for A {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "I am A")
}
}
----
== Resources
=== Documentation
* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string_shadow_display