Skip to content

Commit 8a4b6b3

Browse files
committed
LifetimeSyntax little things
1 parent 17ac62e commit 8a4b6b3

File tree

1 file changed

+20
-46
lines changed

1 file changed

+20
-46
lines changed

compiler/rustc_lint/src/lifetime_syntax.rs

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_hir::intravisit::{self, Visitor};
33
use rustc_hir::{self as hir, LifetimeSource};
44
use rustc_session::{declare_lint, declare_lint_pass};
55
use rustc_span::Span;
6+
use rustc_span::def_id::LocalDefId;
67
use tracing::instrument;
78

89
use crate::{LateContext, LateLintPass, LintContext, lints};
@@ -78,11 +79,11 @@ impl<'tcx> LateLintPass<'tcx> for LifetimeSyntax {
7879
fn check_fn(
7980
&mut self,
8081
cx: &LateContext<'tcx>,
81-
_: hir::intravisit::FnKind<'tcx>,
82+
_: intravisit::FnKind<'tcx>,
8283
fd: &'tcx hir::FnDecl<'tcx>,
8384
_: &'tcx hir::Body<'tcx>,
84-
_: rustc_span::Span,
85-
_: rustc_span::def_id::LocalDefId,
85+
_: Span,
86+
_: LocalDefId,
8687
) {
8788
check_fn_like(cx, fd);
8889
}
@@ -97,11 +98,7 @@ impl<'tcx> LateLintPass<'tcx> for LifetimeSyntax {
9798
}
9899

99100
#[instrument(skip_all)]
100-
fn check_foreign_item(
101-
&mut self,
102-
cx: &LateContext<'tcx>,
103-
fi: &'tcx rustc_hir::ForeignItem<'tcx>,
104-
) {
101+
fn check_foreign_item(&mut self, cx: &LateContext<'tcx>, fi: &'tcx hir::ForeignItem<'tcx>) {
105102
match fi.kind {
106103
hir::ForeignItemKind::Fn(fn_sig, _idents, _generics) => check_fn_like(cx, fn_sig.decl),
107104
hir::ForeignItemKind::Static(..) => {}
@@ -228,7 +225,7 @@ impl<T> LifetimeSyntaxCategories<Vec<T>> {
228225

229226
pub fn iter_unnamed(&self) -> impl Iterator<Item = &T> {
230227
let Self { hidden, elided, named: _ } = self;
231-
[hidden.iter(), elided.iter()].into_iter().flatten()
228+
std::iter::chain(hidden, elided)
232229
}
233230
}
234231

@@ -308,13 +305,13 @@ fn emit_mismatch_diagnostic<'tcx>(
308305
use LifetimeSource::*;
309306
use hir::LifetimeSyntax::*;
310307

311-
let syntax_source = info.syntax_source();
308+
let lifetime = info.lifetime;
312309

313-
if let (ExplicitBound, _) = syntax_source {
310+
if lifetime.syntax == ExplicitBound {
314311
bound_lifetime = Some(info);
315312
}
316313

317-
match syntax_source {
314+
match (lifetime.syntax, lifetime.source) {
318315
// E.g. `&T`.
319316
(Implicit, Reference) => {
320317
suggest_change_to_explicit_anonymous.push(info);
@@ -334,8 +331,8 @@ fn emit_mismatch_diagnostic<'tcx>(
334331
suggest_change_to_explicit_bound.push(info);
335332
}
336333

337-
// E.g. `ContainsLifetime<'_>`.
338-
(ExplicitAnonymous, Path { .. }) => {
334+
// E.g. `ContainsLifetime<'_>`, `+ '_`, `+ use<'_>`.
335+
(ExplicitAnonymous, Path { .. } | OutlivesBound | PreciseCapturing) => {
339336
suggest_change_to_explicit_bound.push(info);
340337
}
341338

@@ -346,8 +343,8 @@ fn emit_mismatch_diagnostic<'tcx>(
346343
suggest_change_to_explicit_anonymous.push(info);
347344
}
348345

349-
// E.g. `ContainsLifetime<'a>`.
350-
(ExplicitBound, Path { .. }) => {
346+
// E.g. `ContainsLifetime<'a>`, `+ 'a`, `+ use<'a>`.
347+
(ExplicitBound, Path { .. } | OutlivesBound | PreciseCapturing) => {
351348
suggest_change_to_mixed_explicit_anonymous.push(info);
352349
suggest_change_to_explicit_anonymous.push(info);
353350
}
@@ -356,29 +353,18 @@ fn emit_mismatch_diagnostic<'tcx>(
356353
panic!("This syntax / source combination is not possible");
357354
}
358355

359-
// E.g. `+ '_`, `+ use<'_>`.
360-
(ExplicitAnonymous, OutlivesBound | PreciseCapturing) => {
361-
suggest_change_to_explicit_bound.push(info);
362-
}
363-
364-
// E.g. `+ 'a`, `+ use<'a>`.
365-
(ExplicitBound, OutlivesBound | PreciseCapturing) => {
366-
suggest_change_to_mixed_explicit_anonymous.push(info);
367-
suggest_change_to_explicit_anonymous.push(info);
368-
}
369-
370356
(_, Other) => {
371357
panic!("This syntax / source combination has already been skipped");
372358
}
373359
}
374360

375-
if matches!(syntax_source, (_, Path { .. } | OutlivesBound | PreciseCapturing)) {
361+
if matches!(lifetime.source, Path { .. } | OutlivesBound | PreciseCapturing) {
376362
allow_suggesting_implicit = false;
377363
}
378364

379-
match syntax_source {
380-
(_, Reference) => saw_a_reference = true,
381-
(_, Path { .. }) => saw_a_path = true,
365+
match lifetime.source {
366+
Reference => saw_a_reference = true,
367+
Path { .. } => saw_a_path = true,
382368
_ => {}
383369
}
384370
}
@@ -398,10 +384,10 @@ fn emit_mismatch_diagnostic<'tcx>(
398384
|infos: &[&Info<'_>]| infos.iter().map(|i| i.removing_span()).collect::<Vec<_>>();
399385

400386
let explicit_bound_suggestion = bound_lifetime.map(|info| {
401-
build_mismatch_suggestion(info.lifetime_name(), &suggest_change_to_explicit_bound)
387+
build_mismatch_suggestion(info.lifetime.ident.as_str(), &suggest_change_to_explicit_bound)
402388
});
403389

404-
let is_bound_static = bound_lifetime.is_some_and(|info| info.is_static());
390+
let is_bound_static = bound_lifetime.is_some_and(|info| info.lifetime.is_static());
405391

406392
tracing::debug!(?bound_lifetime, ?explicit_bound_suggestion, ?is_bound_static);
407393

@@ -514,18 +500,6 @@ struct Info<'tcx> {
514500
}
515501

516502
impl<'tcx> Info<'tcx> {
517-
fn syntax_source(&self) -> (hir::LifetimeSyntax, LifetimeSource) {
518-
(self.lifetime.syntax, self.lifetime.source)
519-
}
520-
521-
fn lifetime_name(&self) -> &str {
522-
self.lifetime.ident.as_str()
523-
}
524-
525-
fn is_static(&self) -> bool {
526-
self.lifetime.is_static()
527-
}
528-
529503
/// When reporting a lifetime that is implicit, we expand the span
530504
/// to include the type. Otherwise we end up pointing at nothing,
531505
/// which is a bit confusing.
@@ -547,7 +521,7 @@ impl<'tcx> Info<'tcx> {
547521
/// ```
548522
// FIXME: Ideally, we'd also remove the lifetime declaration.
549523
fn removing_span(&self) -> Span {
550-
let mut span = self.suggestion("'dummy").0;
524+
let mut span = self.lifetime.ident.span;
551525
if let hir::TyKind::Ref(_, mut_ty) = self.ty.kind {
552526
span = span.until(mut_ty.ty.span);
553527
}

0 commit comments

Comments
 (0)