Compare commits
3 Commits
master
...
rule/add-R
Author | SHA1 | Date | |
---|---|---|---|
![]() |
1a5228300d | ||
![]() |
ed785116ab | ||
![]() |
2c65238661 |
2
rules/S7458/metadata.json
Normal file
2
rules/S7458/metadata.json
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
{
|
||||||
|
}
|
24
rules/S7458/rust/metadata.json
Normal file
24
rules/S7458/rust/metadata.json
Normal 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"
|
||||||
|
}
|
||||||
|
}
|
46
rules/S7458/rust/rule.adoc
Normal file
46
rules/S7458/rust/rule.adoc
Normal 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
|
Loading…
x
Reference in New Issue
Block a user