Skip to content

Commit 039d103

Browse files
committed
[Clippy] Swap map_entry to use diagnostic items instead of paths
1 parent aaed38b commit 039d103

File tree

5 files changed

+14
-10
lines changed

5 files changed

+14
-10
lines changed

compiler/rustc_span/src/symbol.rs

+4
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,8 @@ symbols! {
511511
breakpoint,
512512
bridge,
513513
bswap,
514+
btreemap_contains_key,
515+
btreemap_insert,
514516
builtin_syntax,
515517
c,
516518
c_str,
@@ -971,6 +973,8 @@ symbols! {
971973
half_open_range_patterns,
972974
half_open_range_patterns_in_slices,
973975
hash,
976+
hashmap_contains_key,
977+
hashmap_insert,
974978
hexagon_target_feature,
975979
hidden,
976980
homogeneous_aggregate,

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

+2
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

+2
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

+6-6
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

-4
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ pub const APPLICABILITY_VALUES: [[&str; 3]; 4] = [
1313
];
1414
pub const DIAG: [&str; 2] = ["rustc_errors", "Diag"];
1515
pub const BINARYHEAP_ITER: [&str; 5] = ["alloc", "collections", "binary_heap", "BinaryHeap", "iter"];
16-
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
17-
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
1816
pub const BTREESET_ITER: [&str; 6] = ["alloc", "collections", "btree", "set", "BTreeSet", "iter"];
1917
pub const CORE_ITER_CLONED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "cloned"];
2018
pub const CORE_ITER_COPIED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "copied"];
@@ -30,8 +28,6 @@ pub const FILE_OPTIONS: [&str; 4] = ["std", "fs", "File", "options"];
3028
pub const FUTURES_IO_ASYNCREADEXT: [&str; 3] = ["futures_util", "io", "AsyncReadExt"];
3129
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
3230
pub const FUTURES_IO_ASYNCWRITEEXT: [&str; 3] = ["futures_util", "io", "AsyncWriteExt"];
33-
pub const HASHMAP_CONTAINS_KEY: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "contains_key"];
34-
pub const HASHMAP_INSERT: [&str; 6] = ["std", "collections", "hash", "map", "HashMap", "insert"];
3531
pub const HASHMAP_ITER: [&str; 5] = ["std", "collections", "hash", "map", "Iter"];
3632
pub const HASHMAP_ITER_MUT: [&str; 5] = ["std", "collections", "hash", "map", "IterMut"];
3733
pub const HASHMAP_KEYS: [&str; 5] = ["std", "collections", "hash", "map", "Keys"];

0 commit comments

Comments
 (0)