Skip to content

Commit 28f4c82

Browse files
committed
[Clippy] Swap single_char_add_str/format_push_string to use diagnostic items instead of paths
1 parent 037b978 commit 28f4c82

File tree

5 files changed

+9
-7
lines changed

5 files changed

+9
-7
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,6 +1882,8 @@ symbols! {
18821882
string_as_mut_str,
18831883
string_as_str,
18841884
string_deref_patterns,
1885+
string_insert_str,
1886+
string_push_str,
18851887
stringify,
18861888
struct_field_attributes,
18871889
struct_inherit,

library/alloc/src/string.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ impl String {
11131113
#[inline]
11141114
#[stable(feature = "rust1", since = "1.0.0")]
11151115
#[rustc_confusables("append", "push")]
1116+
#[cfg_attr(not(test), rustc_diagnostic_item = "string_push_str")]
11161117
pub fn push_str(&mut self, string: &str) {
11171118
self.vec.extend_from_slice(string.as_bytes())
11181119
}
@@ -1747,6 +1748,7 @@ impl String {
17471748
#[cfg(not(no_global_oom_handling))]
17481749
#[inline]
17491750
#[stable(feature = "insert_str", since = "1.16.0")]
1751+
#[cfg_attr(not(test), rustc_diagnostic_item = "string_insert_str")]
17501752
pub fn insert_str(&mut self, idx: usize, string: &str) {
17511753
assert!(self.is_char_boundary(idx));
17521754

src/tools/clippy/clippy_lints/src/format_push_string.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_lang_item;
3-
use clippy_utils::{higher, match_def_path, paths};
3+
use clippy_utils::higher;
44
use rustc_hir::{BinOpKind, Expr, ExprKind, LangItem, MatchSource};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_session::declare_lint_pass;
@@ -70,7 +70,7 @@ impl<'tcx> LateLintPass<'tcx> for FormatPushString {
7070
let arg = match expr.kind {
7171
ExprKind::MethodCall(_, _, [arg], _) => {
7272
if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
73-
&& match_def_path(cx, fn_def_id, &paths::PUSH_STR)
73+
&& cx.tcx.is_diagnostic_item(sym::string_push_str, fn_def_id)
7474
{
7575
arg
7676
} else {

src/tools/clippy/clippy_lints/src/methods/single_char_add_str.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::methods::{single_char_insert_string, single_char_push_string};
2-
use clippy_utils::{match_def_path, paths};
32
use rustc_hir as hir;
43
use rustc_lint::LateContext;
4+
use rustc_span::sym;
55

66
pub(super) fn check(cx: &LateContext<'_>, expr: &hir::Expr<'_>, receiver: &hir::Expr<'_>, args: &[hir::Expr<'_>]) {
77
if let Some(fn_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id) {
8-
if match_def_path(cx, fn_def_id, &paths::PUSH_STR) {
8+
if cx.tcx.is_diagnostic_item(sym::string_push_str, fn_def_id) {
99
single_char_push_string::check(cx, expr, receiver, args);
10-
} else if match_def_path(cx, fn_def_id, &paths::INSERT_STR) {
10+
} else if cx.tcx.is_diagnostic_item(sym::string_insert_str, fn_def_id) {
1111
single_char_insert_string::check(cx, expr, receiver, args);
1212
}
1313
}

src/tools/clippy/clippy_utils/src/paths.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ pub const HASHSET_ITER_TY: [&str; 5] = ["std", "collections", "hash", "set", "It
2929
pub const HASHSET_DRAIN: [&str; 5] = ["std", "collections", "hash", "set", "Drain"];
3030
pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
3131
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
32-
pub const INSERT_STR: [&str; 4] = ["alloc", "string", "String", "insert_str"];
3332
pub const ITERTOOLS_NEXT_TUPLE: [&str; 3] = ["itertools", "Itertools", "next_tuple"];
3433
pub const KW_MODULE: [&str; 3] = ["rustc_span", "symbol", "kw"];
3534
pub const LATE_CONTEXT: [&str; 2] = ["rustc_lint", "LateContext"];
@@ -42,7 +41,6 @@ pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwL
4241
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 3] = ["lock_api", "rwlock", "RwLockWriteGuard"];
4342
#[cfg_attr(not(unix), allow(clippy::invalid_paths))]
4443
pub const PERMISSIONS_FROM_MODE: [&str; 6] = ["std", "os", "unix", "fs", "PermissionsExt", "from_mode"];
45-
pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"];
4644
pub const REGEX_BUILDER_NEW: [&str; 3] = ["regex", "RegexBuilder", "new"];
4745
pub const REGEX_BYTES_BUILDER_NEW: [&str; 4] = ["regex", "bytes", "RegexBuilder", "new"];
4846
pub const REGEX_BYTES_NEW: [&str; 4] = ["regex", "bytes", "Regex", "new"];

0 commit comments

Comments
 (0)