|
| 1 | +use clippy_utils::diagnostics::span_lint; |
| 2 | +use clippy_utils::higher::{FormatArgsArg, FormatArgsExpn}; |
| 3 | +use clippy_utils::{is_diag_trait_item, match_def_path, path_to_local_id, paths}; |
| 4 | +use if_chain::if_chain; |
| 5 | +use rustc_hir::{Expr, ExprKind, HirId, Impl, ImplItem, ImplItemKind, Item, ItemKind, UnOp}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 8 | +use rustc_span::{sym, ExpnData, ExpnKind, Symbol}; |
| 9 | + |
| 10 | +const FORMAT_MACRO_PATHS: &[&[&str]] = &[ |
| 11 | + &paths::FORMAT_ARGS_MACRO, |
| 12 | + &paths::ASSERT_EQ_MACRO, |
| 13 | + &paths::ASSERT_MACRO, |
| 14 | + &paths::ASSERT_NE_MACRO, |
| 15 | + &paths::EPRINT_MACRO, |
| 16 | + &paths::EPRINTLN_MACRO, |
| 17 | + &paths::PRINT_MACRO, |
| 18 | + &paths::PRINTLN_MACRO, |
| 19 | + &paths::WRITE_MACRO, |
| 20 | + &paths::WRITELN_MACRO, |
| 21 | +]; |
| 22 | + |
| 23 | +const FORMAT_MACRO_DIAG_ITEMS: &[Symbol] = &[sym::format_macro, sym::std_panic_macro]; |
| 24 | + |
| 25 | +fn outermost_expn_data(expn_data: ExpnData) -> ExpnData { |
| 26 | + if expn_data.call_site.from_expansion() { |
| 27 | + outermost_expn_data(expn_data.call_site.ctxt().outer_expn_data()) |
| 28 | + } else { |
| 29 | + expn_data |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +declare_clippy_lint! { |
| 34 | + /// ### What it does |
| 35 | + /// Checks for recursive use of `Display` trait inside its implementation. |
| 36 | + /// |
| 37 | + /// ### Why is this bad? |
| 38 | + /// This is unconditional recursion and so will lead to infinite |
| 39 | + /// recursion and a stack overflow. |
| 40 | + /// |
| 41 | + /// ### Example |
| 42 | + /// |
| 43 | + /// ```rust |
| 44 | + /// use std::fmt; |
| 45 | + /// |
| 46 | + /// struct Structure(i32); |
| 47 | + /// impl fmt::Display for Structure { |
| 48 | + /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 49 | + /// write!(f, "{}", self.to_string()) |
| 50 | + /// } |
| 51 | + /// } |
| 52 | + /// |
| 53 | + /// ``` |
| 54 | + /// Use instead: |
| 55 | + /// ```rust |
| 56 | + /// use std::fmt; |
| 57 | + /// |
| 58 | + /// struct Structure(i32); |
| 59 | + /// impl fmt::Display for Structure { |
| 60 | + /// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 61 | + /// write!(f, "{}", self.0) |
| 62 | + /// } |
| 63 | + /// } |
| 64 | + /// ``` |
| 65 | + #[clippy::version = "1.48.0"] |
| 66 | + pub RECURSIVE_DISPLAY_IMPL, |
| 67 | + correctness, |
| 68 | + "`Display` trait method called while implementing `Display` trait" |
| 69 | +} |
| 70 | + |
| 71 | +#[derive(Default)] |
| 72 | +pub struct RecursiveDisplayImpl { |
| 73 | + in_display_impl: bool, |
| 74 | + // hir_id of self parameter of method inside Display Impl - i.e. fmt(&self) |
| 75 | + self_hir_id: Option<HirId>, |
| 76 | +} |
| 77 | + |
| 78 | +impl RecursiveDisplayImpl { |
| 79 | + pub fn new() -> Self { |
| 80 | + Self { |
| 81 | + in_display_impl: false, |
| 82 | + self_hir_id: None, |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl_lint_pass!(RecursiveDisplayImpl => [RECURSIVE_DISPLAY_IMPL]); |
| 88 | + |
| 89 | +impl LateLintPass<'_> for RecursiveDisplayImpl { |
| 90 | + fn check_item(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 91 | + if is_display_impl(cx, item) { |
| 92 | + self.in_display_impl = true; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + fn check_item_post(&mut self, cx: &LateContext<'_>, item: &Item<'_>) { |
| 97 | + if is_display_impl(cx, item) { |
| 98 | + self.in_display_impl = false; |
| 99 | + self.self_hir_id = None; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) { |
| 104 | + if_chain! { |
| 105 | + // If we are in Display impl, then get hir_id for self in method impl - i.e. fmt(&self) |
| 106 | + if self.in_display_impl; |
| 107 | + if let ImplItemKind::Fn(.., body_id) = &impl_item.kind; |
| 108 | + let body = cx.tcx.hir().body(*body_id); |
| 109 | + if !body.params.is_empty(); |
| 110 | + then { |
| 111 | + let self_param = &body.params[0]; |
| 112 | + self.self_hir_id = Some(self_param.pat.hir_id); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 118 | + if_chain! { |
| 119 | + if self.in_display_impl; |
| 120 | + if let Some(self_hir_id) = self.self_hir_id; |
| 121 | + then { |
| 122 | + check_to_string_in_display(cx, expr, self_hir_id); |
| 123 | + check_self_in_format_args(cx, expr, self_hir_id); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +fn check_to_string_in_display(cx: &LateContext<'_>, expr: &Expr<'_>, self_hir_id: HirId) { |
| 130 | + if_chain! { |
| 131 | + // Get the hir_id of the object we are calling the method on |
| 132 | + if let ExprKind::MethodCall(path, _, [ref self_arg, ..], _) = expr.kind; |
| 133 | + // Is the method to_string() ? |
| 134 | + if path.ident.name == sym!(to_string); |
| 135 | + // Is the method a part of the ToString trait? (i.e. not to_string() implemented |
| 136 | + // separately) |
| 137 | + if let Some(expr_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id); |
| 138 | + if is_diag_trait_item(cx, expr_def_id, sym::ToString); |
| 139 | + // Is the method is called on self |
| 140 | + if path_to_local_id(self_arg, self_hir_id); |
| 141 | + then { |
| 142 | + span_lint( |
| 143 | + cx, |
| 144 | + RECURSIVE_DISPLAY_IMPL, |
| 145 | + expr.span, |
| 146 | + "using `to_string` in `fmt::Display` implementation might lead to infinite recursion", |
| 147 | + ); |
| 148 | + } |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +fn check_self_in_format_args(cx: &LateContext<'_>, expr: &Expr<'_>, self_hir_id: HirId) { |
| 153 | + // Check each arg in format calls - do we ever use Display on self (directly or via deref)? |
| 154 | + if_chain! { |
| 155 | + if let Some(format_args) = FormatArgsExpn::parse(expr); |
| 156 | + let expr_expn_data = expr.span.ctxt().outer_expn_data(); |
| 157 | + let outermost_expn_data = outermost_expn_data(expr_expn_data); |
| 158 | + if let Some(macro_def_id) = outermost_expn_data.macro_def_id; |
| 159 | + if FORMAT_MACRO_PATHS |
| 160 | + .iter() |
| 161 | + .any(|path| match_def_path(cx, macro_def_id, path)) |
| 162 | + || FORMAT_MACRO_DIAG_ITEMS |
| 163 | + .iter() |
| 164 | + .any(|diag_item| cx.tcx.is_diagnostic_item(*diag_item, macro_def_id)); |
| 165 | + if let ExpnKind::Macro(_, _name) = outermost_expn_data.kind; |
| 166 | + if let Some(args) = format_args.args(); |
| 167 | + then { |
| 168 | + for (_i, arg) in args.iter().enumerate() { |
| 169 | + // We only care about Display, okay to use Debug here |
| 170 | + if !arg.is_display() { |
| 171 | + continue; |
| 172 | + } |
| 173 | + check_format_arg_self(cx, expr, self_hir_id, arg); |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +fn check_format_arg_self(cx: &LateContext<'_>, expr: &Expr<'_>, self_hir_id: HirId, arg: &FormatArgsArg<'_>) { |
| 180 | + // Handle multiple dereferencing of references e.g. &&self |
| 181 | + // Handle single dereference of &self -> self that is equivalent (i.e. via *self in fmt() impl) |
| 182 | + // Since the argument to fmt is itself a reference: &self |
| 183 | + let reference = single_deref(deref_expr(arg.value)); |
| 184 | + if path_to_local_id(reference, self_hir_id) { |
| 185 | + span_lint( |
| 186 | + cx, |
| 187 | + RECURSIVE_DISPLAY_IMPL, |
| 188 | + expr.span, |
| 189 | + "using `self` in `fmt::Display` implementation might lead to infinite recursion", |
| 190 | + ); |
| 191 | + } |
| 192 | +} |
| 193 | + |
| 194 | +fn deref_expr<'a, 'b>(expr: &'a Expr<'b>) -> &'a Expr<'b> { |
| 195 | + if let ExprKind::AddrOf(_, _, reference) = expr.kind { |
| 196 | + deref_expr(reference) |
| 197 | + } else { |
| 198 | + expr |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +fn single_deref<'a, 'b>(expr: &'a Expr<'b>) -> &'a Expr<'b> { |
| 203 | + if let ExprKind::Unary(UnOp::Deref, ex) = expr.kind { |
| 204 | + ex |
| 205 | + } else { |
| 206 | + expr |
| 207 | + } |
| 208 | +} |
| 209 | + |
| 210 | +fn is_display_impl(cx: &LateContext<'_>, item: &'hir Item<'_>) -> bool { |
| 211 | + if_chain! { |
| 212 | + // Are we at an Impl? |
| 213 | + if let ItemKind::Impl(Impl { of_trait: Some(trait_ref), .. }) = &item.kind; |
| 214 | + if let Some(did) = trait_ref.trait_def_id(); |
| 215 | + then { |
| 216 | + // Is it for Display trait? |
| 217 | + match_def_path(cx, did, &paths::DISPLAY_TRAIT) |
| 218 | + } else { |
| 219 | + false |
| 220 | + } |
| 221 | + } |
| 222 | +} |
0 commit comments