|
| 1 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 2 | +use clippy_utils::macros::root_macro_call_first_node; |
| 3 | +use clippy_utils::{get_parent_as_impl, match_any_diagnostic_items}; |
| 4 | +use rustc_errors::Applicability; |
| 5 | +use rustc_hir::{Expr, Impl, ImplItem, ImplItemKind}; |
| 6 | +use rustc_lint::{LateContext, LateLintPass}; |
| 7 | +use rustc_session::{declare_tool_lint, impl_lint_pass}; |
| 8 | +use rustc_span::{sym, Symbol}; |
| 9 | + |
| 10 | +declare_clippy_lint! { |
| 11 | + /// ### What it does |
| 12 | + /// Checks for use of `println`, `print`, `eprintln` or `eprint` in an |
| 13 | + /// implementation of a formatting trait. |
| 14 | + /// |
| 15 | + /// ### Why is this bad? |
| 16 | + /// Printing during a formatting trait impl is likely unintentional. It can |
| 17 | + /// cause output to be split between the wrong streams. If `format!` is used |
| 18 | + /// text would go to stdout/stderr even if not desired. |
| 19 | + /// |
| 20 | + /// ### Example |
| 21 | + /// ```rust |
| 22 | + /// use std::fmt::{Display, Error, Formatter}; |
| 23 | + /// |
| 24 | + /// struct S; |
| 25 | + /// impl Display for S { |
| 26 | + /// fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { |
| 27 | + /// println!("S"); |
| 28 | + /// |
| 29 | + /// Ok(()) |
| 30 | + /// } |
| 31 | + /// } |
| 32 | + /// ``` |
| 33 | + /// Use instead: |
| 34 | + /// ```rust |
| 35 | + /// use std::fmt::{Display, Error, Formatter}; |
| 36 | + /// |
| 37 | + /// struct S; |
| 38 | + /// impl Display for S { |
| 39 | + /// fn fmt(&self, f: &mut Formatter) -> Result<(), Error> { |
| 40 | + /// writeln!(f, "S"); |
| 41 | + /// |
| 42 | + /// Ok(()) |
| 43 | + /// } |
| 44 | + /// } |
| 45 | + /// ``` |
| 46 | + #[clippy::version = "1.59.0"] |
| 47 | + pub PRINT_IN_FMT, |
| 48 | + suspicious, |
| 49 | + "use of a print macro in a formatting trait impl" |
| 50 | +} |
| 51 | + |
| 52 | +struct FmtImpl { |
| 53 | + trait_name: Symbol, |
| 54 | + formatter_name: Option<Symbol>, |
| 55 | +} |
| 56 | + |
| 57 | +#[derive(Default)] |
| 58 | +pub struct PrintInFmt { |
| 59 | + fmt_impl: Option<FmtImpl>, |
| 60 | +} |
| 61 | + |
| 62 | +impl_lint_pass!(PrintInFmt => [PRINT_IN_FMT]); |
| 63 | + |
| 64 | +impl LateLintPass<'_> for PrintInFmt { |
| 65 | + fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) { |
| 66 | + let formatting_traits = [sym::Display, sym::Debug]; |
| 67 | + |
| 68 | + if_chain! { |
| 69 | + if impl_item.ident.name == sym::fmt; |
| 70 | + if let ImplItemKind::Fn(_, body_id) = impl_item.kind; |
| 71 | + if let Some(Impl { of_trait: Some(trait_ref),..}) = get_parent_as_impl(cx.tcx, impl_item.hir_id()); |
| 72 | + if let Some(trait_def_id) = trait_ref.trait_def_id(); |
| 73 | + if let Some(index) = match_any_diagnostic_items(cx, trait_def_id, &formatting_traits); |
| 74 | + then { |
| 75 | + let body = cx.tcx.hir().body(body_id); |
| 76 | + let formatter_name = body.params.get(1) |
| 77 | + .and_then(|param| param.pat.simple_ident()) |
| 78 | + .map(|ident| ident.name); |
| 79 | + |
| 80 | + self.fmt_impl = Some(FmtImpl { |
| 81 | + formatter_name, |
| 82 | + trait_name: formatting_traits[index], |
| 83 | + }); |
| 84 | + } |
| 85 | + }; |
| 86 | + } |
| 87 | + |
| 88 | + fn check_impl_item_post(&mut self, _: &LateContext<'_>, _: &ImplItem<'_>) { |
| 89 | + self.fmt_impl = None; |
| 90 | + } |
| 91 | + |
| 92 | + fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) { |
| 93 | + if let Some(fmt_impl) = &self.fmt_impl { |
| 94 | + if let Some(macro_call) = root_macro_call_first_node(cx, expr) { |
| 95 | + let name = cx.tcx.item_name(macro_call.def_id); |
| 96 | + let replacement = match name.as_str() { |
| 97 | + "print" | "eprint" => "write", |
| 98 | + "println" | "eprintln" => "writeln", |
| 99 | + _ => return, |
| 100 | + }; |
| 101 | + |
| 102 | + span_lint_and_sugg( |
| 103 | + cx, |
| 104 | + PRINT_IN_FMT, |
| 105 | + macro_call.span, |
| 106 | + &format!("use of `{}!` in `{}` impl", name.as_str(), fmt_impl.trait_name), |
| 107 | + "replace with", |
| 108 | + if let Some(formatter_name) = fmt_impl.formatter_name { |
| 109 | + format!("{}!({}, ..)", replacement, formatter_name) |
| 110 | + } else { |
| 111 | + format!("{}!(..)", replacement) |
| 112 | + }, |
| 113 | + Applicability::HasPlaceholders, |
| 114 | + ); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments