]> git.lizzy.rs Git - rust.git/blob - clippy_lints/src/format_push_string.rs
Merge commit 'd7b5cbf065b88830ca519adcb73fad4c0d24b1c7' into clippyup
[rust.git] / clippy_lints / src / format_push_string.rs
1 use clippy_utils::diagnostics::span_lint_and_help;
2 use clippy_utils::ty::is_type_diagnostic_item;
3 use clippy_utils::{match_def_path, paths, peel_hir_expr_refs};
4 use rustc_hir::{BinOpKind, Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass};
6 use rustc_session::{declare_lint_pass, declare_tool_lint};
7 use rustc_span::sym;
8
9 declare_clippy_lint! {
10     /// ### What it does
11     /// Detects cases where the result of a `format!` call is
12     /// appended to an existing `String`.
13     ///
14     /// ### Why is this bad?
15     /// Introduces an extra, avoidable heap allocation.
16     ///
17     /// ### Example
18     /// ```rust
19     /// let mut s = String::new();
20     /// s += &format!("0x{:X}", 1024);
21     /// s.push_str(&format!("0x{:X}", 1024));
22     /// ```
23     /// Use instead:
24     /// ```rust
25     /// use std::fmt::Write as _; // import without risk of name clashing
26     ///
27     /// let mut s = String::new();
28     /// let _ = write!(s, "0x{:X}", 1024);
29     /// ```
30     #[clippy::version = "1.61.0"]
31     pub FORMAT_PUSH_STRING,
32     perf,
33     "`format!(..)` appended to existing `String`"
34 }
35 declare_lint_pass!(FormatPushString => [FORMAT_PUSH_STRING]);
36
37 fn is_string(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
38     is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(e).peel_refs(), sym::String)
39 }
40 fn is_format(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
41     if let Some(macro_def_id) = e.span.ctxt().outer_expn_data().macro_def_id {
42         cx.tcx.get_diagnostic_name(macro_def_id) == Some(sym::format_macro)
43     } else {
44         false
45     }
46 }
47
48 impl<'tcx> LateLintPass<'tcx> for FormatPushString {
49     fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
50         let arg = match expr.kind {
51             ExprKind::MethodCall(_, [_, arg], _) => {
52                 if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) &&
53                 match_def_path(cx, fn_def_id, &paths::PUSH_STR) {
54                     arg
55                 } else {
56                     return;
57                 }
58             }
59             ExprKind::AssignOp(op, left, arg)
60             if op.node == BinOpKind::Add && is_string(cx, left) => {
61                 arg
62             },
63             _ => return,
64         };
65         let (arg, _) = peel_hir_expr_refs(arg);
66         if is_format(cx, arg) {
67             span_lint_and_help(
68                 cx,
69                 FORMAT_PUSH_STRING,
70                 expr.span,
71                 "`format!(..)` appended to existing `String`",
72                 None,
73                 "consider using `write!` to avoid the extra allocation",
74             );
75         }
76     }
77 }