Skip to content

Split elided_lifetime_in_paths into finer-grained lints #120808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_baked_icu_data/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
//! ```

// tidy-alphabetical-start
#![allow(elided_lifetimes_in_paths)]
#![allow(internal_features)]
#![allow(unreachable_pub)] // because this crate is mostly generated code
#![doc(rust_logo)]
#![feature(rustdoc_internals)]
// #![warn(unreachable_pub)] // don't use because this crate is mostly generated code
// tidy-alphabetical-end
#![cfg_attr(bootstrap, allow(elided_lifetimes_in_paths))]
#![cfg_attr(not(bootstrap), allow(hidden_lifetimes_in_paths))]

mod data {
include!("data/mod.rs");
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -292,8 +292,6 @@ lint_hidden_lifetime_in_path =
lint_hidden_lifetime_in_path_suggestion =
indicate the anonymous lifetime

lint_hidden_lifetime_parameters = hidden lifetime parameters in types are deprecated

lint_hidden_unicode_codepoints = unicode codepoint changing visible direction of text present in {$label}
.label = this {$label} contains {$count ->
[one] an invisible
Expand Down
17 changes: 1 addition & 16 deletions compiler/rustc_lint/src/early/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@
use std::borrow::Cow;

use rustc_ast::util::unicode::TEXT_FLOW_CONTROL_CHARS;
use rustc_errors::{
Applicability, Diag, DiagArgValue, LintDiagnostic, elided_lifetime_in_path_suggestion,
};
use rustc_errors::{Applicability, Diag, DiagArgValue, LintDiagnostic};
use rustc_middle::middle::stability;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
Expand Down Expand Up @@ -75,19 +73,6 @@ pub fn decorate_builtin_lint(
lints::MacroExpandedMacroExportsAccessedByAbsolutePaths { definition: span_def }
.decorate_lint(diag)
}

BuiltinLintDiag::ElidedLifetimesInPaths(n, path_span, incl_angl_brckt, insertion_span) => {
lints::ElidedLifetimesInPaths {
subdiag: elided_lifetime_in_path_suggestion(
sess.source_map(),
n,
path_span,
incl_angl_brckt,
insertion_span,
),
}
.decorate_lint(diag);
}
BuiltinLintDiag::UnknownCrateTypes { span, candidate } => {
let sugg = candidate.map(|candidate| lints::UnknownCrateTypesSub { span, candidate });
lints::UnknownCrateTypes { sugg }.decorate_lint(diag);
Expand Down
16 changes: 14 additions & 2 deletions compiler/rustc_lint/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ fn register_builtins(store: &mut LintStore) {
BARE_TRAIT_OBJECTS,
UNUSED_EXTERN_CRATES,
ELLIPSIS_INCLUSIVE_RANGE_PATTERNS,
ELIDED_LIFETIMES_IN_PATHS,
HIDDEN_LIFETIMES_IN_OUTPUT_PATHS,
HIDDEN_LIFETIMES_IN_INPUT_PATHS,
HIDDEN_LIFETIMES_IN_TYPE_PATHS,
EXPLICIT_OUTLIVES_REQUIREMENTS,
// FIXME(#52665, #47816) not always applicable and not all
// macros are ready for this yet.
Expand All @@ -340,9 +342,15 @@ fn register_builtins(store: &mut LintStore) {

add_lint_group!("deprecated_safe", DEPRECATED_SAFE_2024);

add_lint_group!(
"hidden_lifetimes_in_paths",
HIDDEN_LIFETIMES_IN_OUTPUT_PATHS,
HIDDEN_LIFETIMES_IN_INPUT_PATHS,
HIDDEN_LIFETIMES_IN_TYPE_PATHS,
);

// Register renamed and removed lints.
store.register_renamed("single_use_lifetime", "single_use_lifetimes");
store.register_renamed("elided_lifetime_in_path", "elided_lifetimes_in_paths");
store.register_renamed("bare_trait_object", "bare_trait_objects");
store.register_renamed("unstable_name_collision", "unstable_name_collisions");
store.register_renamed("unused_doc_comment", "unused_doc_comments");
Expand All @@ -358,6 +366,10 @@ fn register_builtins(store: &mut LintStore) {
store.register_renamed("temporary_cstring_as_ptr", "dangling_pointers_from_temporaries");
store.register_renamed("elided_named_lifetimes", "mismatched_lifetime_syntaxes");

// Register renamed lint groups
store.register_renamed_group("elided_lifetime_in_path", "hidden_lifetimes_in_paths");
store.register_renamed_group("elided_lifetimes_in_paths", "hidden_lifetimes_in_paths");

// These were moved to tool lints, but rustc still sees them when compiling normally, before
// tool lints are registered, so `check_tool_name_for_backwards_compat` doesn't work. Use
// `register_removed` explicitly.
Expand Down
9 changes: 3 additions & 6 deletions compiler/rustc_lint/src/lifetime_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,9 @@ declare_lint! {
/// ### Example
///
/// ```rust,compile_fail
/// #![deny(hidden_lifetimes_in_input_paths)]
///
/// struct ContainsLifetime<'a>(&'a i32);
///
/// #[deny(hidden_lifetimes_in_input_paths)]
/// fn foo(x: ContainsLifetime) {}
/// ```
///
Expand Down Expand Up @@ -117,10 +116,9 @@ declare_lint! {
/// ### Example
///
/// ```rust,compile_fail
/// #![deny(hidden_lifetimes_in_output_paths)]
///
/// struct ContainsLifetime<'a>(&'a i32);
///
/// #[deny(hidden_lifetimes_in_output_paths)]
/// fn foo(x: &i32) -> ContainsLifetime {
/// ContainsLifetime(x)
/// }
Expand Down Expand Up @@ -662,10 +660,9 @@ declare_lint! {
/// ### Example
///
/// ```rust,compile_fail
/// #![deny(hidden_lifetimes_in_type_paths)]
///
/// struct ContainsLifetime<'a>(&'a i32);
///
/// #[deny(hidden_lifetimes_in_type_paths)]
/// static FOO: ContainsLifetime = ContainsLifetime(&42);
/// ```
///
Expand Down
11 changes: 2 additions & 9 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use std::num::NonZero;
use rustc_abi::ExternAbi;
use rustc_errors::codes::*;
use rustc_errors::{
Applicability, Diag, DiagArgValue, DiagMessage, DiagStyledString, ElidedLifetimeInPathSubdiag,
EmissionGuarantee, LintDiagnostic, MultiSpan, Subdiagnostic, SuggestionStyle,
Applicability, Diag, DiagArgValue, DiagMessage, DiagStyledString, EmissionGuarantee,
LintDiagnostic, MultiSpan, Subdiagnostic, SuggestionStyle,
};
use rustc_hir as hir;
use rustc_hir::def::Namespace;
Expand Down Expand Up @@ -2715,13 +2715,6 @@ pub(crate) struct MacroExpandedMacroExportsAccessedByAbsolutePaths {
pub definition: Span,
}

#[derive(LintDiagnostic)]
#[diag(lint_hidden_lifetime_parameters)]
pub(crate) struct ElidedLifetimesInPaths {
#[subdiagnostic]
pub subdiag: ElidedLifetimeInPathSubdiag,
}

#[derive(LintDiagnostic)]
#[diag(lint_invalid_crate_type_value)]
pub(crate) struct UnknownCrateTypes {
Expand Down
36 changes: 0 additions & 36 deletions compiler/rustc_lint_defs/src/builtin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ declare_lint_pass! {
DEPRECATED_WHERE_CLAUSE_LOCATION,
DUPLICATE_MACRO_ATTRIBUTES,
ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
ELIDED_LIFETIMES_IN_PATHS,
EXPLICIT_BUILTIN_CFGS_IN_FLAGS,
EXPORTED_PRIVATE_DEPENDENCIES,
FFI_UNWIND_CALLS,
Expand Down Expand Up @@ -1750,41 +1749,6 @@ declare_lint! {
};
}

declare_lint! {
/// The `elided_lifetimes_in_paths` lint detects the use of hidden
/// lifetime parameters.
///
/// ### Example
///
/// ```rust,compile_fail
/// #![deny(elided_lifetimes_in_paths)]
/// #![deny(warnings)]
/// struct Foo<'a> {
/// x: &'a u32
/// }
///
/// fn foo(x: &Foo) {
/// }
/// ```
///
/// {{produces}}
///
/// ### Explanation
///
/// Elided lifetime parameters can make it difficult to see at a glance
/// that borrowing is occurring. This lint ensures that lifetime
/// parameters are always explicitly stated, even if it is the `'_`
/// [placeholder lifetime].
///
/// This lint is "allow" by default because it has some known issues, and
/// may require a significant transition for old code.
///
/// [placeholder lifetime]: https://doc.rust-lang.org/reference/lifetime-elision.html#lifetime-elision-in-functions
pub ELIDED_LIFETIMES_IN_PATHS,
Allow,
"hidden lifetime parameters in types are deprecated"
}

declare_lint! {
/// The `bare_trait_objects` lint suggests using `dyn Trait` for trait
/// objects.
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_lint_defs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,6 @@ pub enum BuiltinLintDiag {
ident: Ident,
},
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
ElidedLifetimesInPaths(usize, Span, bool, Span),
UnknownCrateTypes {
span: Span,
candidate: Option<Symbol>,
Expand Down
16 changes: 0 additions & 16 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2104,7 +2104,6 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
kind,
count: expected_lifetimes,
};
let mut should_lint = true;
for rib in self.lifetime_ribs.iter().rev() {
match rib.kind {
// In create-parameter mode we error here because we don't want to support
Expand All @@ -2127,7 +2126,6 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
span: path_span,
subdiag,
});
should_lint = false;

for id in node_ids {
self.record_lifetime_res(
Expand Down Expand Up @@ -2196,20 +2194,6 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
}
}
}

if should_lint {
self.r.lint_buffer.buffer_lint(
lint::builtin::ELIDED_LIFETIMES_IN_PATHS,
segment_id,
elided_lifetime_span,
lint::BuiltinLintDiag::ElidedLifetimesInPaths(
expected_lifetimes,
path_span,
!segment.has_generic_args,
elided_lifetime_span,
),
);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/tools/lint-docs/src/groups.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ static GROUP_DESCRIPTIONS: &[(&str, &str)] = &[
("let-underscore", "Lints that detect wildcard let bindings that are likely to be invalid"),
("rustdoc", "Rustdoc-specific lints"),
("rust-2018-idioms", "Lints to nudge you toward idiomatic features of Rust 2018"),
("hidden-lifetimes-in-paths", "Lints that detect the use of hidden lifetime parameters"),
("nonstandard-style", "Violation of standard naming conventions"),
("future-incompatible", "Lints that detect code that has future-compatibility problems"),
("rust-2018-compatibility", "Lints used to transition code from the 2015 edition to 2018"),
Expand Down
2 changes: 1 addition & 1 deletion src/tools/lint-docs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static RENAMES: &[(Level, &[(&str, &str)])] = &[
Level::Allow,
&[
("single-use-lifetime", "single-use-lifetimes"),
("elided-lifetime-in-path", "elided-lifetimes-in-paths"),
("elided-lifetime-in-path", "hidden-lifetimes-in-paths"),
("async-idents", "keyword-idents"),
("disjoint-capture-migration", "rust-2021-incompatible-closure-captures"),
("keyword-idents", "keyword-idents-2018"),
Expand Down
2 changes: 0 additions & 2 deletions src/tools/tidy/src/issues.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2618,7 +2618,6 @@ ui/let-else/issue-100103.rs
ui/let-else/issue-102317.rs
ui/let-else/issue-94176.rs
ui/let-else/issue-99975.rs
ui/lifetimes/auxiliary/issue-91763-aux.rs
ui/lifetimes/issue-103582-hint-for-missing-lifetime-bound-on-trait-object-using-type-alias.rs
ui/lifetimes/issue-104432-unused-lifetimes-in-expansion.rs
ui/lifetimes/issue-105227.rs
Expand Down Expand Up @@ -2651,7 +2650,6 @@ ui/lifetimes/issue-84398.rs
ui/lifetimes/issue-84604.rs
ui/lifetimes/issue-90170-elision-mismatch.rs
ui/lifetimes/issue-90600-expected-return-static-indirect.rs
ui/lifetimes/issue-91763.rs
ui/lifetimes/issue-93911.rs
ui/lifetimes/issue-95023.rs
ui/lifetimes/issue-97193.rs
Expand Down
42 changes: 0 additions & 42 deletions tests/ui/lifetimes/auxiliary/issue-91763-aux.rs

This file was deleted.

11 changes: 0 additions & 11 deletions tests/ui/lifetimes/elided-lint-in-mod.rs

This file was deleted.

20 changes: 0 additions & 20 deletions tests/ui/lifetimes/elided-lint-in-mod.stderr

This file was deleted.

11 changes: 11 additions & 0 deletions tests/ui/lifetimes/hidden-lifetimes-in-mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
struct Foo<'a>(&'a ());

fn test(_: Foo) {}

#[deny(hidden_lifetimes_in_paths)]
mod w {
fn test2(_: super::Foo) {}
//~^ ERROR paths containing hidden lifetime parameters are deprecated
}
Comment on lines +5 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: can you add a little more elaboration on what this test intends to exercise? Is it to check that we recurse into items with mods, and/or the lint level can be properly controlled through lint level attributes?


fn main() {}
19 changes: 19 additions & 0 deletions tests/ui/lifetimes/hidden-lifetimes-in-mod.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: paths containing hidden lifetime parameters are deprecated
--> $DIR/hidden-lifetimes-in-mod.rs:7:17
|
LL | fn test2(_: super::Foo) {}
| ^^^^^^^^^^
|
note: the lint level is defined here
--> $DIR/hidden-lifetimes-in-mod.rs:5:8
|
LL | #[deny(hidden_lifetimes_in_paths)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
= note: `#[deny(hidden_lifetimes_in_input_paths)]` implied by `#[deny(hidden_lifetimes_in_paths)]`
help: indicate the anonymous lifetime
|
LL | fn test2(_: super::Foo<'_>) {}
| ++++

error: aborting due to 1 previous error

Loading
Loading