Skip to content

Commit 5b69698

Browse files
committed
Update to support 2026-03-08 nightly
1 parent 0e6b813 commit 5b69698

3 files changed

Lines changed: 104 additions & 77 deletions

File tree

flake.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/hir_lints/not_using_prelude.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
66
use rustc_hir::{Item, ItemKind, UseKind};
77
use rustc_lint::{LateContext, LateLintPass, LintContext};
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
9-
use rustc_span::sym;
9+
use rustc_span::{Span, Symbol, sym};
1010

1111
use crate::ctxt::AnalysisCtxt;
1212

@@ -24,6 +24,15 @@ pub struct NotUsingPrelude<'tcx> {
2424

2525
impl_lint_pass!(NotUsingPrelude<'_> => [NOT_USING_PRELUDE]);
2626

27+
#[derive(Diagnostic)]
28+
#[diag("this item is available via prelude")]
29+
#[help("import with `{$crate_name}::prelude::*` instead")]
30+
struct NotUsingPreludeLint {
31+
#[primary_span]
32+
pub span: Span,
33+
pub crate_name: Symbol,
34+
}
35+
2736
impl<'tcx> LateLintPass<'tcx> for NotUsingPrelude<'tcx> {
2837
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'tcx>) {
2938
let ItemKind::Use(path, UseKind::Single(_)) = item.kind else {
@@ -57,10 +66,13 @@ impl<'tcx> LateLintPass<'tcx> for NotUsingPrelude<'tcx> {
5766
let cnum = prelude.get(&imported_def_id).copied().unwrap();
5867
let crate_name = self.cx.crate_name(cnum);
5968

60-
cx.span_lint(NOT_USING_PRELUDE, item.span, |diag| {
61-
diag.primary_message("this item is available via prelude");
62-
diag.help(format!("import with `{crate_name}::prelude::*` instead"));
63-
});
69+
cx.emit_diag_lint(
70+
NOT_USING_PRELUDE,
71+
NotUsingPreludeLint {
72+
span: item.span,
73+
crate_name,
74+
},
75+
);
6476
}
6577
}
6678

src/infallible_allocation.rs

Lines changed: 81 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: MIT OR Apache-2.0
44

55
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
6+
use rustc_errors::{Diag, DiagCtxtHandle, Diagnostic, Level};
67
use rustc_lint::{LateContext, LateLintPass, LintContext};
78
use rustc_middle::mir::mono::MonoItem;
89
use rustc_middle::ty::Instance;
@@ -20,6 +21,16 @@ declare_tool_lint! {
2021

2122
declare_lint_pass!(InfallibleAllocation => [INFALLIBLE_ALLOCATION]);
2223

24+
struct ClosureDiag<F: FnOnce(&mut Diag<'_, ()>)>(F);
25+
26+
impl<'a, F: FnOnce(&mut Diag<'_, ()>)> Diagnostic<'a, ()> for ClosureDiag<F> {
27+
fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
28+
let mut lint = Diag::new(dcx, level, "");
29+
(self.0)(&mut lint);
30+
lint
31+
}
32+
}
33+
2334
fn is_generic_fn<'tcx>(instance: Instance<'tcx>) -> bool {
2435
instance.args.non_erasable_generics().next().is_some()
2536
}
@@ -174,73 +185,77 @@ impl<'tcx> LateLintPass<'tcx> for InfallibleAllocation {
174185
.tcx
175186
.def_path_str_with_args(accessee.def_id(), accessee.args);
176187

177-
cx.span_lint(INFALLIBLE_ALLOCATION, item.span, |diag| {
178-
diag.primary_message(format!(
179-
"`{}` can perform infallible allocation{}",
180-
accessee_path, generic_note
181-
));
182-
// For generic functions try to display a stacktrace until a non-generic one.
183-
let mut caller = *accessor;
184-
let mut visited = FxHashSet::default();
185-
visited.insert(*accessor);
186-
visited.insert(accessee);
187-
while is_generic_fn(caller) {
188-
let spanned_caller = match backward
189-
.get(&caller)
190-
.map(|x| &**x)
191-
.unwrap_or(&[])
192-
.iter()
193-
.find(|x| !visited.contains(&x.node))
194-
{
195-
Some(v) => *v,
196-
None => break,
197-
};
198-
caller = spanned_caller.node;
199-
visited.insert(caller);
200-
201-
diag.span_note(
202-
spanned_caller.span,
203-
format!(
204-
"which is called from `{}`",
205-
cx.tcx.def_path_str_with_args(caller.def_id(), caller.args)
206-
),
207-
);
208-
}
209-
210-
// Generate some help messages for why the function is determined to be infallible.
211-
let mut msg: &str = &format!(
212-
"`{}` is determined to be infallible because it",
213-
accessee_path
214-
);
215-
let mut callee = accessee;
216-
loop {
217-
let callee_callee = match forward
218-
.get(&callee)
219-
.map(|x| &**x)
220-
.unwrap_or(&[])
221-
.iter()
222-
.find(|x| {
223-
infallible.contains(&x.node) && !visited.contains(&x.node)
224-
}) {
225-
Some(v) => v,
226-
None => break,
227-
};
228-
callee = callee_callee.node;
229-
visited.insert(callee);
230-
231-
diag.span_note(
232-
callee_callee.span,
233-
format!(
234-
"{} calls into `{}`",
235-
msg,
236-
cx.tcx.def_path_str_with_args(callee.def_id(), callee.args)
237-
),
188+
cx.emit_span_lint(
189+
INFALLIBLE_ALLOCATION,
190+
item.span,
191+
ClosureDiag(|diag| {
192+
diag.primary_message(format!(
193+
"`{}` can perform infallible allocation{}",
194+
accessee_path, generic_note
195+
));
196+
// For generic functions try to display a stacktrace until a non-generic one.
197+
let mut caller = *accessor;
198+
let mut visited = FxHashSet::default();
199+
visited.insert(*accessor);
200+
visited.insert(accessee);
201+
while is_generic_fn(caller) {
202+
let spanned_caller = match backward
203+
.get(&caller)
204+
.map(|x| &**x)
205+
.unwrap_or(&[])
206+
.iter()
207+
.find(|x| !visited.contains(&x.node))
208+
{
209+
Some(v) => *v,
210+
None => break,
211+
};
212+
caller = spanned_caller.node;
213+
visited.insert(caller);
214+
215+
diag.span_note(
216+
spanned_caller.span,
217+
format!(
218+
"which is called from `{}`",
219+
cx.tcx.def_path_str_with_args(caller.def_id(), caller.args)
220+
),
221+
);
222+
}
223+
224+
// Generate some help messages for why the function is determined to be infallible.
225+
let mut msg: &str = &format!(
226+
"`{}` is determined to be infallible because it",
227+
accessee_path
238228
);
239-
msg = "which";
240-
}
241-
242-
diag.note(format!("{} may call alloc_error_handler", msg));
243-
});
229+
let mut callee = accessee;
230+
loop {
231+
let callee_callee = match forward
232+
.get(&callee)
233+
.map(|x| &**x)
234+
.unwrap_or(&[])
235+
.iter()
236+
.find(|x| {
237+
infallible.contains(&x.node) && !visited.contains(&x.node)
238+
}) {
239+
Some(v) => v,
240+
None => break,
241+
};
242+
callee = callee_callee.node;
243+
visited.insert(callee);
244+
245+
diag.span_note(
246+
callee_callee.span,
247+
format!(
248+
"{} calls into `{}`",
249+
msg,
250+
cx.tcx.def_path_str_with_args(callee.def_id(), callee.args)
251+
),
252+
);
253+
msg = "which";
254+
}
255+
256+
diag.note(format!("{} may call alloc_error_handler", msg));
257+
}),
258+
);
244259
}
245260
}
246261
}

0 commit comments

Comments
 (0)