Skip to content

Commit 14230a7

Browse files
committed
Simplify clippy fix.
1 parent 8c78400 commit 14230a7

File tree

4 files changed

+23
-41
lines changed

4 files changed

+23
-41
lines changed

compiler/rustc_span/src/symbol.rs

+1
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,7 @@ symbols! {
214214
IntoIterator,
215215
IoRead,
216216
IoWrite,
217+
IpAddr,
217218
IrTyKind,
218219
Is,
219220
ItemContext,

library/std/src/net/ip_addr.rs

-2
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ use super::display_buffer::DisplayBuffer;
3434
#[derive(Copy, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
3535
pub enum IpAddr {
3636
/// An IPv4 address.
37-
#[cfg_attr(not(test), rustc_diagnostic_item = "ip_addr_variant_v4")]
3837
#[stable(feature = "ip_addr", since = "1.7.0")]
3938
V4(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv4Addr),
4039
/// An IPv6 address.
41-
#[cfg_attr(not(test), rustc_diagnostic_item = "ip_addr_variant_v6")]
4240
#[stable(feature = "ip_addr", since = "1.7.0")]
4341
V6(#[stable(feature = "ip_addr", since = "1.7.0")] Ipv6Addr),
4442
}

src/tools/clippy/clippy_lints/src/matches/redundant_pattern_match.rs

+22-37
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ use clippy_utils::source::snippet;
44
use clippy_utils::sugg::Sugg;
55
use clippy_utils::ty::{is_type_diagnostic_item, needs_ordered_drop};
66
use clippy_utils::visitors::any_temporaries_need_ordered_drop;
7-
use clippy_utils::{higher, is_lang_ctor, is_trait_method, match_def_path, paths};
7+
use clippy_utils::{higher, is_lang_ctor, is_trait_method};
88
use if_chain::if_chain;
99
use rustc_ast::ast::LitKind;
1010
use rustc_errors::Applicability;
11-
use rustc_hir::LangItem::{OptionNone, PollPending};
11+
use rustc_hir::LangItem::{self, OptionSome, OptionNone, PollPending, PollReady, ResultOk, ResultErr};
1212
use rustc_hir::{Arm, Expr, ExprKind, Node, Pat, PatKind, QPath, UnOp};
1313
use rustc_lint::LateContext;
1414
use rustc_middle::ty::{self, subst::GenericArgKind, DefIdTree, Ty};
15-
use rustc_span::{sym, Symbol, def_id::DefId};
15+
use rustc_span::{sym, Symbol};
1616

1717
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
1818
if let Some(higher::WhileLet { let_pat, let_expr, .. }) = higher::WhileLet::hir(expr) {
@@ -75,9 +75,9 @@ fn find_sugg_for_if_let<'tcx>(
7575
("is_some()", op_ty)
7676
} else if Some(id) == lang_items.poll_ready_variant() {
7777
("is_ready()", op_ty)
78-
} else if is_pat_variant(cx, check_pat, qpath, &paths::IPADDR_V4, Item::Diag(sym!(IpAddr), sym!(V4))) {
78+
} else if is_pat_variant(cx, check_pat, qpath, Item::Diag(sym::IpAddr, sym!(V4))) {
7979
("is_ipv4()", op_ty)
80-
} else if is_pat_variant(cx, check_pat, qpath, &paths::IPADDR_V6, Item::Diag(sym!(IpAddr), sym!(V6))) {
80+
} else if is_pat_variant(cx, check_pat, qpath, Item::Diag(sym::IpAddr, sym!(V6))) {
8181
("is_ipv6()", op_ty)
8282
} else {
8383
return;
@@ -174,7 +174,6 @@ fn find_sugg_for_if_let<'tcx>(
174174

175175
pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op: &Expr<'_>, arms: &[Arm<'_>]) {
176176
if arms.len() == 2 {
177-
let lang_items = cx.tcx.lang_items();
178177
let node_pair = (&arms[0].pat.kind, &arms[1].pat.kind);
179178

180179
let found_good_method = match node_pair {
@@ -188,10 +187,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
188187
arms,
189188
path_left,
190189
path_right,
191-
&paths::RESULT_OK,
192-
Item::Lang(lang_items.result_ok_variant()),
193-
&paths::RESULT_ERR,
194-
Item::Lang(lang_items.result_err_variant()),
190+
Item::Lang(ResultOk),
191+
Item::Lang(ResultErr),
195192
"is_ok()",
196193
"is_err()",
197194
)
@@ -201,10 +198,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
201198
arms,
202199
path_left,
203200
path_right,
204-
&paths::IPADDR_V4,
205-
Item::Diag(sym!(IpAddr), sym!(V4)),
206-
&paths::IPADDR_V6,
207-
Item::Diag(sym!(IpAddr), sym!(V6)),
201+
Item::Diag(sym::IpAddr, sym!(V4)),
202+
Item::Diag(sym::IpAddr, sym!(V6)),
208203
"is_ipv4()",
209204
"is_ipv6()",
210205
)
@@ -224,10 +219,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
224219
arms,
225220
path_left,
226221
path_right,
227-
&paths::OPTION_SOME,
228-
Item::Lang(lang_items.option_some_variant()),
229-
&paths::OPTION_NONE,
230-
Item::Lang(lang_items.option_none_variant()),
222+
Item::Lang(OptionSome),
223+
Item::Lang(OptionNone),
231224
"is_some()",
232225
"is_none()",
233226
)
@@ -237,10 +230,8 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
237230
arms,
238231
path_left,
239232
path_right,
240-
&paths::POLL_READY,
241-
Item::Lang(lang_items.poll_ready_variant()),
242-
&paths::POLL_PENDING,
243-
Item::Lang(lang_items.poll_pending_variant()),
233+
Item::Lang(PollReady),
234+
Item::Lang(PollPending),
244235
"is_ready()",
245236
"is_pending()",
246237
)
@@ -278,21 +269,17 @@ pub(super) fn check_match<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>, op
278269

279270
#[derive(Clone, Copy)]
280271
enum Item {
281-
Lang(Option<DefId>),
272+
Lang(LangItem),
282273
Diag(Symbol, Symbol),
283274
}
284275

285-
fn is_pat_variant(cx: &LateContext<'_>, pat: &Pat<'_>, path: &QPath<'_>, expected_path: &[&str], expected_item: Item) -> bool {
276+
fn is_pat_variant(cx: &LateContext<'_>, pat: &Pat<'_>, path: &QPath<'_>, expected_item: Item) -> bool {
286277
let Some(id) = cx.typeck_results().qpath_res(path, pat.hir_id).opt_def_id() else { return false };
287278

288-
// TODO: Path matching can be removed when `IpAddr` is a diagnostic item.
289-
if match_def_path(cx, id, expected_path) {
290-
return true
291-
}
292-
293279
match expected_item {
294-
Item::Lang(expected_id) => {
295-
Some(cx.tcx.parent(id)) == expected_id
280+
Item::Lang(expected_lang_item) => {
281+
let expected_id = cx.tcx.lang_items().require(expected_lang_item).unwrap();
282+
cx.tcx.parent(id) == expected_id
296283
},
297284
Item::Diag(expected_ty, expected_variant) => {
298285
let ty = cx.typeck_results().pat_ty(pat);
@@ -316,9 +303,7 @@ fn find_good_method_for_match<'a>(
316303
arms: &[Arm<'_>],
317304
path_left: &QPath<'_>,
318305
path_right: &QPath<'_>,
319-
expected_path_left: &[&str],
320306
expected_item_left: Item,
321-
expected_path_right: &[&str],
322307
expected_item_right: Item,
323308
should_be_left: &'a str,
324309
should_be_right: &'a str,
@@ -327,15 +312,15 @@ fn find_good_method_for_match<'a>(
327312
let pat_right = arms[1].pat;
328313

329314
let body_node_pair = if (
330-
is_pat_variant(cx, pat_left, path_left, expected_path_left, expected_item_left)
315+
is_pat_variant(cx, pat_left, path_left, expected_item_left)
331316
) && (
332-
is_pat_variant(cx, pat_right, path_right, expected_path_right, expected_item_right)
317+
is_pat_variant(cx, pat_right, path_right, expected_item_right)
333318
) {
334319
(&arms[0].body.kind, &arms[1].body.kind)
335320
} else if (
336-
is_pat_variant(cx, pat_left, path_left, expected_path_right, expected_item_right)
321+
is_pat_variant(cx, pat_left, path_left, expected_item_right)
337322
) && (
338-
is_pat_variant(cx, pat_right, path_right, expected_path_left, expected_item_left)
323+
is_pat_variant(cx, pat_right, path_right, expected_item_left)
339324
) {
340325
(&arms[1].body.kind, &arms[0].body.kind)
341326
} else {

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

-2
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ pub const INDEX_MUT: [&str; 3] = ["core", "ops", "IndexMut"];
6666
pub const INSERT_STR: [&str; 4] = ["alloc", "string", "String", "insert_str"];
6767
pub const IO_READ: [&str; 3] = ["std", "io", "Read"];
6868
pub const IO_WRITE: [&str; 3] = ["std", "io", "Write"];
69-
pub const IPADDR_V4: [&str; 5] = ["std", "net", "ip", "IpAddr", "V4"];
70-
pub const IPADDR_V6: [&str; 5] = ["std", "net", "ip", "IpAddr", "V6"];
7169
pub const ITER_COUNT: [&str; 6] = ["core", "iter", "traits", "iterator", "Iterator", "count"];
7270
pub const ITER_EMPTY: [&str; 5] = ["core", "iter", "sources", "empty", "Empty"];
7371
pub const ITER_REPEAT: [&str; 5] = ["core", "iter", "sources", "repeat", "repeat"];

0 commit comments

Comments
 (0)