1 use if_chain::if_chain;
2 use rustc_hir::{ImplItem, ImplItemKind};
3 use rustc_lint::{LateContext, LateLintPass};
4 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 get_trait_def_id, is_type_diagnostic_item, implements_trait, paths, return_ty, span_lint_and_help, trait_ref_of_method,
11 declare_clippy_lint! {
12 /// **What it does:** Checks for the definition of inherent methods with a signature of `to_string(&self) -> String`.
14 /// **Why is this bad?** This method is also implicitly defined if a type implements the `Display` trait. As the functionality of `Display` is much more versatile, it should be preferred.
16 /// **Known problems:** None
25 /// pub fn to_string(&self) -> String {
26 /// "I am A".to_string()
37 /// impl fmt::Display for A {
38 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
39 /// write!(f, "I am A")
43 pub INHERENT_TO_STRING,
45 "type implements inherent method `to_string()`, but should instead implement the `Display` trait"
48 declare_clippy_lint! {
49 /// **What it does:** 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.
51 /// **Why is this bad?** 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`.
53 /// **Known problems:** None
64 /// pub fn to_string(&self) -> String {
65 /// "I am A".to_string()
69 /// impl fmt::Display for A {
70 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
71 /// write!(f, "I am A, too")
82 /// impl fmt::Display for A {
83 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84 /// write!(f, "I am A")
88 pub INHERENT_TO_STRING_SHADOW_DISPLAY,
90 "type implements inherent method `to_string()`, which gets shadowed by the implementation of the `Display` trait"
93 declare_lint_pass!(InherentToString => [INHERENT_TO_STRING, INHERENT_TO_STRING_SHADOW_DISPLAY]);
95 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InherentToString {
96 fn check_impl_item(&mut self, cx: &LateContext<'a, 'tcx>, impl_item: &'tcx ImplItem<'_>) {
97 if impl_item.span.from_expansion() {
102 // Check if item is a method, called to_string and has a parameter 'self'
103 if let ImplItemKind::Fn(ref signature, _) = impl_item.kind;
104 if impl_item.ident.name.as_str() == "to_string";
105 let decl = &signature.decl;
106 if decl.implicit_self.has_implicit_self();
107 if decl.inputs.len() == 1;
109 // Check if return type is String
110 if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym!(string_type));
112 // Filters instances of to_string which are required by a trait
113 if trait_ref_of_method(cx, impl_item.hir_id).is_none();
116 show_lint(cx, impl_item);
122 fn show_lint(cx: &LateContext<'_, '_>, item: &ImplItem<'_>) {
123 let display_trait_id = get_trait_def_id(cx, &paths::DISPLAY_TRAIT).expect("Failed to get trait ID of `Display`!");
125 // Get the real type of 'self'
126 let fn_def_id = cx.tcx.hir().local_def_id(item.hir_id);
127 let self_type = cx.tcx.fn_sig(fn_def_id).input(0);
128 let self_type = walk_ptrs_ty(self_type.skip_binder());
130 // Emit either a warning or an error
131 if implements_trait(cx, self_type, display_trait_id, &[]) {
134 INHERENT_TO_STRING_SHADOW_DISPLAY,
137 "type `{}` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`",
138 self_type.to_string()
141 &format!("remove the inherent method from type `{}`", self_type.to_string())
149 "implementation of inherent method `to_string(&self) -> String` for type `{}`",
150 self_type.to_string()
153 &format!("implement trait `Display` for type `{}` instead", self_type.to_string()),