Skip to content

Commit 8915d07

Browse files
Rollup merge of #163096 - lapla-cogito:std_or_core, r=oli-obk
Don't suggest `std::` rustfix paths in `#![no_std]` crates Several lints emit rustfix suggestions with hard-coded `std::...` paths. In `#![no_std]` crates those paths do not exist of course, so `cargo fix` can leave the crate unable to compile. This is best reviewed per commit.
2 parents ebdddb5 + 2b022a3 commit 8915d07

28 files changed

Lines changed: 449 additions & 59 deletions

‎compiler/rustc_lint/src/diagnostics.rs‎

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,9 +1040,10 @@ pub(crate) struct ForgetCopyDiag<'a> {
10401040

10411041
#[derive(Diagnostic)]
10421042
#[diag(
1043-
"calls to `std::mem::drop` with `std::mem::ManuallyDrop` instead of the inner value does nothing"
1043+
"calls to `{$krate}::mem::drop` with `{$krate}::mem::ManuallyDrop` instead of the inner value does nothing"
10441044
)]
10451045
pub(crate) struct UndroppedManuallyDropsDiag<'a> {
1046+
pub krate: &'static str,
10461047
pub arg_ty: Ty<'a>,
10471048
#[label("argument has type `{$arg_ty}`")]
10481049
pub label: Span,
@@ -1052,21 +1053,23 @@ pub(crate) struct UndroppedManuallyDropsDiag<'a> {
10521053

10531054
#[derive(Subdiagnostic)]
10541055
#[multipart_suggestion(
1055-
"use `std::mem::ManuallyDrop::into_inner` to get the inner value",
1056+
"use `{$krate}::mem::ManuallyDrop::into_inner` to get the inner value",
10561057
applicability = "machine-applicable"
10571058
)]
10581059
pub(crate) struct UndroppedManuallyDropsSuggestion {
1059-
#[suggestion_part(code = "std::mem::ManuallyDrop::into_inner(")]
1060+
pub krate: &'static str,
1061+
#[suggestion_part(code = "{krate}::mem::ManuallyDrop::into_inner(")]
10601062
pub start_span: Span,
10611063
#[suggestion_part(code = ")")]
10621064
pub end_span: Span,
10631065
}
10641066

10651067
#[derive(Diagnostic)]
10661068
#[diag(
1067-
"calls to `drop_in_place` with a pointer to a `std::mem::ManuallyDrop` instead of the inner value does nothing"
1069+
"calls to `drop_in_place` with a pointer to a `{$krate}::mem::ManuallyDrop` instead of the inner value does nothing"
10681070
)]
10691071
pub(crate) struct UndroppedManuallyDropsInPlaceDiag<'a> {
1072+
pub krate: &'static str,
10701073
pub arg_ty: Ty<'a>,
10711074
#[label("argument has type `{$arg_ty}`")]
10721075
pub label: Span,
@@ -1076,11 +1079,12 @@ pub(crate) struct UndroppedManuallyDropsInPlaceDiag<'a> {
10761079

10771080
#[derive(Subdiagnostic)]
10781081
#[multipart_suggestion(
1079-
"use `std::mem::ManuallyDrop::drop` to drop the inner value",
1082+
"use `{$krate}::mem::ManuallyDrop::drop` to drop the inner value",
10801083
applicability = "maybe-incorrect"
10811084
)]
10821085
pub(crate) struct UndroppedManuallyDropsInPlaceSuggestion {
1083-
#[suggestion_part(code = "std::mem::ManuallyDrop::drop(&mut *")]
1086+
pub krate: &'static str,
1087+
#[suggestion_part(code = "{krate}::mem::ManuallyDrop::drop(&mut *")]
10841088
pub start_span: Span,
10851089
#[suggestion_part(code = ")")]
10861090
pub end_span: Span,
@@ -2074,44 +2078,53 @@ impl<'a> Diagnostic<'a> for DropGlue<'_> {
20742078
#[note(
20752079
"exposed provenance semantics can be used to create a pointer based on some previously exposed provenance"
20762080
)]
2077-
#[help(
2078-
"if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut`"
2079-
)]
20802081
#[help(
20812082
"for more information about transmute, see <https://doc.rust-lang.org/std/mem/fn.transmute.html#transmutation-between-pointers-and-integers>"
20822083
)]
20832084
#[help(
20842085
"for more information about exposed provenance, see <https://doc.rust-lang.org/std/ptr/index.html#exposed-provenance>"
20852086
)]
20862087
pub(crate) struct IntegerToPtrTransmutes<'tcx> {
2088+
#[subdiagnostic]
2089+
pub without_prov: Option<IntegerToPtrWithoutProvHelp>,
20872090
#[subdiagnostic]
20882091
pub suggestion: Option<IntegerToPtrTransmutesSuggestion<'tcx>>,
20892092
}
20902093

2094+
#[derive(Subdiagnostic)]
2095+
#[help(
2096+
"if you truly mean to create a pointer without provenance, use `{$krate}::ptr::without_provenance_mut`"
2097+
)]
2098+
pub(crate) struct IntegerToPtrWithoutProvHelp {
2099+
pub krate: &'static str,
2100+
}
2101+
20912102
#[derive(Subdiagnostic)]
20922103
pub(crate) enum IntegerToPtrTransmutesSuggestion<'tcx> {
20932104
#[multipart_suggestion(
2094-
"use `std::ptr::with_exposed_provenance{$suffix}` instead to use a previously exposed provenance",
2105+
"use `{$krate}::ptr::with_exposed_provenance{$suffix}` instead to use a previously exposed provenance",
20952106
applicability = "machine-applicable",
20962107
style = "verbose"
20972108
)]
20982109
ToPtr {
2110+
krate: &'static str,
20992111
dst: Ty<'tcx>,
21002112
suffix: &'static str,
2101-
#[suggestion_part(code = "std::ptr::with_exposed_provenance{suffix}::<{dst}>(")]
2113+
#[suggestion_part(code = "{krate}::ptr::with_exposed_provenance{suffix}::<{dst}>(")]
21022114
start_call: Span,
21032115
},
21042116
#[multipart_suggestion(
2105-
"use `std::ptr::with_exposed_provenance{$suffix}` instead to use a previously exposed provenance",
2117+
"use `{$krate}::ptr::with_exposed_provenance{$suffix}` instead to use a previously exposed provenance",
21062118
applicability = "machine-applicable",
21072119
style = "verbose"
21082120
)]
21092121
ToRef {
2122+
krate: &'static str,
21102123
dst: Ty<'tcx>,
21112124
suffix: &'static str,
21122125
ref_mutbl: &'static str,
21132126
#[suggestion_part(
2114-
code = "&{ref_mutbl}*std::ptr::with_exposed_provenance{suffix}::<{dst}>("
2127+
code = "&{ref_mutbl}*{krate}::ptr::with_exposed_provenance{suffix}::<{dst}>("
21152128
)]
21162129
start_call: Span,
21172130
},
@@ -2348,25 +2361,30 @@ pub(crate) enum AmbiguousWidePointerComparisons<'a> {
23482361
#[diag(
23492362
"ambiguous wide pointer comparison, the comparison includes metadata which may not be expected"
23502363
)]
2351-
#[help("use explicit `std::ptr::eq` method to compare metadata and addresses")]
2352-
#[help("use `std::ptr::addr_eq` or untyped pointers to only compare their addresses")]
2353-
Spanless,
2364+
#[help("use explicit `{$krate}::ptr::eq` method to compare metadata and addresses")]
2365+
#[help("use `{$krate}::ptr::addr_eq` or untyped pointers to only compare their addresses")]
2366+
Spanless { krate: &'static str },
2367+
#[diag(
2368+
"ambiguous wide pointer comparison, the comparison includes metadata which may not be expected"
2369+
)]
2370+
Warn,
23542371
}
23552372

23562373
#[derive(Subdiagnostic)]
23572374
#[multipart_suggestion(
2358-
"use explicit `std::ptr::eq` method to compare metadata and addresses",
2375+
"use explicit `{$krate}::ptr::eq` method to compare metadata and addresses",
23592376
style = "verbose",
23602377
// FIXME(#53934): make machine-applicable again
23612378
applicability = "maybe-incorrect"
23622379
)]
23632380
pub(crate) struct AmbiguousWidePointerComparisonsAddrMetadataSuggestion<'a> {
2381+
pub krate: &'static str,
23642382
pub ne: &'a str,
23652383
pub deref_left: &'a str,
23662384
pub deref_right: &'a str,
23672385
pub l_modifiers: &'a str,
23682386
pub r_modifiers: &'a str,
2369-
#[suggestion_part(code = "{ne}std::ptr::eq({deref_left}")]
2387+
#[suggestion_part(code = "{ne}{krate}::ptr::eq({deref_left}")]
23702388
pub left: Span,
23712389
#[suggestion_part(code = "{l_modifiers}, {deref_right}")]
23722390
pub middle: Span,
@@ -2376,18 +2394,19 @@ pub(crate) struct AmbiguousWidePointerComparisonsAddrMetadataSuggestion<'a> {
23762394

23772395
#[derive(Subdiagnostic)]
23782396
#[multipart_suggestion(
2379-
"use `std::ptr::addr_eq` or untyped pointers to only compare their addresses",
2397+
"use `{$krate}::ptr::addr_eq` or untyped pointers to only compare their addresses",
23802398
style = "verbose",
23812399
// FIXME(#53934): make machine-applicable again
23822400
applicability = "maybe-incorrect"
23832401
)]
23842402
pub(crate) struct AmbiguousWidePointerComparisonsAddrSuggestion<'a> {
2403+
pub(crate) krate: &'static str,
23852404
pub(crate) ne: &'a str,
23862405
pub(crate) deref_left: &'a str,
23872406
pub(crate) deref_right: &'a str,
23882407
pub(crate) l_modifiers: &'a str,
23892408
pub(crate) r_modifiers: &'a str,
2390-
#[suggestion_part(code = "{ne}std::ptr::addr_eq({deref_left}")]
2409+
#[suggestion_part(code = "{ne}{krate}::ptr::addr_eq({deref_left}")]
23912410
pub(crate) left: Span,
23922411
#[suggestion_part(code = "{l_modifiers}, {deref_right}")]
23932412
pub(crate) middle: Span,
@@ -2470,32 +2489,34 @@ pub(crate) enum UnpredictableFunctionPointerComparisons<'a, 'tcx> {
24702489
#[derive(Subdiagnostic)]
24712490
pub(crate) enum UnpredictableFunctionPointerComparisonsSuggestion<'a, 'tcx> {
24722491
#[multipart_suggestion(
2473-
"refactor your code, or use `std::ptr::fn_addr_eq` to suppress the lint",
2492+
"refactor your code, or use `{$krate}::ptr::fn_addr_eq` to suppress the lint",
24742493
style = "verbose",
24752494
applicability = "maybe-incorrect"
24762495
)]
24772496
FnAddrEq {
2497+
krate: &'static str,
24782498
ne: &'a str,
24792499
deref_left: &'a str,
24802500
deref_right: &'a str,
2481-
#[suggestion_part(code = "{ne}std::ptr::fn_addr_eq({deref_left}")]
2501+
#[suggestion_part(code = "{ne}{krate}::ptr::fn_addr_eq({deref_left}")]
24822502
left: Span,
24832503
#[suggestion_part(code = ", {deref_right}")]
24842504
middle: Span,
24852505
#[suggestion_part(code = ")")]
24862506
right: Span,
24872507
},
24882508
#[multipart_suggestion(
2489-
"refactor your code, or use `std::ptr::fn_addr_eq` to suppress the lint",
2509+
"refactor your code, or use `{$krate}::ptr::fn_addr_eq` to suppress the lint",
24902510
style = "verbose",
24912511
applicability = "maybe-incorrect"
24922512
)]
24932513
FnAddrEqWithCast {
2514+
krate: &'static str,
24942515
ne: &'a str,
24952516
deref_left: &'a str,
24962517
deref_right: &'a str,
24972518
fn_sig: rustc_middle::ty::PolyFnSig<'tcx>,
2498-
#[suggestion_part(code = "{ne}std::ptr::fn_addr_eq({deref_left}")]
2519+
#[suggestion_part(code = "{ne}{krate}::ptr::fn_addr_eq({deref_left}")]
24992520
left: Span,
25002521
#[suggestion_part(code = ", {deref_right}")]
25012522
middle: Span,

‎compiler/rustc_lint/src/drop_forget_useless.rs‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use crate::diagnostics::{
99
UndroppedManuallyDropsInPlaceSuggestion, UndroppedManuallyDropsSuggestion,
1010
UseLetUnderscoreIgnoreSuggestion,
1111
};
12+
use crate::utils::std_or_core;
1213
use crate::{LateContext, LateLintPass, LintContext};
1314

1415
declare_lint! {
@@ -252,15 +253,18 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
252253
}
253254
sym::mem_drop
254255
if let ty::Adt(adt, _) = arg_ty.kind()
255-
&& adt.is_manually_drop() =>
256+
&& adt.is_manually_drop()
257+
&& let Some(krate) = std_or_core(cx) =>
256258
{
257259
cx.emit_span_lint(
258260
UNDROPPED_MANUALLY_DROPS,
259261
expr.span,
260262
UndroppedManuallyDropsDiag {
263+
krate,
261264
arg_ty,
262265
label: arg.span,
263266
suggestion: UndroppedManuallyDropsSuggestion {
267+
krate,
264268
start_span: arg.span.shrink_to_lo(),
265269
end_span: arg.span.shrink_to_hi(),
266270
},
@@ -270,15 +274,18 @@ impl<'tcx> LateLintPass<'tcx> for DropForgetUseless {
270274
sym::ptr_drop_in_place | sym::ptr_drop_in_place_self
271275
if let &ty::RawPtr(inner_ty, _mutbl) = arg_ty.kind()
272276
&& let ty::Adt(adt, _) = inner_ty.kind()
273-
&& adt.is_manually_drop() =>
277+
&& adt.is_manually_drop()
278+
&& let Some(krate) = std_or_core(cx) =>
274279
{
275280
cx.emit_span_lint(
276281
UNDROPPED_MANUALLY_DROPS,
277282
expr.span,
278283
UndroppedManuallyDropsInPlaceDiag {
284+
krate,
279285
arg_ty,
280286
label: arg.span,
281287
suggestion: UndroppedManuallyDropsInPlaceSuggestion {
288+
krate,
282289
start_span: expr.span.shrink_to_lo().until(arg.span.shrink_to_lo()),
283290
end_span: arg.span.shrink_to_hi().until(expr.span.shrink_to_hi()),
284291
},

‎compiler/rustc_lint/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ pub use rustc_lint_defs::{
148148
Applicability, FutureIncompatibleInfo, Lint, LintId, LintPass, LintVec, declare_lint,
149149
declare_lint_pass, declare_tool_lint, impl_lint_pass,
150150
};
151+
pub use utils::std_or_core;
151152

152153
pub fn provide(providers: &mut Providers) {
153154
levels::provide(providers);

‎compiler/rustc_lint/src/non_fmt_panic.rs‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,14 @@ impl<'a, 'b, 'tcx> Diagnostic<'a> for PanicMessageNotLiteral<'b, 'tcx> {
143143
.get_diagnostic_item(sym::Debug)
144144
.is_some_and(|t| infcx.type_implements_trait(t, [ty], param_env).may_apply());
145145

146-
let suggest_panic_any = !is_str && panic == Some(sym::std_panic_macro);
146+
let suggest_panic_any = !is_str
147+
&& panic == Some(sym::std_panic_macro)
148+
&& cx
149+
.tcx
150+
.all_diagnostic_items(())
151+
.name_to_id
152+
.keys()
153+
.any(|name| name.as_str() == "panic_any");
147154

148155
let fmt_applicability = if suggest_panic_any {
149156
// If we can use panic_any, use that as the MachineApplicable suggestion.

‎compiler/rustc_lint/src/transmute.rs‎

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ use rustc_macros::Diagnostic;
88
use rustc_middle::ty::{self, Ty};
99
use rustc_span::sym;
1010

11-
use crate::diagnostics::{IntegerToPtrTransmutes, IntegerToPtrTransmutesSuggestion};
11+
use crate::diagnostics::{
12+
IntegerToPtrTransmutes, IntegerToPtrTransmutesSuggestion, IntegerToPtrWithoutProvHelp,
13+
};
14+
use crate::utils::std_or_core;
1215
use crate::{LateContext, LateLintPass};
1316

1417
declare_lint! {
@@ -166,30 +169,30 @@ fn check_int_to_ptr_transmute<'tcx>(
166169
}
167170

168171
let suffix = if mutbl.is_mut() { "_mut" } else { "" };
172+
let krate = std_or_core(cx);
169173
cx.tcx.emit_node_span_lint(
170174
INTEGER_TO_PTR_TRANSMUTES,
171175
expr.hir_id,
172176
expr.span,
173177
IntegerToPtrTransmutes {
174-
suggestion: if layout_inner_ty.is_sized() {
175-
Some(if dst.is_ref() {
176-
IntegerToPtrTransmutesSuggestion::ToRef {
178+
without_prov: krate.map(|krate| IntegerToPtrWithoutProvHelp { krate }),
179+
suggestion: match (krate, layout_inner_ty.is_sized()) {
180+
(Some(krate), true) if dst.is_ref() => {
181+
Some(IntegerToPtrTransmutesSuggestion::ToRef {
182+
krate,
177183
dst: *inner_ty,
178184
suffix,
179185
ref_mutbl: mutbl.prefix_str(),
180186
start_call: expr.span.shrink_to_lo().until(arg.span),
181-
}
182-
} else {
183-
IntegerToPtrTransmutesSuggestion::ToPtr {
184-
dst: *inner_ty,
185-
suffix,
186-
start_call: expr.span.shrink_to_lo().until(arg.span),
187-
}
188-
})
189-
} else {
190-
// We can't suggest using `with_exposed_provenance` for unsized type
191-
// so don't suggest anything.
192-
None
187+
})
188+
}
189+
(Some(krate), true) => Some(IntegerToPtrTransmutesSuggestion::ToPtr {
190+
krate,
191+
dst: *inner_ty,
192+
suffix,
193+
start_call: expr.span.shrink_to_lo().until(arg.span),
194+
}),
195+
_ => None,
193196
},
194197
},
195198
);

0 commit comments

Comments
 (0)