Skip to content

Commit 4bd9de5

Browse files
Rollup merge of #130522 - GnomedDev:clippy-manual-retain-paths, r=compiler-errors
[Clippy] Swap `manual_retain` to use diagnostic items instead of paths Part of rust-lang/rust-clippy#5393, just a chore.
2 parents 4d9ce4b + a18564c commit 4bd9de5

File tree

10 files changed

+35
-26
lines changed

10 files changed

+35
-26
lines changed

compiler/rustc_span/src/symbol.rs

+9
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ symbols! {
488488
begin_panic,
489489
bench,
490490
bin,
491+
binaryheap_iter,
491492
bind_by_move_pattern_guards,
492493
bindings_after_at,
493494
bitand,
@@ -511,6 +512,7 @@ symbols! {
511512
breakpoint,
512513
bridge,
513514
bswap,
515+
btreeset_iter,
514516
builtin_syntax,
515517
c,
516518
c_str,
@@ -971,6 +973,7 @@ symbols! {
971973
half_open_range_patterns,
972974
half_open_range_patterns_in_slices,
973975
hash,
976+
hashset_iter,
974977
hexagon_target_feature,
975978
hidden,
976979
homogeneous_aggregate,
@@ -1077,6 +1080,9 @@ symbols! {
10771080
item,
10781081
item_like_imports,
10791082
iter,
1083+
iter_cloned,
1084+
iter_copied,
1085+
iter_filter,
10801086
iter_mut,
10811087
iter_repeat,
10821088
iterator,
@@ -1817,6 +1823,7 @@ symbols! {
18171823
slice,
18181824
slice_from_raw_parts,
18191825
slice_from_raw_parts_mut,
1826+
slice_iter,
18201827
slice_len_fn,
18211828
slice_patterns,
18221829
slicing_syntax,
@@ -1849,6 +1856,7 @@ symbols! {
18491856
stop_after_dataflow,
18501857
store,
18511858
str,
1859+
str_chars,
18521860
str_from_utf8,
18531861
str_from_utf8_mut,
18541862
str_from_utf8_unchecked,
@@ -2064,6 +2072,7 @@ symbols! {
20642072
variant_count,
20652073
vec,
20662074
vec_macro,
2075+
vecdeque_iter,
20672076
version,
20682077
vfp2,
20692078
vis,

library/alloc/src/collections/binary_heap/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ impl<T, A: Allocator> BinaryHeap<T, A> {
959959
/// }
960960
/// ```
961961
#[stable(feature = "rust1", since = "1.0.0")]
962+
#[cfg_attr(not(test), rustc_diagnostic_item = "binaryheap_iter")]
962963
pub fn iter(&self) -> Iter<'_, T> {
963964
Iter { iter: self.data.iter() }
964965
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,7 @@ impl<T, A: Allocator + Clone> BTreeSet<T, A> {
11321132
/// assert_eq!(set_iter.next(), None);
11331133
/// ```
11341134
#[stable(feature = "rust1", since = "1.0.0")]
1135+
#[cfg_attr(not(test), rustc_diagnostic_item = "btreeset_iter")]
11351136
pub fn iter(&self) -> Iter<'_, T> {
11361137
Iter { iter: self.map.keys() }
11371138
}

library/alloc/src/collections/vec_deque/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,7 @@ impl<T, A: Allocator> VecDeque<T, A> {
12011201
/// assert_eq!(&c[..], b);
12021202
/// ```
12031203
#[stable(feature = "rust1", since = "1.0.0")]
1204+
#[cfg_attr(not(test), rustc_diagnostic_item = "vecdeque_iter")]
12041205
pub fn iter(&self) -> Iter<'_, T> {
12051206
let (a, b) = self.as_slices();
12061207
Iter::new(a.iter(), b.iter())

library/core/src/iter/traits/iterator.rs

+3
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@ pub trait Iterator {
876876
#[inline]
877877
#[stable(feature = "rust1", since = "1.0.0")]
878878
#[rustc_do_not_const_check]
879+
#[cfg_attr(not(test), rustc_diagnostic_item = "iter_filter")]
879880
fn filter<P>(self, predicate: P) -> Filter<Self, P>
880881
where
881882
Self: Sized,
@@ -3412,6 +3413,7 @@ pub trait Iterator {
34123413
/// ```
34133414
#[stable(feature = "iter_copied", since = "1.36.0")]
34143415
#[rustc_do_not_const_check]
3416+
#[cfg_attr(not(test), rustc_diagnostic_item = "iter_copied")]
34153417
fn copied<'a, T: 'a>(self) -> Copied<Self>
34163418
where
34173419
Self: Sized + Iterator<Item = &'a T>,
@@ -3460,6 +3462,7 @@ pub trait Iterator {
34603462
/// ```
34613463
#[stable(feature = "rust1", since = "1.0.0")]
34623464
#[rustc_do_not_const_check]
3465+
#[cfg_attr(not(test), rustc_diagnostic_item = "iter_cloned")]
34633466
fn cloned<'a, T: 'a>(self) -> Cloned<Self>
34643467
where
34653468
Self: Sized + Iterator<Item = &'a T>,

library/core/src/slice/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ impl<T> [T] {
10101010
/// ```
10111011
#[stable(feature = "rust1", since = "1.0.0")]
10121012
#[inline]
1013+
#[cfg_attr(not(test), rustc_diagnostic_item = "slice_iter")]
10131014
pub fn iter(&self) -> Iter<'_, T> {
10141015
Iter::new(self)
10151016
}

library/core/src/str/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ impl str {
832832
/// ```
833833
#[stable(feature = "rust1", since = "1.0.0")]
834834
#[inline]
835+
#[cfg_attr(not(test), rustc_diagnostic_item = "str_chars")]
835836
pub fn chars(&self) -> Chars<'_> {
836837
Chars { iter: self.as_bytes().iter() }
837838
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ impl<T, S> HashSet<T, S> {
187187
#[inline]
188188
#[rustc_lint_query_instability]
189189
#[stable(feature = "rust1", since = "1.0.0")]
190+
#[cfg_attr(not(test), rustc_diagnostic_item = "hashset_iter")]
190191
pub fn iter(&self) -> Iter<'_, T> {
191192
Iter { base: self.base.iter() }
192193
}

src/tools/clippy/clippy_lints/src/manual_retain.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ use clippy_config::Conf;
33
use clippy_utils::diagnostics::span_lint_and_sugg;
44
use clippy_utils::source::snippet;
55
use clippy_utils::ty::{get_type_diagnostic_name, is_type_lang_item};
6-
use clippy_utils::{match_def_path, paths, SpanlessEq};
6+
use clippy_utils::SpanlessEq;
77
use rustc_errors::Applicability;
88
use rustc_hir as hir;
99
use rustc_hir::def_id::DefId;
1010
use rustc_hir::ExprKind::Assign;
1111
use rustc_lint::{LateContext, LateLintPass};
1212
use rustc_session::impl_lint_pass;
13-
use rustc_span::symbol::sym;
13+
use rustc_span::symbol::{sym, Symbol};
1414
use rustc_span::Span;
1515

16-
const ACCEPTABLE_METHODS: [&[&str]; 5] = [
17-
&paths::BINARYHEAP_ITER,
18-
&paths::HASHSET_ITER,
19-
&paths::BTREESET_ITER,
20-
&paths::SLICE_INTO,
21-
&paths::VEC_DEQUE_ITER,
16+
const ACCEPTABLE_METHODS: [Symbol; 5] = [
17+
sym::binaryheap_iter,
18+
sym::hashset_iter,
19+
sym::btreeset_iter,
20+
sym::slice_iter,
21+
sym::vecdeque_iter,
2222
];
2323

2424
declare_clippy_lint! {
@@ -84,7 +84,7 @@ fn check_into_iter(
8484
) {
8585
if let hir::ExprKind::MethodCall(_, into_iter_expr, [_], _) = &target_expr.kind
8686
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id)
87-
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)
87+
&& cx.tcx.is_diagnostic_item(sym::iter_filter, filter_def_id)
8888
&& let hir::ExprKind::MethodCall(_, struct_expr, [], _) = &into_iter_expr.kind
8989
&& let Some(into_iter_def_id) = cx.typeck_results().type_dependent_def_id(into_iter_expr.hir_id)
9090
&& Some(into_iter_def_id) == cx.tcx.lang_items().into_iter_fn()
@@ -127,14 +127,14 @@ fn check_iter(
127127
) {
128128
if let hir::ExprKind::MethodCall(_, filter_expr, [], _) = &target_expr.kind
129129
&& let Some(copied_def_id) = cx.typeck_results().type_dependent_def_id(target_expr.hir_id)
130-
&& (match_def_path(cx, copied_def_id, &paths::CORE_ITER_COPIED)
131-
|| match_def_path(cx, copied_def_id, &paths::CORE_ITER_CLONED))
130+
&& (cx.tcx.is_diagnostic_item(sym::iter_copied, copied_def_id)
131+
|| cx.tcx.is_diagnostic_item(sym::iter_cloned, copied_def_id))
132132
&& let hir::ExprKind::MethodCall(_, iter_expr, [_], _) = &filter_expr.kind
133133
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id)
134-
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)
134+
&& cx.tcx.is_diagnostic_item(sym::iter_filter, filter_def_id)
135135
&& let hir::ExprKind::MethodCall(_, struct_expr, [], _) = &iter_expr.kind
136136
&& let Some(iter_expr_def_id) = cx.typeck_results().type_dependent_def_id(iter_expr.hir_id)
137-
&& match_acceptable_def_path(cx, iter_expr_def_id)
137+
&& match_acceptable_sym(cx, iter_expr_def_id)
138138
&& match_acceptable_type(cx, left_expr, msrv)
139139
&& SpanlessEq::new(cx).eq_expr(left_expr, struct_expr)
140140
&& let hir::ExprKind::MethodCall(_, _, [closure_expr], _) = filter_expr.kind
@@ -189,10 +189,10 @@ fn check_to_owned(
189189
&& cx.tcx.is_diagnostic_item(sym::to_owned_method, to_owned_def_id)
190190
&& let hir::ExprKind::MethodCall(_, chars_expr, [_], _) = &filter_expr.kind
191191
&& let Some(filter_def_id) = cx.typeck_results().type_dependent_def_id(filter_expr.hir_id)
192-
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)
192+
&& cx.tcx.is_diagnostic_item(sym::iter_filter, filter_def_id)
193193
&& let hir::ExprKind::MethodCall(_, str_expr, [], _) = &chars_expr.kind
194194
&& let Some(chars_expr_def_id) = cx.typeck_results().type_dependent_def_id(chars_expr.hir_id)
195-
&& match_def_path(cx, chars_expr_def_id, &paths::STR_CHARS)
195+
&& cx.tcx.is_diagnostic_item(sym::str_chars, chars_expr_def_id)
196196
&& let ty = cx.typeck_results().expr_ty(str_expr).peel_refs()
197197
&& is_type_lang_item(cx, ty, hir::LangItem::String)
198198
&& SpanlessEq::new(cx).eq_expr(left_expr, str_expr)
@@ -247,10 +247,10 @@ fn make_sugg(
247247
}
248248
}
249249

250-
fn match_acceptable_def_path(cx: &LateContext<'_>, collect_def_id: DefId) -> bool {
250+
fn match_acceptable_sym(cx: &LateContext<'_>, collect_def_id: DefId) -> bool {
251251
ACCEPTABLE_METHODS
252252
.iter()
253-
.any(|&method| match_def_path(cx, collect_def_id, method))
253+
.any(|&method| cx.tcx.is_diagnostic_item(method, collect_def_id))
254254
}
255255

256256
fn match_acceptable_type(cx: &LateContext<'_>, expr: &hir::Expr<'_>, msrv: &Msrv) -> bool {

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

-9
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,8 @@ 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 BINARYHEAP_ITER: [&str; 5] = ["alloc", "collections", "binary_heap", "BinaryHeap", "iter"];
1615
pub const BTREEMAP_CONTAINS_KEY: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "contains_key"];
1716
pub const BTREEMAP_INSERT: [&str; 6] = ["alloc", "collections", "btree", "map", "BTreeMap", "insert"];
18-
pub const BTREESET_ITER: [&str; 6] = ["alloc", "collections", "btree", "set", "BTreeSet", "iter"];
19-
pub const CORE_ITER_CLONED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "cloned"];
20-
pub const CORE_ITER_COPIED: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "copied"];
21-
pub const CORE_ITER_FILTER: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "filter"];
2217
pub const CORE_RESULT_OK_METHOD: [&str; 4] = ["core", "result", "Result", "ok"];
2318
pub const CSTRING_AS_C_STR: [&str; 5] = ["alloc", "ffi", "c_str", "CString", "as_c_str"];
2419
pub const EARLY_CONTEXT: [&str; 2] = ["rustc_lint", "EarlyContext"];
@@ -39,7 +34,6 @@ pub const HASHMAP_VALUES: [&str; 5] = ["std", "collections", "hash", "map", "Val
3934
pub const HASHMAP_DRAIN: [&str; 5] = ["std", "collections", "hash", "map", "Drain"];
4035
pub const HASHMAP_VALUES_MUT: [&str; 5] = ["std", "collections", "hash", "map", "ValuesMut"];
4136
pub const HASHSET_ITER_TY: [&str; 5] = ["std", "collections", "hash", "set", "Iter"];
42-
pub const HASHSET_ITER: [&str; 6] = ["std", "collections", "hash", "set", "HashSet", "iter"];
4337
pub const HASHSET_DRAIN: [&str; 5] = ["std", "collections", "hash", "set", "Drain"];
4438
pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
4539
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
@@ -71,13 +65,11 @@ pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"];
7165
pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"];
7266
pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
7367
pub const SLICE_INTO_VEC: [&str; 4] = ["alloc", "slice", "<impl [T]>", "into_vec"];
74-
pub const SLICE_INTO: [&str; 4] = ["core", "slice", "<impl [T]>", "iter"];
7568
pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"];
7669
pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"];
7770
pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"];
7871
pub const STRING_AS_STR: [&str; 4] = ["alloc", "string", "String", "as_str"];
7972
pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"];
80-
pub const STR_CHARS: [&str; 4] = ["core", "str", "<impl str>", "chars"];
8173
pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
8274
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];
8375
pub const STR_STARTS_WITH: [&str; 4] = ["core", "str", "<impl str>", "starts_with"];
@@ -100,7 +92,6 @@ pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "Op
10092
pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"];
10193
pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
10294
pub const VEC_AS_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_slice"];
103-
pub const VEC_DEQUE_ITER: [&str; 5] = ["alloc", "collections", "vec_deque", "VecDeque", "iter"];
10495
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
10596
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
10697
pub const VEC_WITH_CAPACITY: [&str; 4] = ["alloc", "vec", "Vec", "with_capacity"];

0 commit comments

Comments
 (0)