Skip to content

Commit 8006dab

Browse files
committed
move single_char_insert_string to its own module
1 parent 2ade32d commit 8006dab

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

clippy_lints/src/methods/mod.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod map_collect_result_unit;
2020
mod ok_expect;
2121
mod option_as_ref_deref;
2222
mod option_map_unwrap_or;
23+
mod single_char_insert_string;
2324
mod skip_while_next;
2425
mod string_extend_chars;
2526
mod suspicious_map;
@@ -1779,7 +1780,7 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
17791780
if match_def_path(cx, fn_def_id, &paths::PUSH_STR) {
17801781
lint_single_char_push_string(cx, expr, args);
17811782
} else if match_def_path(cx, fn_def_id, &paths::INSERT_STR) {
1782-
lint_single_char_insert_string(cx, expr, args);
1783+
single_char_insert_string::check(cx, expr, args);
17831784
}
17841785
}
17851786

@@ -3235,26 +3236,6 @@ fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args
32353236
}
32363237
}
32373238

3238-
/// lint for length-1 `str`s as argument for `insert_str`
3239-
fn lint_single_char_insert_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
3240-
let mut applicability = Applicability::MachineApplicable;
3241-
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[2], &mut applicability) {
3242-
let base_string_snippet =
3243-
snippet_with_applicability(cx, args[0].span.source_callsite(), "_", &mut applicability);
3244-
let pos_arg = snippet_with_applicability(cx, args[1].span, "..", &mut applicability);
3245-
let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
3246-
span_lint_and_sugg(
3247-
cx,
3248-
SINGLE_CHAR_ADD_STR,
3249-
expr.span,
3250-
"calling `insert_str()` using a single-character string literal",
3251-
"consider using `insert` with a character literal",
3252-
sugg,
3253-
applicability,
3254-
);
3255-
}
3256-
}
3257-
32583239
/// Checks for the `USELESS_ASREF` lint.
32593240
fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_ref_args: &[hir::Expr<'_>]) {
32603241
// when we get here, we've already checked that the call name is "as_ref" or "as_mut"
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
use crate::methods::get_hint_if_single_char_arg;
2+
use crate::utils::{snippet_with_applicability, span_lint_and_sugg};
3+
use rustc_errors::Applicability;
4+
use rustc_hir as hir;
5+
use rustc_lint::LateContext;
6+
7+
use super::SINGLE_CHAR_ADD_STR;
8+
9+
/// lint for length-1 `str`s as argument for `insert_str`
10+
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
11+
let mut applicability = Applicability::MachineApplicable;
12+
if let Some(extension_string) = get_hint_if_single_char_arg(cx, &args[2], &mut applicability) {
13+
let base_string_snippet =
14+
snippet_with_applicability(cx, args[0].span.source_callsite(), "_", &mut applicability);
15+
let pos_arg = snippet_with_applicability(cx, args[1].span, "..", &mut applicability);
16+
let sugg = format!("{}.insert({}, {})", base_string_snippet, pos_arg, extension_string);
17+
span_lint_and_sugg(
18+
cx,
19+
SINGLE_CHAR_ADD_STR,
20+
expr.span,
21+
"calling `insert_str()` using a single-character string literal",
22+
"consider using `insert` with a character literal",
23+
sugg,
24+
applicability,
25+
);
26+
}
27+
}

0 commit comments

Comments
 (0)