diff options
| author | bors <bors@rust-lang.org> | 2020-08-16 16:21:37 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2020-08-16 16:21:37 +0000 |
| commit | e522ca3c8d980f51c521512a6740849bf78cdcbf (patch) | |
| tree | 49d5ffddc7bf6499276c9c67361467b088189be2 /tests | |
| parent | f8db258b224bb5616fb550a2a6b780beec9ff3c4 (diff) | |
| parent | 8e549978e58fe724c4637ab71808ff8785743c61 (diff) | |
| download | rust-e522ca3c8d980f51c521512a6740849bf78cdcbf.tar.gz rust-e522ca3c8d980f51c521512a6740849bf78cdcbf.zip | |
Auto merge of #5831 - chansuke:to_string_in_display, r=flip1995
Don't use `to_string` in impl Display fixes #3876 this PR is derived from [Toxyxer's implementation](https://github.com/rust-lang/rust-clippy/pull/5574). changelog: add [`to_string_in_display`] lint
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/ui/to_string_in_display.rs | 55 | ||||
| -rw-r--r-- | tests/ui/to_string_in_display.stderr | 10 |
2 files changed, 65 insertions, 0 deletions
diff --git a/tests/ui/to_string_in_display.rs b/tests/ui/to_string_in_display.rs new file mode 100644 index 00000000000..3b46324704e --- /dev/null +++ b/tests/ui/to_string_in_display.rs @@ -0,0 +1,55 @@ +#![warn(clippy::to_string_in_display)] +#![allow(clippy::inherent_to_string_shadow_display)] + +use std::fmt; + +struct A; +impl A { + fn fmt(&self) { + self.to_string(); + } +} + +trait B { + fn fmt(&self) {} +} + +impl B for A { + fn fmt(&self) { + self.to_string(); + } +} + +impl fmt::Display for A { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.to_string()) + } +} + +fn fmt(a: A) { + a.to_string(); +} + +struct C; + +impl C { + fn to_string(&self) -> String { + String::from("I am C") + } +} + +impl fmt::Display for C { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.to_string()) + } +} + +fn main() { + let a = A; + a.to_string(); + a.fmt(); + fmt(a); + + let c = C; + c.to_string(); +} diff --git a/tests/ui/to_string_in_display.stderr b/tests/ui/to_string_in_display.stderr new file mode 100644 index 00000000000..cbc0a41036b --- /dev/null +++ b/tests/ui/to_string_in_display.stderr @@ -0,0 +1,10 @@ +error: Using to_string in fmt::Display implementation might lead to infinite recursion + --> $DIR/to_string_in_display.rs:25:25 + | +LL | write!(f, "{}", self.to_string()) + | ^^^^^^^^^^^^^^^^ + | + = note: `-D clippy::to-string-in-display` implied by `-D warnings` + +error: aborting due to previous error + |
