Skip to content

Commit efb9ee2

Browse files
author
Joshua Nelson
authored
Rollup merge of #82578 - camsteffen:diag-items, r=oli-obk
Add some diagnostic items for Clippy
2 parents 4f14f17 + 6a3b834 commit efb9ee2

File tree

15 files changed

+55
-41
lines changed

15 files changed

+55
-41
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ symbols! {
126126
Argument,
127127
ArgumentV1,
128128
Arguments,
129+
BTreeMap,
130+
BTreeSet,
131+
BinaryHeap,
129132
C,
130133
CString,
131134
Center,
@@ -163,6 +166,7 @@ symbols! {
163166
Iterator,
164167
Layout,
165168
Left,
169+
LinkedList,
166170
LintPass,
167171
None,
168172
Ok,
@@ -191,6 +195,7 @@ symbols! {
191195
RangeToInclusive,
192196
Rc,
193197
Ready,
198+
Receiver,
194199
Result,
195200
Return,
196201
Right,
@@ -592,6 +597,8 @@ symbols! {
592597
gt,
593598
half_open_range_patterns,
594599
hash,
600+
hashmap_type,
601+
hashset_type,
595602
hexagon_target_feature,
596603
hidden,
597604
homogeneous_aggregate,
@@ -1256,6 +1263,7 @@ symbols! {
12561263
variant_count,
12571264
vec,
12581265
vec_type,
1266+
vecdeque_type,
12591267
version,
12601268
vis,
12611269
visible_private_types,

library/alloc/src/collections/binary_heap.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ use super::SpecExtend;
247247
/// [peek]: BinaryHeap::peek
248248
/// [peek\_mut]: BinaryHeap::peek_mut
249249
#[stable(feature = "rust1", since = "1.0.0")]
250+
#[cfg_attr(not(test), rustc_diagnostic_item = "BinaryHeap")]
250251
pub struct BinaryHeap<T> {
251252
data: Vec<T>,
252253
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ pub(super) const MIN_LEN: usize = node::MIN_LEN_AFTER_SPLIT;
138138
/// *stat += random_stat_buff();
139139
/// ```
140140
#[stable(feature = "rust1", since = "1.0.0")]
141+
#[cfg_attr(not(test), rustc_diagnostic_item = "BTreeMap")]
141142
pub struct BTreeMap<K, V> {
142143
root: Option<Root<K, V>>,
143144
length: usize,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ use super::Recover;
6161
/// ```
6262
#[derive(Hash, PartialEq, Eq, Ord, PartialOrd)]
6363
#[stable(feature = "rust1", since = "1.0.0")]
64+
#[cfg_attr(not(test), rustc_diagnostic_item = "BTreeSet")]
6465
pub struct BTreeSet<T> {
6566
map: BTreeMap<T, ()>,
6667
}

library/alloc/src/collections/linked_list.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ mod tests;
3535
/// array-based containers are generally faster,
3636
/// more memory efficient, and make better use of CPU cache.
3737
#[stable(feature = "rust1", since = "1.0.0")]
38+
#[cfg_attr(not(test), rustc_diagnostic_item = "LinkedList")]
3839
pub struct LinkedList<T> {
3940
head: Option<NonNull<Node<T>>>,
4041
tail: Option<NonNull<Node<T>>>,

library/std/src/sync/mpsc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ mod cache_aligned;
310310
/// println!("{}", recv.recv().unwrap()); // Received after 2 seconds
311311
/// ```
312312
#[stable(feature = "rust1", since = "1.0.0")]
313+
#[cfg_attr(not(test), rustc_diagnostic_item = "Receiver")]
313314
pub struct Receiver<T> {
314315
inner: UnsafeCell<Flavor<T>>,
315316
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::hir::map::Map;
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
1111
use rustc_span::source_map::Span;
12+
use rustc_span::sym;
1213

1314
declare_clippy_lint! {
1415
/// **What it does:** Checks for uses of `contains_key` + `insert` on `HashMap`
@@ -111,7 +112,7 @@ fn check_cond<'a>(cx: &LateContext<'_>, check: &'a Expr<'a>) -> Option<(&'static
111112
return if match_type(cx, obj_ty, &paths::BTREEMAP) {
112113
Some(("BTreeMap", map, key))
113114
}
114-
else if is_type_diagnostic_item(cx, obj_ty, sym!(hashmap_type)) {
115+
else if is_type_diagnostic_item(cx, obj_ty, sym::hashmap_type) {
115116
Some(("HashMap", map, key))
116117
}
117118
else {

src/tools/clippy/clippy_lints/src/loops.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ fn is_slice_like<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'_>) -> bool {
10101010
_ => false,
10111011
};
10121012

1013-
is_slice || is_type_diagnostic_item(cx, ty, sym::vec_type) || is_type_diagnostic_item(cx, ty, sym!(vecdeque_type))
1013+
is_slice || is_type_diagnostic_item(cx, ty, sym::vec_type) || is_type_diagnostic_item(cx, ty, sym::vecdeque_type)
10141014
}
10151015

10161016
fn fetch_cloned_expr<'tcx>(expr: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> {
@@ -1908,7 +1908,7 @@ fn check_for_loop_over_map_kv<'tcx>(
19081908
_ => arg,
19091909
};
19101910

1911-
if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) || match_type(cx, ty, &paths::BTREEMAP) {
1911+
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || match_type(cx, ty, &paths::BTREEMAP) {
19121912
span_lint_and_then(
19131913
cx,
19141914
FOR_KV_MAP,
@@ -2386,9 +2386,9 @@ fn is_ref_iterable_type(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
23862386
is_iterable_array(ty, cx) ||
23872387
is_type_diagnostic_item(cx, ty, sym::vec_type) ||
23882388
match_type(cx, ty, &paths::LINKED_LIST) ||
2389-
is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) ||
2390-
is_type_diagnostic_item(cx, ty, sym!(hashset_type)) ||
2391-
is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) ||
2389+
is_type_diagnostic_item(cx, ty, sym::hashmap_type) ||
2390+
is_type_diagnostic_item(cx, ty, sym::hashset_type) ||
2391+
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
23922392
match_type(cx, ty, &paths::BINARY_HEAP) ||
23932393
match_type(cx, ty, &paths::BTREEMAP) ||
23942394
match_type(cx, ty, &paths::BTREESET)
@@ -2922,9 +2922,9 @@ fn check_needless_collect_direct_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCont
29222922
then {
29232923
let ty = cx.typeck_results().node_type(ty.hir_id);
29242924
if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
2925-
is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) ||
2925+
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
29262926
match_type(cx, ty, &paths::BTREEMAP) ||
2927-
is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) {
2927+
is_type_diagnostic_item(cx, ty, sym::hashmap_type) {
29282928
if method.ident.name == sym!(len) {
29292929
let span = shorten_needless_collect_span(expr);
29302930
span_lint_and_sugg(
@@ -2992,7 +2992,7 @@ fn check_needless_collect_indirect_usage<'tcx>(expr: &'tcx Expr<'_>, cx: &LateCo
29922992
if let Some(GenericArg::Type(ref ty)) = generic_args.args.get(0);
29932993
if let ty = cx.typeck_results().node_type(ty.hir_id);
29942994
if is_type_diagnostic_item(cx, ty, sym::vec_type) ||
2995-
is_type_diagnostic_item(cx, ty, sym!(vecdeque_type)) ||
2995+
is_type_diagnostic_item(cx, ty, sym::vecdeque_type) ||
29962996
match_type(cx, ty, &paths::LINKED_LIST);
29972997
if let Some(iter_calls) = detect_iter_and_into_iters(block, *ident);
29982998
if iter_calls.len() == 1;

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_middle::ty::{self, TraitRef, Ty, TyS};
2424
use rustc_semver::RustcVersion;
2525
use rustc_session::{declare_tool_lint, impl_lint_pass};
2626
use rustc_span::source_map::Span;
27-
use rustc_span::symbol::{sym, SymbolStr};
27+
use rustc_span::symbol::{sym, Symbol, SymbolStr};
2828
use rustc_typeck::hir_ty_to_ty;
2929

3030
use crate::consts::{constant, Constant};
@@ -2598,7 +2598,7 @@ fn lint_iter_nth<'tcx>(
25982598
"slice"
25992599
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vec_type) {
26002600
"Vec"
2601-
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym!(vecdeque_type)) {
2601+
} else if is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(&iter_args[0]), sym::vecdeque_type) {
26022602
"VecDeque"
26032603
} else {
26042604
let nth_args = nth_and_iter_args[0];
@@ -2652,10 +2652,10 @@ fn lint_get_unwrap<'tcx>(cx: &LateContext<'tcx>, expr: &hir::Expr<'_>, get_args:
26522652
} else if is_type_diagnostic_item(cx, expr_ty, sym::vec_type) {
26532653
needs_ref = get_args_str.parse::<usize>().is_ok();
26542654
"Vec"
2655-
} else if is_type_diagnostic_item(cx, expr_ty, sym!(vecdeque_type)) {
2655+
} else if is_type_diagnostic_item(cx, expr_ty, sym::vecdeque_type) {
26562656
needs_ref = get_args_str.parse::<usize>().is_ok();
26572657
"VecDeque"
2658-
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym!(hashmap_type)) {
2658+
} else if !is_mut && is_type_diagnostic_item(cx, expr_ty, sym::hashmap_type) {
26592659
needs_ref = true;
26602660
"HashMap"
26612661
} else if !is_mut && match_type(cx, expr_ty, &paths::BTREEMAP) {
@@ -3619,7 +3619,7 @@ fn lint_asref(cx: &LateContext<'_>, expr: &hir::Expr<'_>, call_name: &str, as_re
36193619
}
36203620
}
36213621

3622-
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(&'static str, &'static str)> {
3622+
fn ty_has_iter_method(cx: &LateContext<'_>, self_ref_ty: Ty<'_>) -> Option<(Symbol, &'static str)> {
36233623
has_iter_method(cx, self_ref_ty).map(|ty_name| {
36243624
let mutbl = match self_ref_ty.kind() {
36253625
ty::Ref(_, _, mutbl) => mutbl,

src/tools/clippy/clippy_lints/src/swap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ fn check_for_slice<'a>(cx: &LateContext<'_>, lhs1: &'a Expr<'_>, lhs2: &'a Expr<
199199
if matches!(ty.kind(), ty::Slice(_))
200200
|| matches!(ty.kind(), ty::Array(_, _))
201201
|| is_type_diagnostic_item(cx, ty, sym::vec_type)
202-
|| is_type_diagnostic_item(cx, ty, sym!(vecdeque_type))
202+
|| is_type_diagnostic_item(cx, ty, sym::vecdeque_type)
203203
{
204204
return Slice::Swappable(lhs1, idx1, idx2);
205205
}

src/tools/clippy/clippy_lints/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2680,14 +2680,14 @@ impl<'tcx> ImplicitHasherType<'tcx> {
26802680

26812681
let ty = hir_ty_to_ty(cx.tcx, hir_ty);
26822682

2683-
if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) && params_len == 2 {
2683+
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) && params_len == 2 {
26842684
Some(ImplicitHasherType::HashMap(
26852685
hir_ty.span,
26862686
ty,
26872687
snippet(cx, params[0].span, "K"),
26882688
snippet(cx, params[1].span, "V"),
26892689
))
2690-
} else if is_type_diagnostic_item(cx, ty, sym!(hashset_type)) && params_len == 1 {
2690+
} else if is_type_diagnostic_item(cx, ty, sym::hashset_type) && params_len == 1 {
26912691
Some(ImplicitHasherType::HashSet(
26922692
hir_ty.span,
26932693
ty,

src/tools/clippy/clippy_lints/src/zero_sized_map_values.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_middle::ty::{Adt, Ty};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
66
use rustc_target::abi::LayoutOf as _;
77
use rustc_typeck::hir_ty_to_ty;
8+
use rustc_span::sym;
89

910
use crate::utils::{is_normalizable, is_type_diagnostic_item, match_type, paths, span_lint_and_help};
1011

@@ -47,7 +48,7 @@ impl LateLintPass<'_> for ZeroSizedMapValues {
4748
if !hir_ty.span.from_expansion();
4849
if !in_trait_impl(cx, hir_ty.hir_id);
4950
let ty = ty_from_hir_ty(cx, hir_ty);
50-
if is_type_diagnostic_item(cx, ty, sym!(hashmap_type)) || match_type(cx, ty, &paths::BTREEMAP);
51+
if is_type_diagnostic_item(cx, ty, sym::hashmap_type) || match_type(cx, ty, &paths::BTREEMAP);
5152
if let Adt(_, ref substs) = ty.kind();
5253
let ty = substs.type_at(1);
5354
// Do this to prevent `layout_of` crashing, being unable to fully normalize `ty`.

src/tools/clippy/clippy_utils/src/eager_or_lazy.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_hir::intravisit::{NestedVisitorMap, Visitor};
1818
use rustc_hir::{Block, Expr, ExprKind, Path, QPath};
1919
use rustc_lint::LateContext;
2020
use rustc_middle::hir::map::Map;
21+
use rustc_span::sym;
2122

2223
/// Is the expr pure (is it free from side-effects)?
2324
/// This function is named so to stress that it isn't exhaustive and returns FNs.
@@ -99,7 +100,7 @@ fn identify_some_potentially_expensive_patterns<'tcx>(cx: &LateContext<'tcx>, ex
99100
ExprKind::Call(..) => !is_ctor_or_promotable_const_function(self.cx, expr),
100101
ExprKind::Index(obj, _) => {
101102
let ty = self.cx.typeck_results().expr_ty(obj);
102-
is_type_diagnostic_item(self.cx, ty, sym!(hashmap_type))
103+
is_type_diagnostic_item(self.cx, ty, sym::hashmap_type)
103104
|| match_type(self.cx, ty, &paths::BTREEMAP)
104105
},
105106
ExprKind::MethodCall(..) => true,

src/tools/clippy/clippy_utils/src/lib.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,24 +1295,24 @@ pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_>, node: HirId) -> bool
12951295
}
12961296

12971297
/// Returns true if ty has `iter` or `iter_mut` methods
1298-
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<&'static str> {
1298+
pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<Symbol> {
12991299
// FIXME: instead of this hard-coded list, we should check if `<adt>::iter`
13001300
// exists and has the desired signature. Unfortunately FnCtxt is not exported
13011301
// so we can't use its `lookup_method` method.
1302-
let into_iter_collections: [&[&str]; 13] = [
1303-
&paths::VEC,
1304-
&paths::OPTION,
1305-
&paths::RESULT,
1306-
&paths::BTREESET,
1307-
&paths::BTREEMAP,
1308-
&paths::VEC_DEQUE,
1309-
&paths::LINKED_LIST,
1310-
&paths::BINARY_HEAP,
1311-
&paths::HASHSET,
1312-
&paths::HASHMAP,
1313-
&paths::PATH_BUF,
1314-
&paths::PATH,
1315-
&paths::RECEIVER,
1302+
let into_iter_collections: &[Symbol] = &[
1303+
sym::vec_type,
1304+
sym::option_type,
1305+
sym::result_type,
1306+
sym::BTreeMap,
1307+
sym::BTreeSet,
1308+
sym::vecdeque_type,
1309+
sym::LinkedList,
1310+
sym::BinaryHeap,
1311+
sym::hashset_type,
1312+
sym::hashmap_type,
1313+
sym::PathBuf,
1314+
sym::Path,
1315+
sym::Receiver,
13161316
];
13171317

13181318
let ty_to_check = match probably_ref_ty.kind() {
@@ -1321,15 +1321,15 @@ pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<
13211321
};
13221322

13231323
let def_id = match ty_to_check.kind() {
1324-
ty::Array(..) => return Some("array"),
1325-
ty::Slice(..) => return Some("slice"),
1324+
ty::Array(..) => return Some(sym::array),
1325+
ty::Slice(..) => return Some(sym::slice),
13261326
ty::Adt(adt, _) => adt.did,
13271327
_ => return None,
13281328
};
13291329

1330-
for path in &into_iter_collections {
1331-
if match_def_path(cx, def_id, path) {
1332-
return Some(*path.last().unwrap());
1330+
for &name in into_iter_collections {
1331+
if cx.tcx.is_diagnostic_item(name, def_id) {
1332+
return Some(cx.tcx.item_name(def_id));
13331333
}
13341334
}
13351335
None

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ pub(super) const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
9999
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
100100
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
101101
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];
102-
pub const PATH: [&str; 3] = ["std", "path", "Path"];
103102
pub const PATH_BUF: [&str; 3] = ["std", "path", "PathBuf"];
104103
pub const PATH_BUF_AS_PATH: [&str; 4] = ["std", "path", "PathBuf", "as_path"];
105104
pub const PATH_TO_PATH_BUF: [&str; 4] = ["std", "path", "Path", "to_path_buf"];
@@ -116,7 +115,6 @@ pub const PUSH_STR: [&str; 4] = ["alloc", "string", "String", "push_str"];
116115
pub const RANGE_ARGUMENT_TRAIT: [&str; 3] = ["core", "ops", "RangeBounds"];
117116
pub const RC: [&str; 3] = ["alloc", "rc", "Rc"];
118117
pub const RC_PTR_EQ: [&str; 4] = ["alloc", "rc", "Rc", "ptr_eq"];
119-
pub const RECEIVER: [&str; 4] = ["std", "sync", "mpsc", "Receiver"];
120118
pub const REFCELL_REF: [&str; 3] = ["core", "cell", "Ref"];
121119
pub const REFCELL_REFMUT: [&str; 3] = ["core", "cell", "RefMut"];
122120
pub const REGEX_BUILDER_NEW: [&str; 5] = ["regex", "re_builder", "unicode", "RegexBuilder", "new"];

0 commit comments

Comments
 (0)