Skip to content

Commit a786be5

Browse files
committed
[Clippy] Swap map_entry to use diagnostic items instead of paths
1 parent f8192ba commit a786be5

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ symbols! {
512512
breakpoint,
513513
bridge,
514514
bswap,
515+
btreemap_contains_key,
516+
btreemap_insert,
515517
btreeset_iter,
516518
builtin_syntax,
517519
c,
@@ -973,6 +975,8 @@ symbols! {
973975
half_open_range_patterns,
974976
half_open_range_patterns_in_slices,
975977
hash,
978+
hashmap_contains_key,
979+
hashmap_insert,
976980
hashset_iter,
977981
hexagon_target_feature,
978982
hidden,

library/alloc/src/collections/btree/map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
916916
/// assert_eq!(map.contains_key(&2), false);
917917
/// ```
918918
#[stable(feature = "rust1", since = "1.0.0")]
919+
#[cfg_attr(not(test), rustc_diagnostic_item = "btreemap_contains_key")]
919920
pub fn contains_key<Q: ?Sized>(&self, key: &Q) -> bool
920921
where
921922
K: Borrow<Q> + Ord,
@@ -981,6 +982,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
981982
/// ```
982983
#[stable(feature = "rust1", since = "1.0.0")]
983984
#[rustc_confusables("push", "put", "set")]
985+
#[cfg_attr(not(test), rustc_diagnostic_item = "btreemap_insert")]
984986
pub fn insert(&mut self, key: K, value: V) -> Option<V>
985987
where
986988
K: Ord,

library/std/src/collections/hash/map.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,6 +1037,7 @@ where
10371037
/// ```
10381038
#[inline]
10391039
#[stable(feature = "rust1", since = "1.0.0")]
1040+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_contains_key")]
10401041
pub fn contains_key<Q: ?Sized>(&self, k: &Q) -> bool
10411042
where
10421043
K: Borrow<Q>,
@@ -1100,6 +1101,7 @@ where
11001101
#[inline]
11011102
#[stable(feature = "rust1", since = "1.0.0")]
11021103
#[rustc_confusables("push", "append", "put")]
1104+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashmap_insert")]
11031105
pub fn insert(&mut self, k: K, v: V) -> Option<V> {
11041106
self.base.insert(k, v)
11051107
}

src/tools/clippy/clippy_lints/src/entry.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::{reindent_multiline, snippet_indent, snippet_with_applicability, snippet_with_context};
33
use clippy_utils::{
4-
can_move_expr_to_closure_no_visit, higher, is_expr_final_block_expr, is_expr_used_or_unified, match_def_path,
5-
paths, peel_hir_expr_while, SpanlessEq,
4+
can_move_expr_to_closure_no_visit, higher, is_expr_final_block_expr, is_expr_used_or_unified,
5+
peel_hir_expr_while, SpanlessEq,
66
};
77
use core::fmt::{self, Write};
88
use rustc_errors::Applicability;
@@ -11,7 +11,7 @@ use rustc_hir::intravisit::{walk_expr, Visitor};
1111
use rustc_hir::{Block, Expr, ExprKind, HirId, Pat, Stmt, StmtKind, UnOp};
1212
use rustc_lint::{LateContext, LateLintPass};
1313
use rustc_session::declare_lint_pass;
14-
use rustc_span::{Span, SyntaxContext, DUMMY_SP};
14+
use rustc_span::{sym, Span, SyntaxContext, DUMMY_SP};
1515

1616
declare_clippy_lint! {
1717
/// ### What it does
@@ -269,9 +269,9 @@ fn try_parse_contains<'tcx>(cx: &LateContext<'_>, expr: &'tcx Expr<'_>) -> Optio
269269
key,
270270
call_ctxt: expr.span.ctxt(),
271271
};
272-
if match_def_path(cx, id, &paths::BTREEMAP_CONTAINS_KEY) {
272+
if cx.tcx.is_diagnostic_item(sym::btreemap_contains_key, id) {
273273
Some((MapType::BTree, expr))
274-
} else if match_def_path(cx, id, &paths::HASHMAP_CONTAINS_KEY) {
274+
} else if cx.tcx.is_diagnostic_item(sym::hashmap_contains_key, id) {
275275
Some((MapType::Hash, expr))
276276
} else {
277277
None
@@ -306,7 +306,7 @@ struct InsertExpr<'tcx> {
306306
fn try_parse_insert<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<InsertExpr<'tcx>> {
307307
if let ExprKind::MethodCall(_, map, [key, value], _) = expr.kind {
308308
let id = cx.typeck_results().type_dependent_def_id(expr.hir_id)?;
309-
if match_def_path(cx, id, &paths::BTREEMAP_INSERT) || match_def_path(cx, id, &paths::HASHMAP_INSERT) {
309+
if cx.tcx.is_diagnostic_item(sym::btreemap_insert, id) || cx.tcx.is_diagnostic_item(sym::hashmap_insert, id) {
310310
Some(InsertExpr { map, key, value })
311311
} else {
312312
None

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [
1212
["rustc_lint_defs", "Applicability", "MachineApplicable"],
1313
];
1414
pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"];
15-
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
16-
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
1715
pub const CORE_RESULT_OK_METHOD: [&str; 4] = ["core", "result", "Result", "ok"];
1816
pub const CSTRING_AS_C_STR: [&str; 5] = ["alloc", "ffi", "c_str", "CString", "as_c_str"];
1917
pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"];
@@ -25,8 +23,6 @@ pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"];
2523
pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"];
2624
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
2725
pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"];
28-
pub const HASHMAP_CONTAINS_KEY: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "contains_key"];
29-
pub const HASHMAP_INSERT: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "insert"];
3026
pub const HASHMAP_ITER: [&str; 5] = ["std", "collections", "hash", "map", "Iter"];
3127
pub const HASHMAP_ITER_MUT: [&str; 5] = ["std", "collections", "hash", "map", "IterMut"];
3228
pub const HASHMAP_KEYS: [&str; 5] = ["std", "collections", "hash", "map", "Keys"];

0 commit comments

Comments
 (0)