move single_char_insert_string to its own module
This commit is contained in:
parent
2ade32ddf2
commit
8006dab817
@ -20,6 +20,7 @@ mod map_collect_result_unit;
|
||||
mod ok_expect;
|
||||
mod option_as_ref_deref;
|
||||
mod option_map_unwrap_or;
|
||||
mod single_char_insert_string;
|
||||
mod skip_while_next;
|
||||
mod string_extend_chars;
|
||||
mod suspicious_map;
|
||||
@ -1779,7 +1780,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
|
||||
if match_def_path(cx, fn_def_id, &paths::PUSH_STR) {
|
||||
lint_single_char_push_string(cx, expr, args);
|
||||
} else if match_def_path(cx, fn_def_id, &paths::INSERT_STR) {
|
||||
lint_single_char_insert_string(cx, expr, args);
|
||||
single_char_insert_string::check(cx, expr, args);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3235,26 +3236,6 @@ fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args
|
||||
}
|
||||
}
|
||||
|
||||
/// lint for length-1 `str`s as argument for `insert_str`
|
||||
fn lint_single_char_insert_string(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,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Checks for the `USELESS_ASREF` lint.
|
||||
fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_ref_args: &[hir::Expr<'_>]) {
|
||||
// when we get here, we've already checked that the call name is "as_ref" or "as_mut"
|
||||
|
27
clippy_lints/src/methods/single_char_insert_string.rs
Normal file
27
clippy_lints/src/methods/single_char_insert_string.rs
Normal file
@ -0,0 +1,27 @@
|
||||
use crate::methods::get_hint_if_single_char_arg;
|
||||
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
|
||||
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,
|
||||
);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user