Skip to content

Commit 25da0e2

Browse files
committed
[Clippy] Swap manual_while_let_some to use diagnostic items instead of paths
1 parent 15240a9 commit 25da0e2

File tree

5 files changed

+15
-11
lines changed

5 files changed

+15
-11
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1366,6 +1366,8 @@ symbols! {
13661366
optin_builtin_traits,
13671367
option,
13681368
option_env,
1369+
option_expect,
1370+
option_unwrap,
13691371
options,
13701372
or,
13711373
or_patterns,
@@ -2092,8 +2094,10 @@ symbols! {
20922094
vec_as_mut_slice,
20932095
vec_as_slice,
20942096
vec_from_elem,
2097+
vec_is_empty,
20952098
vec_macro,
20962099
vec_new,
2100+
vec_pop,
20972101
vec_with_capacity,
20982102
vecdeque_iter,
20992103
version,

library/alloc/src/vec/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2384,6 +2384,7 @@ impl<T, A: Allocator> Vec<T, A> {
23842384
/// Takes *O*(1) time.
23852385
#[inline]
23862386
#[stable(feature = "rust1", since = "1.0.0")]
2387+
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_pop")]
23872388
pub fn pop(&mut self) -> Option<T> {
23882389
if self.len == 0 {
23892390
None
@@ -2577,6 +2578,7 @@ impl<T, A: Allocator> Vec<T, A> {
25772578
/// assert!(!v.is_empty());
25782579
/// ```
25792580
#[stable(feature = "rust1", since = "1.0.0")]
2581+
#[cfg_attr(not(test), rustc_diagnostic_item = "vec_is_empty")]
25802582
pub fn is_empty(&self) -> bool {
25812583
self.len() == 0
25822584
}

library/core/src/option.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ impl<T> Option<T> {
923923
#[inline]
924924
#[track_caller]
925925
#[stable(feature = "rust1", since = "1.0.0")]
926+
#[cfg_attr(not(test), rustc_diagnostic_item = "option_expect")]
926927
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
927928
pub const fn expect(self, msg: &str) -> T {
928929
match self {
@@ -960,6 +961,7 @@ impl<T> Option<T> {
960961
#[inline(always)]
961962
#[track_caller]
962963
#[stable(feature = "rust1", since = "1.0.0")]
964+
#[cfg_attr(not(test), rustc_diagnostic_item = "option_unwrap")]
963965
#[rustc_const_unstable(feature = "const_option", issue = "67441")]
964966
pub const fn unwrap(self) -> T {
965967
match self {

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::source::snippet;
3-
use clippy_utils::{match_def_path, paths, SpanlessEq};
3+
use clippy_utils::SpanlessEq;
44
use rustc_errors::Applicability;
55
use rustc_hir::{Expr, ExprKind, Pat, Stmt, StmtKind, UnOp};
66
use rustc_lint::LateContext;
7-
use rustc_span::Span;
7+
use rustc_span::{sym, Symbol, Span};
88
use std::borrow::Cow;
99

1010
use super::MANUAL_WHILE_LET_SOME;
@@ -47,20 +47,20 @@ fn report_lint(cx: &LateContext<'_>, pop_span: Span, pop_stmt_kind: PopStmt<'_>,
4747
);
4848
}
4949

50-
fn match_method_call(cx: &LateContext<'_>, expr: &Expr<'_>, method: &[&str]) -> bool {
50+
fn match_method_call(cx: &LateContext<'_>, expr: &Expr<'_>, method: Symbol) -> bool {
5151
if let ExprKind::MethodCall(..) = expr.kind
5252
&& let Some(id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
5353
{
54-
match_def_path(cx, id, method)
54+
cx.tcx.is_diagnostic_item(method, id)
5555
} else {
5656
false
5757
}
5858
}
5959

6060
fn is_vec_pop_unwrap(cx: &LateContext<'_>, expr: &Expr<'_>, is_empty_recv: &Expr<'_>) -> bool {
61-
if (match_method_call(cx, expr, &paths::OPTION_UNWRAP) || match_method_call(cx, expr, &paths::OPTION_EXPECT))
61+
if (match_method_call(cx, expr, sym::option_unwrap) || match_method_call(cx, expr, sym::option_expect))
6262
&& let ExprKind::MethodCall(_, unwrap_recv, ..) = expr.kind
63-
&& match_method_call(cx, unwrap_recv, &paths::VEC_POP)
63+
&& match_method_call(cx, unwrap_recv, sym::vec_pop)
6464
&& let ExprKind::MethodCall(_, pop_recv, ..) = unwrap_recv.kind
6565
{
6666
// make sure they're the same `Vec`
@@ -96,7 +96,7 @@ fn check_call_arguments(cx: &LateContext<'_>, stmt: &Stmt<'_>, is_empty_recv: &E
9696
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, full_cond: &'tcx Expr<'_>, body: &'tcx Expr<'_>, loop_span: Span) {
9797
if let ExprKind::Unary(UnOp::Not, cond) = full_cond.kind
9898
&& let ExprKind::MethodCall(_, is_empty_recv, _, _) = cond.kind
99-
&& match_method_call(cx, cond, &paths::VEC_IS_EMPTY)
99+
&& match_method_call(cx, cond, sym::vec_is_empty)
100100
&& let ExprKind::Block(body, _) = body.kind
101101
&& let Some(stmt) = body.stmts.first()
102102
{

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,5 @@ pub const TOKIO_IO_OPEN_OPTIONS: [&str; 4] = ["tokio", "fs", "open_options", "Op
7373
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
7474
pub const TOKIO_IO_OPEN_OPTIONS_NEW: [&str; 5] = ["tokio", "fs", "open_options", "OpenOptions", "new"];
7575
pub const INSTANT_NOW: [&str; 4] = ["std", "time", "Instant", "now"];
76-
pub const VEC_IS_EMPTY: [&str; 4] = ["alloc", "vec", "Vec", "is_empty"];
77-
pub const VEC_POP: [&str; 4] = ["alloc", "vec", "Vec", "pop"];
7876
pub const WAKER: [&str; 4] = ["core", "task", "wake", "Waker"];
79-
pub const OPTION_UNWRAP: [&str; 4] = ["core", "option", "Option", "unwrap"];
80-
pub const OPTION_EXPECT: [&str; 4] = ["core", "option", "Option", "expect"];
8177
pub const BOOL_THEN: [&str; 4] = ["core", "bool", "<impl bool>", "then"];

0 commit comments

Comments
 (0)