]> git.lizzy.rs Git - rust.git/blob - src/docs/inherent_to_string_shadow_display.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / inherent_to_string_shadow_display.txt
1 ### What it does
2 Checks for the definition of inherent methods with a signature of `to_string(&self) -> String` and if the type implementing this method also implements the `Display` trait.
3
4 ### Why is this bad?
5 This method is also implicitly defined if a type implements the `Display` trait. The less versatile inherent method will then shadow the implementation introduced by `Display`.
6
7 ### Example
8 ```
9 use std::fmt;
10
11 pub struct A;
12
13 impl A {
14     pub fn to_string(&self) -> String {
15         "I am A".to_string()
16     }
17 }
18
19 impl fmt::Display for A {
20     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
21         write!(f, "I am A, too")
22     }
23 }
24 ```
25
26 Use instead:
27 ```
28 use std::fmt;
29
30 pub struct A;
31
32 impl fmt::Display for A {
33     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
34         write!(f, "I am A")
35     }
36 }
37 ```