2019-07-06 13:13:38 -05:00
|
|
|
#![warn(clippy::inherent_to_string)]
|
2019-07-16 11:29:37 -05:00
|
|
|
#![deny(clippy::inherent_to_string_shadow_display)]
|
2019-08-27 14:23:26 -05:00
|
|
|
#![allow(clippy::many_single_char_names)]
|
2019-07-06 13:13:38 -05:00
|
|
|
|
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
trait FalsePositive {
|
|
|
|
fn to_string(&self) -> String;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct A;
|
|
|
|
struct B;
|
|
|
|
struct C;
|
|
|
|
struct D;
|
|
|
|
struct E;
|
2019-08-27 14:23:26 -05:00
|
|
|
struct F;
|
2021-02-20 16:47:49 -06:00
|
|
|
struct G;
|
2019-07-06 13:13:38 -05:00
|
|
|
|
|
|
|
impl A {
|
|
|
|
// Should be detected; emit warning
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
"A.to_string()".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not be detected as it does not match the function signature
|
|
|
|
fn to_str(&self) -> String {
|
|
|
|
"A.to_str()".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should not be detected as it is a free function
|
|
|
|
fn to_string() -> String {
|
|
|
|
"free to_string()".to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
impl B {
|
|
|
|
// Should not be detected, wrong return type
|
|
|
|
fn to_string(&self) -> i32 {
|
|
|
|
42
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl C {
|
|
|
|
// Should be detected and emit error as C also implements Display
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
"C.to_string()".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for C {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(f, "impl Display for C")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FalsePositive for D {
|
|
|
|
// Should not be detected, as it is a trait function
|
|
|
|
fn to_string(&self) -> String {
|
|
|
|
"impl FalsePositive for D".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl E {
|
|
|
|
// Should not be detected, as it is not bound to an instance
|
|
|
|
fn to_string() -> String {
|
|
|
|
"E::to_string()".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 14:23:26 -05:00
|
|
|
impl F {
|
|
|
|
// Should not be detected, as it does not match the function signature
|
|
|
|
fn to_string(&self, _i: i32) -> String {
|
|
|
|
"F.to_string()".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-20 16:47:49 -06:00
|
|
|
impl G {
|
|
|
|
// Should not be detected, as it does not match the function signature
|
|
|
|
fn to_string<const _N: usize>(&self) -> String {
|
|
|
|
"G.to_string()".to_string()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-06 13:13:38 -05:00
|
|
|
fn main() {
|
|
|
|
let a = A;
|
|
|
|
a.to_string();
|
|
|
|
a.to_str();
|
|
|
|
|
|
|
|
to_string();
|
|
|
|
|
|
|
|
let b = B;
|
|
|
|
b.to_string();
|
|
|
|
|
|
|
|
let c = C;
|
|
|
|
C.to_string();
|
|
|
|
|
|
|
|
let d = D;
|
|
|
|
d.to_string();
|
|
|
|
|
|
|
|
E::to_string();
|
2019-08-27 14:23:26 -05:00
|
|
|
|
|
|
|
let f = F;
|
|
|
|
f.to_string(1);
|
2021-02-20 16:47:49 -06:00
|
|
|
|
|
|
|
let g = G;
|
|
|
|
g.to_string::<1>();
|
2019-07-06 13:13:38 -05:00
|
|
|
}
|