2021-07-15 08:44:10 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2022-01-13 12:18:19 +00:00
|
|
|
use clippy_utils::macros::{root_macro_call_first_node, FormatArgsExpn};
|
2021-07-15 08:44:10 +00:00
|
|
|
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
|
2021-04-22 09:31:13 +00:00
|
|
|
use clippy_utils::sugg::Sugg;
|
2018-11-27 20:14:15 +00:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2021-10-21 11:11:36 +00:00
|
|
|
use rustc_hir::{Expr, ExprKind};
|
2021-07-15 08:44:10 +00:00
|
|
|
use rustc_lint::{LateContext, LateLintPass};
|
|
|
|
use rustc_middle::ty;
|
2020-01-11 11:37:08 +00:00
|
|
|
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
2021-07-15 08:44:10 +00:00
|
|
|
use rustc_span::symbol::kw;
|
2022-01-17 12:29:07 +00:00
|
|
|
use rustc_span::{sym, BytePos, Span};
|
2016-02-20 16:35:07 +00:00
|
|
|
|
2018-03-28 13:24:26 +00:00
|
|
|
declare_clippy_lint! {
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### What it does
|
|
|
|
/// Checks for the use of `format!("string literal with no
|
2019-03-05 16:50:33 +00:00
|
|
|
/// argument")` and `format!("{}", foo)` where `foo` is a string.
|
|
|
|
///
|
2021-07-29 10:16:06 +00:00
|
|
|
/// ### Why is this bad?
|
|
|
|
/// There is no point of doing that. `format!("foo")` can
|
2019-03-05 16:50:33 +00: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 10:16:06 +00:00
|
|
|
/// ### Examples
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```rust
|
2021-03-12 14:30:50 +00:00
|
|
|
/// let foo = "foo";
|
2019-08-02 06:13:54 +00:00
|
|
|
/// format!("{}", foo);
|
2022-05-28 13:22:59 +00:00
|
|
|
/// ```
|
2020-06-09 14:36:01 +00:00
|
|
|
///
|
2022-05-28 13:22:59 +00:00
|
|
|
/// Use instead:
|
|
|
|
/// ```rust
|
|
|
|
/// let foo = "foo";
|
2021-03-12 14:30:50 +00:00
|
|
|
/// foo.to_owned();
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2021-12-06 11:33:31 +00:00
|
|
|
#[clippy::version = "pre 1.29.0"]
|
2016-02-20 16:35:07 +00:00
|
|
|
pub USELESS_FORMAT,
|
2018-03-28 13:24:26 +00:00
|
|
|
complexity,
|
2016-02-20 16:35:07 +00:00
|
|
|
"useless use of `format!`"
|
|
|
|
}
|
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
declare_lint_pass!(UselessFormat => [USELESS_FORMAT]);
|
2016-02-20 16:35:07 +00:00
|
|
|
|
2020-06-25 20:41:36 +00:00
|
|
|
impl<'tcx> LateLintPass<'tcx> for UselessFormat {
|
|
|
|
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
|
2022-01-13 12:18:19 +00:00
|
|
|
let (format_args, call_site) = if_chain! {
|
|
|
|
if let Some(macro_call) = root_macro_call_first_node(cx, expr);
|
|
|
|
if cx.tcx.is_diagnostic_item(sym::format_macro, macro_call.def_id);
|
|
|
|
if let Some(format_args) = FormatArgsExpn::find_nested(cx, expr, macro_call.expn);
|
|
|
|
then {
|
|
|
|
(format_args, macro_call.span)
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
2019-08-23 05:26:24 +00:00
|
|
|
};
|
2018-10-03 18:59:59 +00:00
|
|
|
|
2021-07-15 08:44:10 +00:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
if format_args.value_args.is_empty() {
|
2022-01-13 12:18:19 +00:00
|
|
|
match *format_args.format_string_parts {
|
|
|
|
[] => span_useless_format_empty(cx, call_site, "String::new()".to_owned(), applicability),
|
|
|
|
[_] => {
|
|
|
|
if let Some(s_src) = snippet_opt(cx, format_args.format_string_span) {
|
2021-11-04 12:52:36 +00:00
|
|
|
// Simulate macro expansion, converting {{ and }} to { and }.
|
|
|
|
let s_expand = s_src.replace("{{", "{").replace("}}", "}");
|
|
|
|
let sugg = format!("{}.to_string()", s_expand);
|
|
|
|
span_useless_format(cx, call_site, sugg, applicability);
|
|
|
|
}
|
2022-01-13 12:18:19 +00:00
|
|
|
},
|
|
|
|
[..] => {},
|
2021-07-15 08:44:10 +00:00
|
|
|
}
|
|
|
|
} else if let [value] = *format_args.value_args {
|
|
|
|
if_chain! {
|
2022-01-13 12:18:19 +00:00
|
|
|
if format_args.format_string_parts == [kw::Empty];
|
2021-07-15 08:44:10 +00:00
|
|
|
if match cx.typeck_results().expr_ty(value).peel_refs().kind() {
|
2022-03-04 20:28:41 +00:00
|
|
|
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(sym::String, adt.did()),
|
2021-07-15 08:44:10 +00:00
|
|
|
ty::Str => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
2021-10-21 11:11:36 +00:00
|
|
|
if let Some(args) = format_args.args();
|
2022-01-13 12:18:19 +00:00
|
|
|
if args.iter().all(|arg| arg.format_trait == sym::Display && !arg.has_string_formatting());
|
2021-07-15 08:44:10 +00:00
|
|
|
then {
|
|
|
|
let is_new_string = match value.kind {
|
|
|
|
ExprKind::Binary(..) => true,
|
|
|
|
ExprKind::MethodCall(path, ..) => path.ident.name.as_str() == "to_string",
|
|
|
|
_ => false,
|
|
|
|
};
|
2022-01-17 12:29:07 +00:00
|
|
|
let sugg = if format_args.format_string_span.contains(value.span) {
|
|
|
|
// Implicit argument. e.g. `format!("{x}")` span points to `{x}`
|
|
|
|
let spdata = value.span.data();
|
|
|
|
let span = Span::new(
|
|
|
|
spdata.lo + BytePos(1),
|
|
|
|
spdata.hi - BytePos(1),
|
|
|
|
spdata.ctxt,
|
|
|
|
spdata.parent
|
|
|
|
);
|
|
|
|
let snip = snippet_with_applicability(cx, span, "..", &mut applicability);
|
|
|
|
if is_new_string {
|
|
|
|
snip.into()
|
|
|
|
} else {
|
|
|
|
format!("{snip}.to_string()")
|
|
|
|
}
|
|
|
|
} else if is_new_string {
|
2021-07-15 08:44:10 +00:00
|
|
|
snippet_with_applicability(cx, value.span, "..", &mut applicability).into_owned()
|
|
|
|
} else {
|
|
|
|
let sugg = Sugg::hir_with_applicability(cx, value, "<arg>", &mut applicability);
|
|
|
|
format!("{}.to_string()", sugg.maybe_par())
|
|
|
|
};
|
|
|
|
span_useless_format(cx, call_site, sugg, applicability);
|
2019-08-23 05:26:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-15 08:44:10 +00:00
|
|
|
};
|
2017-10-23 19:18:02 +00:00
|
|
|
}
|
2016-02-20 20:15:05 +00:00
|
|
|
}
|
|
|
|
|
2021-11-04 12:52:36 +00: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 18:28:30 +00:00
|
|
|
fn span_useless_format(cx: &LateContext<'_>, span: Span, sugg: String, applicability: Applicability) {
|
2021-07-15 08:44:10 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
USELESS_FORMAT,
|
|
|
|
span,
|
|
|
|
"useless use of `format!`",
|
|
|
|
"consider using `.to_string()`",
|
|
|
|
sugg,
|
|
|
|
applicability,
|
|
|
|
);
|
2019-08-23 05:26:24 +00:00
|
|
|
}
|