diff --git a/rules/S7458/rust/rule.adoc b/rules/S7458/rust/rule.adoc index 368fb2954e..8113676171 100644 --- a/rules/S7458/rust/rule.adoc +++ b/rules/S7458/rust/rule.adoc @@ -1,44 +1,46 @@ -FIXME: add a description - -// If you want to factorize the description uncomment the following line and create the file. -//include::../description.adoc[] == 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. -FIXME: remove the unused optional headers (that are commented out) - -//=== What is the potential impact? - -== How to fix it -//== How to fix it in FRAMEWORK NAME === Code examples ==== Noncompliant code example - [source,rust,diff-id=1,diff-type=noncompliant] ---- -FIXME +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] ---- -FIXME +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") + } +} ---- -//=== How does this work? +== Resources +=== Documentation -//=== Pitfalls - -//=== Going the extra mile - - -//== Resources -//=== Documentation -//=== Articles & blog posts -//=== Conference presentations -//=== Standards -//=== External coding guidelines -//=== Benchmarks +* Clippy Lints - https://rust-lang.github.io/rust-clippy/master/index.html#inherent_to_string_shadow_display