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};
8 get_trait_def_id, implements_trait, is_type_diagnostic_item, paths, return_ty, span_lint_and_help,
12 declare_clippy_lint! {
13 /// **What it does:** Checks for the definition of inherent methods with a signature of `to_string(&self) -> String`.
15 /// **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.
17 /// **Known problems:** None
26 /// pub fn to_string(&self) -> String {
27 /// "I am A".to_string()
38 /// impl fmt::Display for A {
39 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
40 /// write!(f, "I am A")
44 pub INHERENT_TO_STRING,
46 "type implements inherent method `to_string()`, but should instead implement the `Display` trait"
49 declare_clippy_lint! {
50 /// **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.
52 /// **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`.
54 /// **Known problems:** None
65 /// pub fn to_string(&self) -> String {
66 /// "I am A".to_string()
70 /// impl fmt::Display for A {
71 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
72 /// write!(f, "I am A, too")
83 /// impl fmt::Display for A {
84 /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
85 /// write!(f, "I am A")
89 pub INHERENT_TO_STRING_SHADOW_DISPLAY,
91 "type implements inherent method `to_string()`, which gets shadowed by the implementation of the `Display` trait"
94 declare_lint_pass!(InherentToString => [INHERENT_TO_STRING, INHERENT_TO_STRING_SHADOW_DISPLAY]);
96 impl<'tcx> LateLintPass<'tcx> for InherentToString {
97 fn check_impl_item(&mut self, cx: &LateContext<'tcx>, impl_item: &'tcx ImplItem<'_>) {
98 if impl_item.span.from_expansion() {
103 // Check if item is a method, called to_string and has a parameter 'self'
104 if let ImplItemKind::Fn(ref signature, _) = impl_item.kind;
105 if impl_item.ident.name.as_str() == "to_string";
106 let decl = &signature.decl;
107 if decl.implicit_self.has_implicit_self();
108 if decl.inputs.len() == 1;
110 // Check if return type is String
111 if is_type_diagnostic_item(cx, return_ty(cx, impl_item.hir_id), sym::string_type);
113 // Filters instances of to_string which are required by a trait
114 if trait_ref_of_method(cx, impl_item.hir_id).is_none();
117 show_lint(cx, impl_item);
123 fn show_lint(cx: &LateContext<'_>, item: &ImplItem<'_>) {
124 let display_trait_id = get_trait_def_id(cx, &paths::DISPLAY_TRAIT).expect("Failed to get trait ID of `Display`!");
126 // Get the real type of 'self'
127 let fn_def_id = cx.tcx.hir().local_def_id(item.hir_id);
128 let self_type = cx.tcx.fn_sig(fn_def_id).input(0);
129 let self_type = self_type.skip_binder().peel_refs();
131 // Emit either a warning or an error
132 if implements_trait(cx, self_type, display_trait_id, &[]) {
135 INHERENT_TO_STRING_SHADOW_DISPLAY,
138 "type `{}` implements inherent method `to_string(&self) -> String` which shadows the implementation of `Display`",
139 self_type.to_string()
142 &format!("remove the inherent method from type `{}`", self_type.to_string())
150 "implementation of inherent method `to_string(&self) -> String` for type `{}`",
151 self_type.to_string()
154 &format!("implement trait `Display` for type `{}` instead", self_type.to_string()),