Skip to content

Commit 5f42ae1

Browse files
committed
[Clippy] Swap manual_strip to use diagnostic items instead of paths
1 parent 89532c0 commit 5f42ae1

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,11 +1876,14 @@ symbols! {
18761876
store,
18771877
str,
18781878
str_chars,
1879+
str_ends_with,
18791880
str_from_utf8,
18801881
str_from_utf8_mut,
18811882
str_from_utf8_unchecked,
18821883
str_from_utf8_unchecked_mut,
1884+
str_len,
18831885
str_split_whitespace,
1886+
str_starts_with,
18841887
str_trim,
18851888
str_trim_end,
18861889
str_trim_start,

library/core/src/str/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ impl str {
134134
/// ```
135135
#[stable(feature = "rust1", since = "1.0.0")]
136136
#[rustc_const_stable(feature = "const_str_len", since = "1.39.0")]
137+
#[cfg_attr(not(test), rustc_diagnostic_item = "str_len")]
137138
#[must_use]
138139
#[inline]
139140
pub const fn len(&self) -> usize {
@@ -1157,6 +1158,7 @@ impl str {
11571158
/// assert!(bananas.starts_with(&['a', 'b', 'c', 'd']));
11581159
/// ```
11591160
#[stable(feature = "rust1", since = "1.0.0")]
1161+
#[cfg_attr(not(test), rustc_diagnostic_item = "str_starts_with")]
11601162
pub fn starts_with<P: Pattern>(&self, pat: P) -> bool {
11611163
pat.is_prefix_of(self)
11621164
}
@@ -1181,6 +1183,7 @@ impl str {
11811183
/// assert!(!bananas.ends_with("nana"));
11821184
/// ```
11831185
#[stable(feature = "rust1", since = "1.0.0")]
1186+
#[cfg_attr(not(test), rustc_diagnostic_item = "str_ends_with")]
11841187
pub fn ends_with<P: Pattern>(&self, pat: P) -> bool
11851188
where
11861189
for<'a> P::Searcher<'a>: ReverseSearcher<'a>,

src/tools/clippy/clippy_lints/src/manual_strip.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use clippy_utils::consts::{ConstEvalCtxt, Constant};
44
use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::source::snippet;
66
use clippy_utils::usage::mutated_variables;
7-
use clippy_utils::{eq_expr_value, higher, match_def_path, paths};
7+
use clippy_utils::{eq_expr_value, higher};
88
use rustc_ast::ast::LitKind;
99
use rustc_errors::Applicability;
1010
use rustc_hir::def::Res;
@@ -14,7 +14,7 @@ use rustc_lint::{LateContext, LateLintPass};
1414
use rustc_middle::ty;
1515
use rustc_session::impl_lint_pass;
1616
use rustc_span::source_map::Spanned;
17-
use rustc_span::Span;
17+
use rustc_span::{sym, Span};
1818
use std::iter;
1919

2020
declare_clippy_lint! {
@@ -76,9 +76,9 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
7676
&& self.msrv.meets(msrvs::STR_STRIP_PREFIX)
7777
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(cond.hir_id)
7878
{
79-
let strip_kind = if match_def_path(cx, method_def_id, &paths::STR_STARTS_WITH) {
79+
let strip_kind = if cx.tcx.is_diagnostic_item(sym::str_starts_with, method_def_id) {
8080
StripKind::Prefix
81-
} else if match_def_path(cx, method_def_id, &paths::STR_ENDS_WITH) {
81+
} else if cx.tcx.is_diagnostic_item(sym::str_ends_with, method_def_id) {
8282
StripKind::Suffix
8383
} else {
8484
return;
@@ -137,7 +137,7 @@ impl<'tcx> LateLintPass<'tcx> for ManualStrip {
137137
fn len_arg<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx Expr<'tcx>> {
138138
if let ExprKind::MethodCall(_, arg, [], _) = expr.kind
139139
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(expr.hir_id)
140-
&& match_def_path(cx, method_def_id, &paths::STR_LEN)
140+
&& cx.tcx.is_diagnostic_item(sym::str_len, method_def_id)
141141
{
142142
Some(arg)
143143
} else {

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ pub const SERDE_DE_VISITOR: [&str; 3] = ["serde", "de", "Visitor"];
5252
pub const STD_IO_SEEK_FROM_CURRENT: [&str; 4] = ["std", "io", "SeekFrom", "Current"];
5353
pub const STD_IO_SEEKFROM_START: [&str; 4] = ["std", "io", "SeekFrom", "Start"];
5454
pub const STRING_NEW: [&str; 4] = ["alloc", "string", "String", "new"];
55-
pub const STR_ENDS_WITH: [&str; 4] = ["core", "str", "<impl str>", "ends_with"];
56-
pub const STR_LEN: [&str; 4] = ["core", "str", "<impl str>", "len"];
57-
pub const STR_STARTS_WITH: [&str; 4] = ["core", "str", "<impl str>", "starts_with"];
5855
pub const SYMBOL: [&str; 3] = ["rustc_span", "symbol", "Symbol"];
5956
pub const SYMBOL_AS_STR: [&str; 4] = ["rustc_span", "symbol", "Symbol", "as_str"];
6057
pub const SYMBOL_INTERN: [&str; 4] = ["rustc_span", "symbol", "Symbol", "intern"];

0 commit comments

Comments
 (0)