2021-07-15 03:44:10 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2023-03-28 16:28:57 -05:00
|
|
|
use clippy_utils::macros::{find_format_arg_expr, find_format_args, root_macro_call_first_node};
|
|
|
|
use clippy_utils::source::{snippet_opt, snippet_with_context};
|
2021-04-22 04:31:13 -05:00
|
|
|
use clippy_utils::sugg::Sugg;
|
2023-03-28 16:28:57 -05:00
|
|
|
use rustc_ast::{FormatArgsPiece, FormatOptions, FormatTrait};
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2021-10-21 06:11:36 -05:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2021-07-15 03:44:10 -05:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty;
|
2020-01-11 05:37:08 -06:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2022-07-28 12:08:22 -05:00
|
|
|
use rustc_span::{sym, Span};
|
2016-02-20 10:35:07 -06:00
|
|
|
|
2018-03-28 08:24:26 -05:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for the use of `format!("string literal with no
|
2019-03-05 10:50:33 -06:00
|
|
|
/// argument")` and `format!("{}", foo)` where `foo` is a string.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// There is no point of doing that. `format!("foo")` can
|
2019-03-05 10:50:33 -06:00
|
|
|
/// be replaced by `"foo".to_owned()` if you really need a `String`. The even
|
|
|
|
/// worse `&format!("foo")` is often encountered in the wild. `format!("{}",
|
|
|
|
/// foo)` can be replaced by `foo.clone()` if `foo: String` or `foo.to_owned()`
|
|
|
|
/// if `foo: &str`.
|
|
|
|
///
|
2021-07-29 05:16:06 -05:00
|
|
|
/// ### Examples
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2021-03-12 08:30:50 -06:00
|
|
|
/// let foo = "foo";
|
2019-08-02 01:13:54 -05:00
|
|
|
/// format!("{}", foo);
|
2022-06-04 06:34:07 -05:00
|
|
|
/// ```
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
2022-06-04 06:34:07 -05:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// let foo = "foo";
|
2021-03-12 08:30:50 -06:00
|
|
|
/// foo.to_owned();
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
2021-12-06 05:33:31 -06:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-02-20 10:35:07 -06:00
|
|
|
pub USELESS_FORMAT,
|
2018-03-28 08:24:26 -05:00
|
|
|
complexity,
|
2016-02-20 10:35:07 -06:00
|
|
|
"useless use of `format!`"
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:43:55 -05:00
|
|
|
declare_lint_pass!(UselessFormat => [USELESS_FORMAT]);
|
2016-02-20 10:35:07 -06:00
|
|
|
|
2020-06-25 15:41:36 -05:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UselessFormat {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2023-03-28 16:28:57 -05:00
|
|
|
let Some(macro_call) = root_macro_call_first_node(cx, expr) else { return };
|
|
|
|
if !cx.tcx.is_diagnostic_item(sym::format_macro, macro_call.def_id) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
find_format_args(cx, expr, macro_call.expn, |format_args| {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
let call_site = macro_call.span;
|
2018-10-03 13:59:59 -05:00
|
|
|
|
2023-03-28 16:28:57 -05:00
|
|
|
match (format_args.arguments.all_args(), &format_args.template[..]) {
|
|
|
|
([], []) => span_useless_format_empty(cx, call_site, "String::new()".to_owned(), applicability),
|
|
|
|
([], [_]) => {
|
2022-08-31 08:24:45 -05:00
|
|
|
// Simulate macro expansion, converting {{ and }} to { and }.
|
2023-03-28 16:28:57 -05:00
|
|
|
let Some(snippet) = snippet_opt(cx, format_args.span) else { return };
|
|
|
|
let s_expand = snippet.replace("{{", "{").replace("}}", "}");
|
2022-10-06 02:44:38 -05:00
|
|
|
let sugg = format!("{s_expand}.to_string()");
|
2022-08-31 08:24:45 -05:00
|
|
|
span_useless_format(cx, call_site, sugg, applicability);
|
2022-01-13 06:18:19 -06:00
|
|
|
},
|
2023-03-28 16:28:57 -05:00
|
|
|
([arg], [piece]) => {
|
|
|
|
if let Ok(value) = find_format_arg_expr(expr, arg)
|
|
|
|
&& let FormatArgsPiece::Placeholder(placeholder) = piece
|
|
|
|
&& placeholder.format_trait == FormatTrait::Display
|
|
|
|
&& placeholder.format_options == FormatOptions::default()
|
|
|
|
&& match cx.typeck_results().expr_ty(value).peel_refs().kind() {
|
|
|
|
ty::Adt(adt, _) => Some(adt.did()) == cx.tcx.lang_items().string(),
|
|
|
|
ty::Str => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
{
|
|
|
|
let is_new_string = match value.kind {
|
|
|
|
ExprKind::Binary(..) => true,
|
|
|
|
ExprKind::MethodCall(path, ..) => path.ident.name == sym::to_string,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
let sugg = if is_new_string {
|
|
|
|
snippet_with_context(cx, value.span, call_site.ctxt(), "..", &mut applicability).0.into_owned()
|
|
|
|
} else {
|
|
|
|
let sugg = Sugg::hir_with_context(cx, value, call_site.ctxt(), "<arg>", &mut applicability);
|
|
|
|
format!("{}.to_string()", sugg.maybe_par())
|
|
|
|
};
|
|
|
|
span_useless_format(cx, call_site, sugg, applicability);
|
|
|
|
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {},
|
2019-08-23 00:26:24 -05:00
|
|
|
}
|
2023-03-28 16:28:57 -05:00
|
|
|
});
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
2016-02-20 14:15:05 -06:00
|
|
|
}
|
|
|
|
|
2021-11-04 07:52:36 -05:00
|
|
|
fn span_useless_format_empty(cx: &LateContext<'_>, span: Span, sugg: String, applicability: Applicability) {
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
USELESS_FORMAT,
|
|
|
|
span,
|
|
|
|
"useless use of `format!`",
|
|
|
|
"consider using `String::new()`",
|
|
|
|
sugg,
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-14 13:28:30 -05:00
|
|
|
fn span_useless_format(cx: &LateContext<'_>, span: Span, sugg: String, applicability: Applicability) {
|
2021-07-15 03:44:10 -05:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
USELESS_FORMAT,
|
|
|
|
span,
|
|
|
|
"useless use of `format!`",
|
|
|
|
"consider using `.to_string()`",
|
|
|
|
sugg,
|
|
|
|
applicability,
|
|
|
|
);
|
2019-08-23 00:26:24 -05:00
|
|
|
}
|