Skip to content

Commit f222630

Browse files
committed
[Clippy] Swap manual_retain to use diagnostic items instead of paths
1 parent aaed38b commit f222630

File tree

8 files changed

+20
-15
lines changed

8 files changed

+20
-15
lines changed

compiler/rustc_span/src/symbol.rs

+5
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,
@@ -1817,6 +1820,7 @@ symbols! {
18171820
slice,
18181821
slice_from_raw_parts,
18191822
slice_from_raw_parts_mut,
1823+
slice_iter,
18201824
slice_len_fn,
18211825
slice_patterns,
18221826
slicing_syntax,
@@ -2064,6 +2068,7 @@ symbols! {
20642068
variant_count,
20652069
vec,
20662070
vec_macro,
2071+
vecdeque_iter,
20672072
version,
20682073
vfp2,
20692074
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/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/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

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ 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! {
@@ -134,7 +134,7 @@ fn check_iter(
134134
&& match_def_path(cx, filter_def_id, &paths::CORE_ITER_FILTER)
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
@@ -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

-5
Original file line numberDiff line numberDiff line change
@@ -12,10 +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"];
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"];
2119
pub const CORE_ITER_FILTER: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "filter"];
@@ -39,7 +37,6 @@ pub const HASHMAP_VALUES: [&str; 5] = ["std", "collections", "hash", "map", "Val
3937
pub const HASHMAP_DRAIN: [&str; 5] = ["std", "collections", "hash", "map", "Drain"];
4038
pub const HASHMAP_VALUES_MUT: [&str; 5] = ["std", "collections", "hash", "map", "ValuesMut"];
4139
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"];
4340
pub const HASHSET_DRAIN: [&str; 5] = ["std", "collections", "hash", "set", "Drain"];
4441
pub const IDENT: [&str; 3] = ["rustc_span", "symbol", "Ident"];
4542
pub const IDENT_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Ident", "as_str"];
@@ -71,7 +68,6 @@ pub const REGEX_SET_NEW: [&str; 3] = ["regex", "RegexSet", "new"];
7168
pub const SERDE_DESERIALIZE: [&str; 3] = ["serde", "de", "Deserialize"];
7269
pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
7370
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"];
7571
pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"];
7672
pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"];
7773
pub const STRING_AS_MUT_STR: [&str; 4] = ["alloc", "string", "String", "as_mut_str"];
@@ -100,7 +96,6 @@ pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "Op
10096
pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"];
10197
pub const VEC_AS_MUT_SLICE: [&str; 4] = ["alloc", "vec", "Vec", "as_mut_slice"];
10298
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"];
10499
pub const VEC_FROM_ELEM: [&str; 3] = ["alloc", "vec", "from_elem"];
105100
pub const VEC_NEW: [&str; 4] = ["alloc", "vec", "Vec", "new"];
106101
pub const VEC_WITH_CAPACITY: [&str; 4] = ["alloc", "vec", "Vec", "with_capacity"];

0 commit comments

Comments
 (0)