Skip to content

Commit 95f0a2e

Browse files
author
chansuke
committed
Use match_trait_method to detect an another method of struct
1 parent 11dadb8 commit 95f0a2e

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

clippy_lints/src/to_string_in_display.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::utils::{match_def_path, paths, span_lint};
1+
use crate::utils::{match_def_path, match_trait_method, paths, span_lint};
22
use if_chain::if_chain;
33
use rustc_hir::*;
44
use rustc_lint::{LateContext, LateLintPass};
@@ -51,6 +51,8 @@ impl LateLintPass<'_> for ToStringInDisplay {
5151

5252
if_chain! {
5353
if let ExprKind::MethodCall(ref path, _, _, _) = expr.kind;
54+
if path.ident.name == sym!(to_string)
55+
&& match_trait_method(cx, expr, &paths::TO_STRING);
5456
if path.ident.as_str() == "to_string";
5557
if let Some(Node::ImplItem(ImplItem {ident, kind, ..})) = parent_node;
5658
if ident.as_str() == "fmt";
@@ -63,6 +65,7 @@ impl LateLintPass<'_> for ToStringInDisplay {
6365
if let Path{res, ..} = path;
6466
if let def::Res::SelfTy(Some(did), _) = res;
6567
if match_def_path(cx, *did, &paths::DISPLAY_TRAIT);
68+
6669
then {
6770
span_lint(
6871
cx,

tests/ui/to_string_in_display.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,5 @@ LL | write!(f, "{}", self.to_string())
66
|
77
= note: `-D clippy::to-string-in-display` implied by `-D warnings`
88

9-
error: Using to_string in fmt::Display implementation might lead to infinite recursion
10-
--> $DIR/to_string_in_display.rs:43:25
11-
|
12-
LL | write!(f, "{}", self.to_string())
13-
| ^^^^^^^^^^^^^^^^
14-
15-
error: aborting due to 2 previous errors
9+
error: aborting due to previous error
1610

0 commit comments

Comments
 (0)