|
| 1 | +use crate::utils::{match_def_path, match_trait_method, paths, span_lint}; |
| 2 | +use if_chain::if_chain; |
| 3 | +use rustc_hir::*; |
| 4 | +use rustc_lint::{LateContext, LateLintPass}; |
| 5 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 6 | + |
| 7 | +declare_clippy_lint! { |
| 8 | + /// **What it does:** Checks for uses of `to_string()` in `Display` traits. |
| 9 | + /// |
| 10 | + /// **Why is this bad?** Usually `to_string` is implemented indirectly |
| 11 | + /// via `Display`. Hence using it while implementing `Display` would |
| 12 | + /// lead to infinite recursion. |
| 13 | + /// |
| 14 | + /// **Known problems:** None. |
| 15 | + /// |
| 16 | + /// **Example:** |
| 17 | + /// |
| 18 | + /// ```rust |
| 19 | + /// use std::fmt; |
| 20 | + /// |
| 21 | + /// struct Structure(i32); |
| 22 | + /// impl fmt::Display for Structure { |
| 23 | + /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 24 | + /// write!(f, "{}", self.to_string()) |
| 25 | + /// } |
| 26 | + /// } |
| 27 | + /// |
| 28 | + /// ``` |
| 29 | + /// Use instead: |
| 30 | + /// ```rust |
| 31 | + /// use std::fmt; |
| 32 | + /// |
| 33 | + /// struct Structure(i32); |
| 34 | + /// impl fmt::Display for Structure { |
| 35 | + /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 36 | + /// write!(f, "{}", self.0) |
| 37 | + /// } |
| 38 | + /// } |
| 39 | + /// ``` |
| 40 | + pub TO_STRING_IN_DISPLAY, |
| 41 | + correctness, |
| 42 | + "to_string method used while implementing Display trait" |
| 43 | +} |
| 44 | + |
| 45 | +#[derive(Default)] |
| 46 | +pub struct ToStringInDisplay { |
| 47 | + in_display_impl: bool, |
| 48 | +} |
| 49 | + |
| 50 | +impl ToStringInDisplay { |
| 51 | + pub fn new() -> Self { |
| 52 | + Self { in_display_impl: false } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +impl_lint_pass!(ToStringInDisplay => [TO_STRING_IN_DISPLAY]); |
| 57 | + |
| 58 | +impl LateLintPass<'_> for ToStringInDisplay { |
| 59 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 60 | + if is_display_impl(cx, item) { |
| 61 | + self.in_display_impl = true; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 66 | + if is_display_impl(cx, item) { |
| 67 | + self.in_display_impl = false; |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 72 | + let parent_id = cx.tcx.hir().get_parent_item(expr.hir_id); |
| 73 | + let parent_node = cx.tcx.hir().find(parent_id); |
| 74 | + |
| 75 | + if_chain! { |
| 76 | + if let ExprKind::MethodCall(ref path, _, _, _) = expr.kind; |
| 77 | + if path.ident.name == sym!(to_string); |
| 78 | + if match_trait_method(cx, expr, &paths::TO_STRING); |
| 79 | + if self.in_display_impl; |
| 80 | + |
| 81 | + then { |
| 82 | + span_lint( |
| 83 | + cx, |
| 84 | + TO_STRING_IN_DISPLAY, |
| 85 | + expr.span, |
| 86 | + "Using to_string in fmt::Display implementation might lead to infinite recursion", |
| 87 | + ); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +fn is_display_impl(cx: &LateContext<'_>, item: &Item<'_>) -> bool { |
| 94 | + if_chain! { |
| 95 | + if let ItemKind::Impl { of_trait: Some(trait_ref), .. } = &item.kind; |
| 96 | + if let Some(did) = trait_ref.trait_def_id(); |
| 97 | + if match_def_path(cx, did, &paths::DISPLAY_TRAIT); |
| 98 | + then { |
| 99 | + true |
| 100 | + } else { |
| 101 | + false |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments