Skip to content

Commit 89532c0

Browse files
committed
[Clippy] Swap unnecessary_to_owned to use diagnostic item instead of path
1 parent 1890620 commit 89532c0

File tree

4 files changed

+4
-3
lines changed

4 files changed

+4
-3
lines changed

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,6 +1888,7 @@ symbols! {
18881888
string_as_mut_str,
18891889
string_as_str,
18901890
string_deref_patterns,
1891+
string_from_utf8,
18911892
string_insert_str,
18921893
string_push_str,
18931894
stringify,

library/alloc/src/string.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,7 @@ impl String {
571571
/// [`into_bytes`]: String::into_bytes
572572
#[inline]
573573
#[stable(feature = "rust1", since = "1.0.0")]
574+
#[cfg_attr(not(test), rustc_diagnostic_item = "string_from_utf8")]
574575
pub fn from_utf8(vec: Vec<u8>) -> Result<String, FromUtf8Error> {
575576
match str::from_utf8(&vec) {
576577
Ok(..) => Ok(String { vec }),

src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use clippy_utils::source::{snippet, SpanRangeExt};
66
use clippy_utils::ty::{get_iterator_item_ty, implements_trait, is_copy, is_type_diagnostic_item, is_type_lang_item};
77
use clippy_utils::visitors::find_all_ret_expressions;
88
use clippy_utils::{
9-
fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, match_def_path, paths, peel_middle_ty_refs,
9+
fn_def_id, get_parent_expr, is_diag_item_method, is_diag_trait_item, peel_middle_ty_refs,
1010
return_ty,
1111
};
1212
use rustc_errors::Applicability;
@@ -250,7 +250,7 @@ fn check_string_from_utf8<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>,
250250
if let Some((call, arg)) = skip_addr_of_ancestors(cx, expr)
251251
&& !arg.span.from_expansion()
252252
&& let ExprKind::Call(callee, _) = call.kind
253-
&& fn_def_id(cx, call).is_some_and(|did| match_def_path(cx, did, &paths::STRING_FROM_UTF8))
253+
&& fn_def_id(cx, call).is_some_and(|did| cx.tcx.is_diagnostic_item(sym::string_from_utf8, did))
254254
&& let Some(unwrap_call) = get_parent_expr(cx, call)
255255
&& let ExprKind::MethodCall(unwrap_method_name, ..) = unwrap_call.kind
256256
&& matches!(unwrap_method_name.ident.name, sym::unwrap | sym::expect)

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ pub const SYMBOL_INTERN: [&str; 4] = ["rustc_span", "symbol", "Symbol", "intern"
6161
pub const SYMBOL_TO_IDENT_STRING: [&str; 4] = ["rustc_span", "symbol", "Symbol", "to_ident_string"];
6262
pub const SYM_MODULE: [&str; 3] = ["rustc_span", "symbol", "sym"];
6363
pub const SYNTAX_CONTEXT: [&str; 3] = ["rustc_span", "hygiene", "SyntaxContext"];
64-
pub const STRING_FROM_UTF8: [&str; 4] = ["alloc", "string", "String", "from_utf8"];
6564
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates
6665
pub const TOKIO_FILE_OPTIONS: [&str; 5] = ["tokio", "fs", "file", "File", "options"];
6766
#[expect(clippy::invalid_paths)] // internal lints do not know about all external crates

0 commit comments

Comments
 (0)