2021-03-17 16:23:37 +00:00
|
|
|
use super::utils::get_hint_if_single_char_arg;
|
2021-03-16 00:55:45 +00:00
|
|
|
use clippy_utils::diagnostics::span_lint_and_sugg;
|
2021-03-14 23:17:44 +00:00
|
|
|
use clippy_utils::source::snippet_with_applicability;
|
2021-03-06 06:29:02 +00:00
|
|
|
use rustc_errors::Applicability;
|
|
|
|
use rustc_hir as hir;
|
|
|
|
use rustc_lint::LateContext;
|
|
|
|
|
|
|
|
use super::SINGLE_CHAR_ADD_STR;
|
|
|
|
|
|
|
|
/// lint for length-1 `str`s as argument for `insert_str`
|
|
|
|
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
|
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
|
|
|
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[2], &mut applicability) {
|
|
|
|
let base_string_snippet =
|
|
|
|
snippet_with_applicability(cx, args[0].span.source_callsite(), "_", &mut applicability);
|
|
|
|
let pos_arg = snippet_with_applicability(cx, args[1].span, "..", &mut applicability);
|
|
|
|
let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
SINGLE_CHAR_ADD_STR,
|
|
|
|
expr.span,
|
|
|
|
"calling `insert_str()` using a single-character string literal",
|
|
|
|
"consider using `insert` with a character literal",
|
|
|
|
sugg,
|
|
|
|
applicability,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|