From ae5b740a6921e5c40fe15dfb339ef3a5b1066404 Mon Sep 17 00:00:00 2001 From: Andre Bogus Date: Mon, 6 Jan 2025 01:28:19 +0100 Subject: [PATCH] distinguish no_std code --- clippy_lints/src/too_owned.rs | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/too_owned.rs b/clippy_lints/src/too_owned.rs index 60714185ce14..eca94762a1b1 100644 --- a/clippy_lints/src/too_owned.rs +++ b/clippy_lints/src/too_owned.rs @@ -4,8 +4,8 @@ use rustc_errors::Applicability; use rustc_hir as hir; use rustc_lint::{LateContext, LateLintPass}; use rustc_session::declare_lint_pass; -use rustc_span::symbol::sym; use rustc_span::Span; +use rustc_span::symbol::sym; declare_clippy_lint! { /// ### What it does @@ -71,11 +71,19 @@ fn replacement(cx: &LateContext<'_>, cty: &hir::Ty<'_>) -> Option<(Span, String) None }; } - for (diag, repl) in [ - (sym::cstring_type, "std::ffi::CStr"), - (sym::OsString, "std::ffi::OsStr"), - (sym::PathBuf, "std::path::Path"), - ] { + if clippy_utils::is_path_diagnostic_item(cx, cty, sym::cstring_type) { + return Some(( + cty.span, + (if clippy_utils::is_no_std_crate(cx) { + "core::ffi::CStr" + } else { + "std::ffi::CStr" + }) + .into(), + )); + } + // Neither OsString nor PathBuf are available outside std + for (diag, repl) in [(sym::OsString, "std::ffi::OsStr"), (sym::PathBuf, "std::path::Path")] { if clippy_utils::is_path_diagnostic_item(cx, cty, diag) { return Some((cty.span, repl.into())); }