2021-07-06 11:51:15 -05:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
|
|
|
use clippy_utils::higher::FormatExpn;
|
|
|
|
use clippy_utils::last_path_segment;
|
|
|
|
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
|
2021-04-19 10:20:21 -05:00
|
|
|
use clippy_utils::sugg::Sugg;
|
2018-11-27 14:14:15 -06:00
|
|
|
use if_chain::if_chain;
|
2018-12-29 09:04:45 -06:00
|
|
|
use rustc_errors::Applicability;
|
2021-07-06 11:51:15 -05:00
|
|
|
use rustc_hir::{BorrowKind, Expr, ExprKind, QPath};
|
|
|
|
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};
|
2021-07-04 16:34:22 -05:00
|
|
|
use rustc_span::symbol::kw;
|
2021-07-06 11:51:15 -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-02 13:37:11 -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-02 13:37:11 -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-02 13:37:11 -05:00
|
|
|
/// ### Examples
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```rust
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Bad
|
2021-03-05 14:11:31 -06:00
|
|
|
/// let foo = "foo";
|
2019-08-02 01:13:54 -05:00
|
|
|
/// format!("{}", foo);
|
2020-06-09 09:36:01 -05:00
|
|
|
///
|
|
|
|
/// // Good
|
2021-03-05 14:11:31 -06:00
|
|
|
/// foo.to_owned();
|
2019-03-05 10:50:33 -06:00
|
|
|
/// ```
|
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<'_>) {
|
2021-07-06 11:51:15 -05:00
|
|
|
let FormatExpn { call_site, format_args } = match FormatExpn::parse(expr) {
|
|
|
|
Some(e) if !e.call_site.from_expansion() => e,
|
2019-08-23 00:26:24 -05:00
|
|
|
_ => return,
|
|
|
|
};
|
2018-10-03 13:59:59 -05:00
|
|
|
|
2021-07-06 11:51:15 -05:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
if format_args.value_args.is_empty() {
|
|
|
|
if_chain! {
|
|
|
|
if let [e] = &*format_args.format_string_parts;
|
|
|
|
if let ExprKind::Lit(lit) = &e.kind;
|
|
|
|
if let Some(s_src) = snippet_opt(cx, lit.span);
|
|
|
|
then {
|
|
|
|
// 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);
|
2019-08-23 00:26:24 -05:00
|
|
|
}
|
2021-07-06 11:51:15 -05:00
|
|
|
}
|
|
|
|
} else if let [value] = *format_args.value_args {
|
|
|
|
if_chain! {
|
|
|
|
if format_args.format_string_symbols == [kw::Empty];
|
|
|
|
if match cx.typeck_results().expr_ty(value).peel_refs().kind() {
|
|
|
|
ty::Adt(adt, _) => cx.tcx.is_diagnostic_item(sym::string_type, adt.did),
|
|
|
|
ty::Str => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if format_args.args.iter().all(|e| is_display_arg(e));
|
|
|
|
if format_args.fmt_expr.map_or(true, |e| check_unformatted(e));
|
|
|
|
then {
|
|
|
|
let is_new_string = match value.kind {
|
|
|
|
ExprKind::Binary(..) => true,
|
|
|
|
ExprKind::MethodCall(path, ..) => path.ident.name.as_str() == "to_string",
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
let sugg = if is_new_string {
|
|
|
|
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 00:26:24 -05:00
|
|
|
}
|
|
|
|
}
|
2021-07-06 11:51:15 -05:00
|
|
|
};
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
2016-02-20 14:15:05 -06:00
|
|
|
}
|
|
|
|
|
2021-07-06 11:51:15 -05:00
|
|
|
fn span_useless_format(cx: &LateContext<'_>, span: Span, mut sugg: String, mut applicability: Applicability) {
|
|
|
|
// The callsite span contains the statement semicolon for some reason.
|
|
|
|
if snippet_with_applicability(cx, span, "..", &mut applicability).ends_with(';') {
|
|
|
|
sugg.push(';');
|
2018-04-12 01:21:03 -05:00
|
|
|
}
|
2021-07-06 11:51:15 -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
|
|
|
}
|
2018-04-12 01:21:03 -05:00
|
|
|
|
2021-07-06 11:51:15 -05:00
|
|
|
fn is_display_arg(expr: &Expr<'_>) -> bool {
|
2019-08-23 00:26:24 -05:00
|
|
|
if_chain! {
|
2021-07-06 11:51:15 -05:00
|
|
|
if let ExprKind::Call(_, [_, fmt]) = expr.kind;
|
|
|
|
if let ExprKind::Path(QPath::Resolved(_, path)) = fmt.kind;
|
|
|
|
if let [.., t, _] = path.segments;
|
|
|
|
if t.ident.name.as_str() == "Display";
|
|
|
|
then { true } else { false }
|
2019-08-23 00:26:24 -05:00
|
|
|
}
|
2018-04-12 01:21:03 -05:00
|
|
|
}
|
2017-11-04 14:55:56 -05:00
|
|
|
|
2018-04-12 01:21:03 -05:00
|
|
|
/// Checks if the expression matches
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// &[_ {
|
|
|
|
/// format: _ {
|
|
|
|
/// width: _::Implied,
|
2019-08-23 00:26:24 -05:00
|
|
|
/// precision: _::Implied,
|
2018-04-12 01:21:03 -05:00
|
|
|
/// ...
|
|
|
|
/// },
|
|
|
|
/// ...,
|
|
|
|
/// }]
|
|
|
|
/// ```
|
2019-12-27 01:12:26 -06:00
|
|
|
fn check_unformatted(expr: &Expr<'_>) -> bool {
|
2018-04-12 01:21:03 -05:00
|
|
|
if_chain! {
|
2021-04-02 16:35:32 -05:00
|
|
|
if let ExprKind::AddrOf(BorrowKind::Ref, _, expr) = expr.kind;
|
2021-07-06 11:51:15 -05:00
|
|
|
if let ExprKind::Array([expr]) = expr.kind;
|
2019-08-23 00:26:24 -05:00
|
|
|
// struct `core::fmt::rt::v1::Argument`
|
2021-07-06 11:51:15 -05:00
|
|
|
if let ExprKind::Struct(_, fields, _) = expr.kind;
|
2020-11-02 10:32:55 -06:00
|
|
|
if let Some(format_field) = fields.iter().find(|f| f.ident.name == sym::format);
|
2019-08-23 00:26:24 -05:00
|
|
|
// struct `core::fmt::rt::v1::FormatSpec`
|
2021-04-02 16:35:32 -05:00
|
|
|
if let ExprKind::Struct(_, fields, _) = format_field.expr.kind;
|
2020-11-02 10:32:55 -06:00
|
|
|
if let Some(precision_field) = fields.iter().find(|f| f.ident.name == sym::precision);
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Path(ref precision_path) = precision_field.expr.kind;
|
2020-11-02 10:32:55 -06:00
|
|
|
if last_path_segment(precision_path).ident.name == sym::Implied;
|
|
|
|
if let Some(width_field) = fields.iter().find(|f| f.ident.name == sym::width);
|
2019-09-27 10:16:06 -05:00
|
|
|
if let ExprKind::Path(ref width_qpath) = width_field.expr.kind;
|
2020-11-02 10:32:55 -06:00
|
|
|
if last_path_segment(width_qpath).ident.name == sym::Implied;
|
2018-04-12 01:21:03 -05:00
|
|
|
then {
|
|
|
|
return true;
|
2017-10-23 14:18:02 -05:00
|
|
|
}
|
|
|
|
}
|
2016-02-20 14:15:05 -06:00
|
|
|
|
|
|
|
false
|
|
|
|
}
|