From c072e0120da0907369451b741b8d6db69bc55eac Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Fri, 8 Aug 2025 08:40:13 +0000 Subject: [PATCH] refactor(all): add missing lifetimes in function return types (#12895) Where a type being returned by a function has lifetimes, specify them with `'_` instead of omitting them entirely. `Cow` -> `Cow<'_, str>`. This is split out from Rust upgrade PR #12873, so can see the diff in that PR more clearly. Note: This is very slightly different from what was in #12873. Clippy auto-fix introduced a couple of unnecessary and weird-looking anonymous lifetimes in function params, which this PR skips. --- apps/oxlint/src/output_formatter/xml_utils.rs | 4 ++-- crates/oxc_allocator/src/pool.rs | 2 +- crates/oxc_allocator/src/pool_fixed_size.rs | 2 +- crates/oxc_allocator/src/vec2/mod.rs | 4 ++-- crates/oxc_ast/src/ast_impl/js.rs | 2 +- crates/oxc_ast/src/ast_impl/literal.rs | 2 +- crates/oxc_ast/src/ast_kind_impl.rs | 2 +- crates/oxc_ast_visit/src/utf8_to_utf16.rs | 2 +- crates/oxc_ecmascript/src/private_bound_identifiers.rs | 10 +++++----- .../oxc_formatter/src/formatter/format_element/mod.rs | 2 +- crates/oxc_formatter/src/formatter/formatter.rs | 2 +- crates/oxc_formatter/src/formatter/token/string.rs | 2 +- crates/oxc_formatter/src/formatter/trivia.rs | 2 +- crates/oxc_linter/src/config/settings/jsdoc.rs | 4 ++-- .../src/rules/unicorn/numeric_separators_style.rs | 2 +- crates/oxc_linter/src/service/runtime.rs | 2 +- crates/oxc_semantic/src/class/table.rs | 2 +- crates/oxc_semantic/src/jsdoc/parser/parse.rs | 7 +++++-- crates/oxc_semantic/src/lib.rs | 2 +- crates/oxc_semantic/src/scoping.rs | 6 +++--- crates/oxc_semantic/tests/integration/util/mod.rs | 8 ++++---- crates/oxc_transformer/src/jsx/jsx_impl.rs | 2 +- tasks/ast_tools/src/parse/attr.rs | 2 +- 23 files changed, 39 insertions(+), 36 deletions(-) diff --git a/apps/oxlint/src/output_formatter/xml_utils.rs b/apps/oxlint/src/output_formatter/xml_utils.rs index 6acde7e359636..70ff8ae3c2f93 100644 --- a/apps/oxlint/src/output_formatter/xml_utils.rs +++ b/apps/oxlint/src/output_formatter/xml_utils.rs @@ -1,11 +1,11 @@ use std::borrow::Cow; /// -pub fn xml_escape(raw: &str) -> Cow { +pub fn xml_escape(raw: &str) -> Cow<'_, str> { xml_escape_impl(raw, |ch| matches!(ch, b'<' | b'>' | b'&' | b'\'' | b'\"')) } -fn xml_escape_impl bool>(raw: &str, escape_chars: F) -> Cow { +fn xml_escape_impl bool>(raw: &str, escape_chars: F) -> Cow<'_, str> { let bytes = raw.as_bytes(); let mut escaped = None; let mut iter = bytes.iter(); diff --git a/crates/oxc_allocator/src/pool.rs b/crates/oxc_allocator/src/pool.rs index b0505d0d32c6e..9198867649ea7 100644 --- a/crates/oxc_allocator/src/pool.rs +++ b/crates/oxc_allocator/src/pool.rs @@ -24,7 +24,7 @@ impl AllocatorPool { /// # Panics /// /// Panics if the underlying mutex is poisoned. - pub fn get(&self) -> AllocatorGuard { + pub fn get(&self) -> AllocatorGuard<'_> { let allocator = { let mut allocators = self.allocators.lock().unwrap(); allocators.pop() diff --git a/crates/oxc_allocator/src/pool_fixed_size.rs b/crates/oxc_allocator/src/pool_fixed_size.rs index 90ee69b5564cd..8be0e96a260c5 100644 --- a/crates/oxc_allocator/src/pool_fixed_size.rs +++ b/crates/oxc_allocator/src/pool_fixed_size.rs @@ -45,7 +45,7 @@ impl AllocatorPool { /// # Panics /// /// Panics if the underlying mutex is poisoned. - pub fn get(&self) -> AllocatorGuard { + pub fn get(&self) -> AllocatorGuard<'_> { let allocator = { let mut allocators = self.allocators.lock().unwrap(); allocators.pop() diff --git a/crates/oxc_allocator/src/vec2/mod.rs b/crates/oxc_allocator/src/vec2/mod.rs index f3ed7756c4b75..68aea00607b7b 100644 --- a/crates/oxc_allocator/src/vec2/mod.rs +++ b/crates/oxc_allocator/src/vec2/mod.rs @@ -1621,7 +1621,7 @@ impl<'a, T: 'a, A: Alloc> Vec<'a, T, A> { /// v.drain(..); /// assert_eq!(v, &[]); /// ``` - pub fn drain(&mut self, range: R) -> Drain + pub fn drain(&mut self, range: R) -> Drain<'_, '_, T, A> where R: RangeBounds, { @@ -2280,7 +2280,7 @@ impl<'a, T: 'a, A: Alloc> Vec<'a, T, A> { /// assert_eq!(u, &[1, 2]); /// ``` #[inline] - pub fn splice(&mut self, range: R, replace_with: I) -> Splice + pub fn splice(&mut self, range: R, replace_with: I) -> Splice<'_, '_, I::IntoIter, A> where R: RangeBounds, I: IntoIterator, diff --git a/crates/oxc_ast/src/ast_impl/js.rs b/crates/oxc_ast/src/ast_impl/js.rs index 658b2aa663632..c07f2f8012344 100644 --- a/crates/oxc_ast/src/ast_impl/js.rs +++ b/crates/oxc_ast/src/ast_impl/js.rs @@ -786,7 +786,7 @@ impl CallExpression<'_> { /// require() // => false /// require(123) // => false /// ``` - pub fn common_js_require(&self) -> Option<&StringLiteral> { + pub fn common_js_require(&self) -> Option<&StringLiteral<'_>> { if !(self.callee.is_specific_id("require") && self.arguments.len() == 1) { return None; } diff --git a/crates/oxc_ast/src/ast_impl/literal.rs b/crates/oxc_ast/src/ast_impl/literal.rs index f25d2b5596daa..31bd0e3996bb8 100644 --- a/crates/oxc_ast/src/ast_impl/literal.rs +++ b/crates/oxc_ast/src/ast_impl/literal.rs @@ -58,7 +58,7 @@ impl NumericLiteral<'_> { /// Return raw source code for `NumericLiteral`. /// If `raw` is `None` (node is generated, not parsed from source), fallback to formatting `value`. - pub fn raw_str(&self) -> Cow { + pub fn raw_str(&self) -> Cow<'_, str> { match self.raw.as_ref() { Some(raw) => Cow::Borrowed(raw), None => Cow::Owned(format!("{}", self.value)), diff --git a/crates/oxc_ast/src/ast_kind_impl.rs b/crates/oxc_ast/src/ast_kind_impl.rs index bb40306b96cf1..43c8950e0977d 100644 --- a/crates/oxc_ast/src/ast_kind_impl.rs +++ b/crates/oxc_ast/src/ast_kind_impl.rs @@ -251,7 +251,7 @@ impl AstKind<'_> { /// /// Note that this method does not exist in release builds. Do not include /// usage of this method within your code. - pub fn debug_name(&self) -> std::borrow::Cow { + pub fn debug_name(&self) -> std::borrow::Cow<'_, str> { use std::borrow::Cow; const COMPUTED: Cow<'static, str> = Cow::Borrowed(""); diff --git a/crates/oxc_ast_visit/src/utf8_to_utf16.rs b/crates/oxc_ast_visit/src/utf8_to_utf16.rs index b50b08d3dd098..5418c699e0745 100644 --- a/crates/oxc_ast_visit/src/utf8_to_utf16.rs +++ b/crates/oxc_ast_visit/src/utf8_to_utf16.rs @@ -59,7 +59,7 @@ impl Utf8ToUtf16 { /// It will also correctly handle offsets in any order, but at a performance cost. /// /// Returns `None` if the source text is entirely ASCII, and so requires no conversion. - pub fn converter(&self) -> Option { + pub fn converter(&self) -> Option> { if self.translations.is_empty() { None } else { diff --git a/crates/oxc_ecmascript/src/private_bound_identifiers.rs b/crates/oxc_ecmascript/src/private_bound_identifiers.rs index 4c27f50680d17..708c4ebd4b703 100644 --- a/crates/oxc_ecmascript/src/private_bound_identifiers.rs +++ b/crates/oxc_ecmascript/src/private_bound_identifiers.rs @@ -5,11 +5,11 @@ use oxc_ast::ast::{ /// [`PrivateBoundIdentifiers`](https://tc39.es/ecma262/#sec-static-semantics-privateboundidentifiers) pub trait PrivateBoundIdentifiers { - fn private_bound_identifiers(&self) -> Option; + fn private_bound_identifiers(&self) -> Option>; } impl PrivateBoundIdentifiers for ClassElement<'_> { - fn private_bound_identifiers(&self) -> Option { + fn private_bound_identifiers(&self) -> Option> { match self { ClassElement::StaticBlock(_) | ClassElement::TSIndexSignature(_) => None, ClassElement::MethodDefinition(def) => def.private_bound_identifiers(), @@ -20,7 +20,7 @@ impl PrivateBoundIdentifiers for ClassElement<'_> { } impl PrivateBoundIdentifiers for MethodDefinition<'_> { - fn private_bound_identifiers(&self) -> Option { + fn private_bound_identifiers(&self) -> Option> { self.value.body.as_ref()?; if let PropertyKey::PrivateIdentifier(ident) = &self.key { return Some((*ident).clone()); @@ -30,7 +30,7 @@ impl PrivateBoundIdentifiers for MethodDefinition<'_> { } impl PrivateBoundIdentifiers for PropertyDefinition<'_> { - fn private_bound_identifiers(&self) -> Option { + fn private_bound_identifiers(&self) -> Option> { if let PropertyKey::PrivateIdentifier(ident) = &self.key { return Some((*ident).clone()); } @@ -39,7 +39,7 @@ impl PrivateBoundIdentifiers for PropertyDefinition<'_> { } impl PrivateBoundIdentifiers for AccessorProperty<'_> { - fn private_bound_identifiers(&self) -> Option { + fn private_bound_identifiers(&self) -> Option> { if let PropertyKey::PrivateIdentifier(ident) = &self.key { return Some((*ident).clone()); } diff --git a/crates/oxc_formatter/src/formatter/format_element/mod.rs b/crates/oxc_formatter/src/formatter/format_element/mod.rs index 4ce63245b9b5d..2ea69c2966fff 100644 --- a/crates/oxc_formatter/src/formatter/format_element/mod.rs +++ b/crates/oxc_formatter/src/formatter/format_element/mod.rs @@ -169,7 +169,7 @@ pub const LINE_TERMINATORS: [char; 3] = ['\r', LINE_SEPARATOR, PARAGRAPH_SEPARAT /// Replace the line terminators matching the provided list with "\n" /// since its the only line break type supported by the printer -pub fn normalize_newlines(text: &str, terminators: [char; N]) -> Cow { +pub fn normalize_newlines(text: &str, terminators: [char; N]) -> Cow<'_, str> { let mut result = String::new(); let mut last_end = 0; diff --git a/crates/oxc_formatter/src/formatter/formatter.rs b/crates/oxc_formatter/src/formatter/formatter.rs index 631b9a7a21d18..690bf11dd2bef 100644 --- a/crates/oxc_formatter/src/formatter/formatter.rs +++ b/crates/oxc_formatter/src/formatter/formatter.rs @@ -57,7 +57,7 @@ impl<'buf, 'ast> Formatter<'buf, 'ast> { /// Returns the comments from the context. #[inline] - pub fn comments(&self) -> &Comments { + pub fn comments(&self) -> &Comments<'_> { self.context().comments() } diff --git a/crates/oxc_formatter/src/formatter/token/string.rs b/crates/oxc_formatter/src/formatter/token/string.rs index 106054dabd3aa..d3020a0933862 100644 --- a/crates/oxc_formatter/src/formatter/token/string.rs +++ b/crates/oxc_formatter/src/formatter/token/string.rs @@ -49,7 +49,7 @@ pub fn normalize_string( raw_content: &str, preferred_quote: Quote, quotes_will_change: bool, -) -> Cow { +) -> Cow<'_, str> { let alternate_quote = preferred_quote.other().as_byte(); let preferred_quote = preferred_quote.as_byte(); let mut reduced_string = String::new(); diff --git a/crates/oxc_formatter/src/formatter/trivia.rs b/crates/oxc_formatter/src/formatter/trivia.rs index 2c9e1f5f2e757..9f694eb2d9f14 100644 --- a/crates/oxc_formatter/src/formatter/trivia.rs +++ b/crates/oxc_formatter/src/formatter/trivia.rs @@ -367,7 +367,7 @@ impl<'a> Format<'a> for FormatDanglingComments<'a> { /// /// ## Warning /// It's your responsibility to format any skipped trivia. -pub const fn format_trimmed_token(token: &SyntaxToken) -> FormatTrimmedToken { +pub const fn format_trimmed_token(token: &SyntaxToken) -> FormatTrimmedToken<'_> { FormatTrimmedToken { token } } diff --git a/crates/oxc_linter/src/config/settings/jsdoc.rs b/crates/oxc_linter/src/config/settings/jsdoc.rs index 5fb847f2d7271..f68b786e191a1 100644 --- a/crates/oxc_linter/src/config/settings/jsdoc.rs +++ b/crates/oxc_linter/src/config/settings/jsdoc.rs @@ -100,7 +100,7 @@ impl Default for JSDocPluginSettings { impl JSDocPluginSettings { /// Only for `check-tag-names` rule /// Return `Some(reason)` if blocked - pub fn check_blocked_tag_name(&self, tag_name: &str) -> Option> { + pub fn check_blocked_tag_name(&self, tag_name: &str) -> Option> { match self.tag_name_preference.get(tag_name) { Some(TagNamePreference::FalseOnly(_)) => { Some(Cow::Owned(format!("Unexpected tag `@{tag_name}`."))) @@ -112,7 +112,7 @@ impl JSDocPluginSettings { /// Only for `check-tag-names` rule /// Return `Some(reason)` if replacement found or default aliased - pub fn check_preferred_tag_name(&self, original_name: &str) -> Option> { + pub fn check_preferred_tag_name(&self, original_name: &str) -> Option> { let reason = |preferred_name: &str| -> Cow { Cow::Owned(format!("Replace tag `@{original_name}` with `@{preferred_name}`.")) }; diff --git a/crates/oxc_linter/src/rules/unicorn/numeric_separators_style.rs b/crates/oxc_linter/src/rules/unicorn/numeric_separators_style.rs index 4a6975f2a8046..d42161f2f44fe 100644 --- a/crates/oxc_linter/src/rules/unicorn/numeric_separators_style.rs +++ b/crates/oxc_linter/src/rules/unicorn/numeric_separators_style.rs @@ -325,7 +325,7 @@ struct ParsedNumberLiteral<'n> { exponent_part: Option<&'n str>, } -fn parse_number_literal(num: &str) -> ParsedNumberLiteral { +fn parse_number_literal(num: &str) -> ParsedNumberLiteral<'_> { let mut parsed = ParsedNumberLiteral { integer_part: None, decimal_part: None, diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index 7a886e467b909..cf3911b755ba0 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -795,7 +795,7 @@ impl Runtime { path: &Arc, check_syntax_errors: bool, tx_error: &DiagnosticSender, - ) -> ModuleProcessOutput { + ) -> ModuleProcessOutput<'_> { let default_output = || ModuleProcessOutput { path: Arc::clone(path), processed_module: ProcessedModule::default(), diff --git a/crates/oxc_semantic/src/class/table.rs b/crates/oxc_semantic/src/class/table.rs index b56c124942062..13c379a18a331 100644 --- a/crates/oxc_semantic/src/class/table.rs +++ b/crates/oxc_semantic/src/class/table.rs @@ -71,7 +71,7 @@ impl<'a> ClassTable<'a> { pub fn iter_private_identifiers( &self, class_id: ClassId, - ) -> impl Iterator + '_ { + ) -> impl Iterator> + '_ { self.private_identifier_references[class_id].iter() } diff --git a/crates/oxc_semantic/src/jsdoc/parser/parse.rs b/crates/oxc_semantic/src/jsdoc/parser/parse.rs index 204b04488b1ab..38d0c4448fe38 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/parse.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/parse.rs @@ -8,7 +8,10 @@ use super::{ /// source_text: Inside of /**HERE*/, NOT includes `/**` and `*/` /// span_start: Global positioned `Span` start for this JSDoc comment -pub fn parse_jsdoc(source_text: &str, jsdoc_span_start: u32) -> (JSDocCommentPart, Vec) { +pub fn parse_jsdoc( + source_text: &str, + jsdoc_span_start: u32, +) -> (JSDocCommentPart<'_>, Vec>) { debug_assert!(!source_text.starts_with("/*")); debug_assert!(!source_text.ends_with("*/")); @@ -115,7 +118,7 @@ pub fn parse_jsdoc(source_text: &str, jsdoc_span_start: u32) -> (JSDocCommentPar } /// tag_content: Starts with `@`, may be multiline -fn parse_jsdoc_tag(tag_content: &str, jsdoc_tag_span: Span) -> JSDocTag { +fn parse_jsdoc_tag(tag_content: &str, jsdoc_tag_span: Span) -> JSDocTag<'_> { debug_assert!(tag_content.starts_with('@')); // This surely exists, at least `@` itself let (k_start, k_end) = utils::find_token_range(tag_content).unwrap(); diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index 2a5e678f9c045..ded2da4b0d3d0 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -120,7 +120,7 @@ impl<'a> Semantic<'a> { (&mut self.scoping, &self.nodes) } - pub fn classes(&self) -> &ClassTable { + pub fn classes(&self) -> &ClassTable<'_> { &self.classes } diff --git a/crates/oxc_semantic/src/scoping.rs b/crates/oxc_semantic/src/scoping.rs index b4b6bb0ac3e57..9b191a574c27a 100644 --- a/crates/oxc_semantic/src/scoping.rs +++ b/crates/oxc_semantic/src/scoping.rs @@ -472,7 +472,7 @@ impl Scoping { } #[inline] - pub fn root_unresolved_references(&self) -> &UnresolvedReferences { + pub fn root_unresolved_references(&self) -> &UnresolvedReferences<'_> { &self.cell.borrow_dependent().root_unresolved_references } @@ -635,7 +635,7 @@ impl Scoping { /// Get all bound identifiers in a scope. #[inline] - pub fn get_bindings(&self, scope_id: ScopeId) -> &Bindings { + pub fn get_bindings(&self, scope_id: ScopeId) -> &Bindings<'_> { &self.cell.borrow_dependent().bindings[scope_id] } @@ -652,7 +652,7 @@ impl Scoping { /// If you only want bindings in a specific scope, use [`iter_bindings_in`]. /// /// [`iter_bindings_in`]: Scoping::iter_bindings_in - pub fn iter_bindings(&self) -> impl Iterator + '_ { + pub fn iter_bindings(&self) -> impl Iterator)> + '_ { self.cell.borrow_dependent().bindings.iter_enumerated() } diff --git a/crates/oxc_semantic/tests/integration/util/mod.rs b/crates/oxc_semantic/tests/integration/util/mod.rs index 1d1c99201331c..60471770fa1fa 100644 --- a/crates/oxc_semantic/tests/integration/util/mod.rs +++ b/crates/oxc_semantic/tests/integration/util/mod.rs @@ -208,7 +208,7 @@ impl<'a> SemanticTester<'a> { /// /// ## Fails /// If no symbol with the given name exists at the top-level scope. - pub fn has_root_symbol(&self, name: &str) -> SymbolTester { + pub fn has_root_symbol(&self, name: &str) -> SymbolTester<'_> { SymbolTester::new_at_root(self, self.build(), name) } @@ -216,7 +216,7 @@ impl<'a> SemanticTester<'a> { /// /// ## Fails /// 1. No symbol with the given name exists, - pub fn has_symbol(&self, name: &str) -> SymbolTester { + pub fn has_symbol(&self, name: &str) -> SymbolTester<'_> { SymbolTester::new_first_binding(self, self.build(), name) } @@ -224,7 +224,7 @@ impl<'a> SemanticTester<'a> { /// /// ## Fails /// If no class with the given name exists. - pub fn has_class(&self, name: &str) -> ClassTester { + pub fn has_class(&self, name: &str) -> ClassTester<'_> { ClassTester::has_class(self.build(), name) } @@ -263,7 +263,7 @@ impl<'a> SemanticTester<'a> { /// 1. No symbol with the given name exists, /// 2. More than one symbol with the given name exists, so a symbol cannot /// be uniquely obtained. - pub fn has_some_symbol(&self, name: &str) -> SymbolTester { + pub fn has_some_symbol(&self, name: &str) -> SymbolTester<'_> { SymbolTester::new_unique(self, self.build(), name) } diff --git a/crates/oxc_transformer/src/jsx/jsx_impl.rs b/crates/oxc_transformer/src/jsx/jsx_impl.rs index f20381c4fb63e..0f93ba652e2db 100644 --- a/crates/oxc_transformer/src/jsx/jsx_impl.rs +++ b/crates/oxc_transformer/src/jsx/jsx_impl.rs @@ -310,7 +310,7 @@ impl<'a, 'ctx> AutomaticModuleBindings<'a, 'ctx> { } #[inline] -fn get_import_source(jsx_runtime_importer: &str, react_importer_len: u32) -> Atom { +fn get_import_source(jsx_runtime_importer: &str, react_importer_len: u32) -> Atom<'_> { Atom::from(&jsx_runtime_importer[..react_importer_len as usize]) } diff --git a/tasks/ast_tools/src/parse/attr.rs b/tasks/ast_tools/src/parse/attr.rs index de305f86ec1c8..5013fbf5b3552 100644 --- a/tasks/ast_tools/src/parse/attr.rs +++ b/tasks/ast_tools/src/parse/attr.rs @@ -91,7 +91,7 @@ pub enum AttrLocation<'d> { impl AttrLocation<'_> { /// Convert `&mut AttrLocation` to `AttrLocation`. - pub fn unpack(&mut self) -> AttrLocation { + pub fn unpack(&mut self) -> AttrLocation<'_> { match self { AttrLocation::Struct(struct_def) => AttrLocation::Struct(struct_def), AttrLocation::Enum(enum_def) => AttrLocation::Enum(enum_def),