@@ -1290,8 +1290,8 @@ declare_clippy_lint! {
12901290}
12911291
12921292declare_clippy_lint ! {
1293- /// **What it does:** Warns when using `push_str` with a single-character string literal,
1294- /// and `push` with a `char` would work fine.
1293+ /// **What it does:** Warns when using `push_str` with a single-character string literal
1294+ /// where `push` with a `char` would work fine.
12951295 ///
12961296 /// **Why is this bad?** It's less clear that we are pushing a single character.
12971297 ///
@@ -1521,6 +1521,8 @@ impl<'tcx> LateLintPass<'tcx> for Methods {
15211521 if let Some ( fn_def_id) = cx. typeck_results ( ) . type_dependent_def_id ( expr. hir_id ) {
15221522 if match_def_path ( cx, fn_def_id, & paths:: PUSH_STR ) {
15231523 lint_single_char_push_string ( cx, expr, args) ;
1524+ } else if match_def_path ( cx, fn_def_id, & paths:: INSERT_STR ) {
1525+ lint_single_char_insert_string ( cx, expr, args) ;
15241526 }
15251527 }
15261528
@@ -3255,6 +3257,25 @@ fn lint_single_char_push_string(cx: &LateContext<'_>, expr: &hir::Expr<'_>, args
32553257 }
32563258}
32573259
3260+ /// lint for length-1 `str`s as argument for `insert_str`
3261+ fn lint_single_char_insert_string ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > , args : & [ hir:: Expr < ' _ > ] ) {
3262+ let mut applicability = Applicability :: MachineApplicable ;
3263+ if let Some ( extension_string) = get_hint_if_single_char_arg ( cx, & args[ 2 ] , & mut applicability) {
3264+ let base_string_snippet = snippet_with_applicability ( cx, args[ 0 ] . span , "_" , & mut applicability) ;
3265+ let pos_arg = snippet ( cx, args[ 1 ] . span , ".." ) ;
3266+ let sugg = format ! ( "{}.insert({}, {})" , base_string_snippet, pos_arg, extension_string) ;
3267+ span_lint_and_sugg (
3268+ cx,
3269+ SINGLE_CHAR_PUSH_STR ,
3270+ expr. span ,
3271+ "calling `insert_str()` using a single-character string literal" ,
3272+ "consider using `insert` with a character literal" ,
3273+ sugg,
3274+ applicability,
3275+ ) ;
3276+ }
3277+ }
3278+
32583279/// Checks for the `USELESS_ASREF` lint.
32593280fn lint_asref ( cx : & LateContext < ' _ > , expr : & hir:: Expr < ' _ > , call_name : & str , as_ref_args : & [ hir:: Expr < ' _ > ] ) {
32603281 // when we get here, we've already checked that the call name is "as_ref" or "as_mut"
0 commit comments