Skip to content

Commit 2366661

Browse files
authored
Rollup merge of #4460 - JohnTitor:fix-inherent-to-string, r=flip1995
Fix `inherent_to_string` false positive Fixes #4457 changelog: fixes `inherent_to_string` false positive
2 parents ad43d68 + 1dca950 commit 2366661

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

clippy_lints/src/inherent_to_string.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InherentToString {
104104
if impl_item.ident.name.as_str() == "to_string";
105105
let decl = &signature.decl;
106106
if decl.implicit_self.has_implicit_self();
107+
if decl.inputs.len() == 1;
107108

108109
// Check if return type is String
109110
if match_type(cx, return_ty(cx, impl_item.hir_id), &paths::STRING);

tests/ui/inherent_to_string.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::inherent_to_string)]
22
#![deny(clippy::inherent_to_string_shadow_display)]
3+
#![allow(clippy::many_single_char_names)]
34

45
use std::fmt;
56

@@ -12,6 +13,7 @@ struct B;
1213
struct C;
1314
struct D;
1415
struct E;
16+
struct F;
1517

1618
impl A {
1719
// Should be detected; emit warning
@@ -64,6 +66,13 @@ impl E {
6466
}
6567
}
6668

69+
impl F {
70+
// Should not be detected, as it does not match the function signature
71+
fn to_string(&self, _i: i32) -> String {
72+
"F.to_string()".to_string()
73+
}
74+
}
75+
6776
fn main() {
6877
let a = A;
6978
a.to_string();
@@ -81,4 +90,7 @@ fn main() {
8190
d.to_string();
8291

8392
E::to_string();
93+
94+
let f = F;
95+
f.to_string(1);
8496
}

tests/ui/inherent_to_string.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: implementation of inherent method `to_string(&self) -> String` for type `A`
2-
--> $DIR/inherent_to_string.rs:18:5
2+
--> $DIR/inherent_to_string.rs:20:5
33
|
44
LL | / fn to_string(&self) -> String {
55
LL | | "A.to_string()".to_string()
@@ -10,7 +10,7 @@ LL | | }
1010
= help: implement trait `Display` for type `A` instead
1111

1212
error: type `C` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`
13-
--> $DIR/inherent_to_string.rs:42:5
13+
--> $DIR/inherent_to_string.rs:44:5
1414
|
1515
LL | / fn to_string(&self) -> String {
1616
LL | | "C.to_string()".to_string()

0 commit comments

Comments
 (0)