2022-05-05 09:12:52 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_help;
|
2022-07-05 11:56:16 -05:00
|
|
|
use clippy_utils::ty::is_type_lang_item;
|
2023-07-02 07:35:19 -05:00
|
|
|
use clippy_utils::{higher, match_def_path, paths};
|
|
|
|
use rustc_hir::{BinOpKind, Expr, ExprKind, LangItem, MatchSource};
|
2022-05-05 09:12:52 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
2023-12-01 11:21:58 -06:00
|
|
|
use rustc_session::declare_lint_pass;
|
2022-05-05 09:12:52 -05:00
|
|
|
use rustc_span::sym;
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
|
|
|
/// ### What it does
|
|
|
|
/// Detects cases where the result of a `format!` call is
|
|
|
|
/// appended to an existing `String`.
|
|
|
|
///
|
|
|
|
/// ### Why is this bad?
|
|
|
|
/// Introduces an extra, avoidable heap allocation.
|
|
|
|
///
|
2022-07-18 02:39:37 -05:00
|
|
|
/// ### Known problems
|
|
|
|
/// `format!` returns a `String` but `write!` returns a `Result`.
|
|
|
|
/// Thus you are forced to ignore the `Err` variant to achieve the same API.
|
|
|
|
///
|
|
|
|
/// While using `write!` in the suggested way should never fail, this isn't necessarily clear to the programmer.
|
|
|
|
///
|
2022-05-05 09:12:52 -05:00
|
|
|
/// ### Example
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-05-05 09:12:52 -05:00
|
|
|
/// let mut s = String::new();
|
|
|
|
/// s += &format!("0x{:X}", 1024);
|
|
|
|
/// s.push_str(&format!("0x{:X}", 1024));
|
|
|
|
/// ```
|
|
|
|
/// Use instead:
|
2023-11-02 11:35:56 -05:00
|
|
|
/// ```no_run
|
2022-05-05 09:12:52 -05:00
|
|
|
/// use std::fmt::Write as _; // import without risk of name clashing
|
|
|
|
///
|
|
|
|
/// let mut s = String::new();
|
|
|
|
/// let _ = write!(s, "0x{:X}", 1024);
|
|
|
|
/// ```
|
2022-07-18 02:39:37 -05:00
|
|
|
#[clippy::version = "1.62.0"]
|
2022-05-05 09:12:52 -05:00
|
|
|
pub FORMAT_PUSH_STRING,
|
2022-07-18 02:39:37 -05:00
|
|
|
restriction,
|
2022-05-05 09:12:52 -05:00
|
|
|
"`format!(..)` appended to existing `String`"
|
|
|
|
}
|
|
|
|
declare_lint_pass!(FormatPushString => [FORMAT_PUSH_STRING]);
|
|
|
|
|
|
|
|
fn is_string(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
|
2022-07-05 11:56:16 -05:00
|
|
|
is_type_lang_item(cx, cx.typeck_results().expr_ty(e).peel_refs(), LangItem::String)
|
2022-05-05 09:12:52 -05:00
|
|
|
}
|
|
|
|
fn is_format(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
|
2023-07-02 07:35:19 -05:00
|
|
|
let e = e.peel_blocks().peel_borrows();
|
|
|
|
|
|
|
|
if e.span.from_expansion()
|
|
|
|
&& let Some(macro_def_id) = e.span.ctxt().outer_expn_data().macro_def_id
|
|
|
|
{
|
2022-05-05 09:12:52 -05:00
|
|
|
cx.tcx.get_diagnostic_name(macro_def_id) == Some(sym::format_macro)
|
2023-07-02 07:35:19 -05:00
|
|
|
} else if let Some(higher::If { then, r#else, .. }) = higher::If::hir(e) {
|
|
|
|
is_format(cx, then) || r#else.is_some_and(|e| is_format(cx, e))
|
2022-05-05 09:12:52 -05:00
|
|
|
} else {
|
2023-07-02 07:35:19 -05:00
|
|
|
match higher::IfLetOrMatch::parse(cx, e) {
|
|
|
|
Some(higher::IfLetOrMatch::Match(_, arms, MatchSource::Normal)) => {
|
|
|
|
arms.iter().any(|arm| is_format(cx, arm.body))
|
|
|
|
},
|
2023-12-28 12:33:07 -06:00
|
|
|
Some(higher::IfLetOrMatch::IfLet(_, _, then, r#else, _)) => {
|
2023-11-02 11:35:56 -05:00
|
|
|
is_format(cx, then) || r#else.is_some_and(|e| is_format(cx, e))
|
2023-07-02 07:35:19 -05:00
|
|
|
},
|
|
|
|
_ => false,
|
|
|
|
}
|
2022-05-05 09:12:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> LateLintPass<'tcx> for FormatPushString {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
|
|
|
let arg = match expr.kind {
|
2022-09-01 04:43:35 -05:00
|
|
|
ExprKind::MethodCall(_, _, [arg], _) => {
|
2023-11-02 11:35:56 -05:00
|
|
|
if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
|
|
|
|
&& match_def_path(cx, fn_def_id, &paths::PUSH_STR)
|
|
|
|
{
|
2022-05-05 09:12:52 -05:00
|
|
|
arg
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
2023-11-02 11:35:56 -05:00
|
|
|
ExprKind::AssignOp(op, left, arg) if op.node == BinOpKind::Add && is_string(cx, left) => arg,
|
2022-05-05 09:12:52 -05:00
|
|
|
_ => return,
|
|
|
|
};
|
|
|
|
if is_format(cx, arg) {
|
|
|
|
span_lint_and_help(
|
|
|
|
cx,
|
|
|
|
FORMAT_PUSH_STRING,
|
|
|
|
expr.span,
|
|
|
|
"`format!(..)` appended to existing `String`",
|
|
|
|
None,
|
|
|
|
"consider using `write!` to avoid the extra allocation",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|