Skip to content

Commit

Permalink
refactor(transformer): elide lifetimes where possible (#8285)
Browse files Browse the repository at this point in the history
Pure refactor. Enable `clippy::needless_lifetimes` lint rule in `oxc_transformer`, and fix what the rule flags - remove unnecessary lifetimes which can be elided.

Also rename a few lifetimes `'b` and `'c` to more descriptive `'ctx`.
  • Loading branch information
overlookmotel committed Jan 6, 2025
1 parent e81f34f commit 109b8fc
Show file tree
Hide file tree
Showing 51 changed files with 100 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub struct ArrowFunctionConverter<'a> {
super_methods: Option<FxIndexMap<SuperMethodKey<'a>, SuperMethodInfo<'a>>>,
}

impl<'a> ArrowFunctionConverter<'a> {
impl ArrowFunctionConverter<'_> {
pub fn new(env: &EnvOptions) -> Self {
let mode = if env.es2015.arrow_function.is_some() {
ArrowFunctionConverterMode::Enabled
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/common/helper_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ pub struct HelperLoaderStore<'a> {
pub(crate) used_helpers: RefCell<FxHashMap<Helper, String>>,
}

impl<'a> HelperLoaderStore<'a> {
impl HelperLoaderStore<'_> {
pub fn new(options: &HelperLoaderOptions) -> Self {
Self {
module_name: options.module_name.clone(),
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<'a, 'ctx> Common<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for Common<'a, 'ctx> {
impl<'a> Traverse<'a> for Common<'a, '_> {
fn exit_program(&mut self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.module_imports.exit_program(program, ctx);
self.var_declarations.exit_program(program, ctx);
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/common/module_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a, 'ctx> ModuleImports<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ModuleImports<'a, 'ctx> {
impl<'a> Traverse<'a> for ModuleImports<'a, '_> {
fn exit_program(&mut self, _program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
self.ctx.module_imports.insert_into_program(self.ctx, ctx);
}
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/common/statement_injector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<'a, 'ctx> StatementInjector<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for StatementInjector<'a, 'ctx> {
impl<'a> Traverse<'a> for StatementInjector<'a, '_> {
fn exit_statements(
&mut self,
statements: &mut ArenaVec<'a, Statement<'a>>,
Expand Down Expand Up @@ -61,7 +61,7 @@ pub struct StatementInjectorStore<'a> {
}

// Public methods
impl<'a> StatementInjectorStore<'a> {
impl StatementInjectorStore<'_> {
/// Create new `StatementInjectorStore`.
pub fn new() -> Self {
Self { insertions: RefCell::new(FxHashMap::default()) }
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/common/top_level_statements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl<'a, 'ctx> TopLevelStatements<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for TopLevelStatements<'a, 'ctx> {
impl<'a> Traverse<'a> for TopLevelStatements<'a, '_> {
fn exit_program(&mut self, program: &mut Program<'a>, _ctx: &mut TraverseCtx<'a>) {
self.ctx.top_level_statements.insert_into_program(program);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/common/var_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<'a, 'ctx> VarDeclarations<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for VarDeclarations<'a, 'ctx> {
impl<'a> Traverse<'a> for VarDeclarations<'a, '_> {
fn enter_statements(
&mut self,
_stmts: &mut ArenaVec<'a, Statement<'a>>,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub struct TransformCtx<'a> {
pub top_level_statements: TopLevelStatementsStore<'a>,
}

impl<'a> TransformCtx<'a> {
impl TransformCtx<'_> {
pub fn new(source_path: &Path, options: &TransformOptions) -> Self {
let filename = source_path
.file_stem() // omit file extension
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2015/arrow_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,4 @@ impl<'a, 'ctx> ArrowFunctions<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ArrowFunctions<'a, 'ctx> {}
impl<'a> Traverse<'a> for ArrowFunctions<'a, '_> {}
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2015/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ impl<'a, 'ctx> ES2015<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ES2015<'a, 'ctx> {}
impl<'a> Traverse<'a> for ES2015<'a, '_> {}
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/es2016/exponentiation_operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ExponentiationOperator<'a, 'ctx> {
impl<'a> Traverse<'a> for ExponentiationOperator<'a, '_> {
// Note: Do not transform to `Math.pow` with BigInt arguments - that's a runtime error
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
match expr {
Expand Down Expand Up @@ -98,7 +98,7 @@ impl<'a, 'ctx> Traverse<'a> for ExponentiationOperator<'a, 'ctx> {
}
}

impl<'a, 'ctx> ExponentiationOperator<'a, 'ctx> {
impl<'a> ExponentiationOperator<'a, '_> {
/// Convert `BinaryExpression`.
///
/// `left ** right` -> `Math.pow(left, right)`
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2016/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a, 'ctx> ES2016<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ES2016<'a, 'ctx> {
impl<'a> Traverse<'a> for ES2016<'a, '_> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.exponentiation_operator {
self.exponentiation_operator.enter_expression(expr, ctx);
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_transformer/src/es2017/async_to_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
impl<'a> Traverse<'a> for AsyncToGenerator<'a, '_> {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let new_expr = match expr {
Expression::AwaitExpression(await_expr) => {
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<'a, 'ctx> Traverse<'a> for AsyncToGenerator<'a, 'ctx> {
}
}

impl<'a, 'ctx> AsyncToGenerator<'a, 'ctx> {
impl<'a> AsyncToGenerator<'a, '_> {
/// Check whether the current node is inside an async function.
fn is_inside_async_function(ctx: &mut TraverseCtx<'a>) -> bool {
// Early return if current scope is top because we don't need to transform top-level await expression.
Expand Down Expand Up @@ -819,7 +819,7 @@ impl<'a, 'ctx> BindingMover<'a, 'ctx> {
}
}

impl<'a, 'ctx> Visit<'a> for BindingMover<'a, 'ctx> {
impl<'a> Visit<'a> for BindingMover<'a, '_> {
/// Visits a binding identifier and moves it to the target scope.
fn visit_binding_identifier(&mut self, ident: &BindingIdentifier<'a>) {
let symbols = self.ctx.symbols();
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2017/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a, 'ctx> ES2017<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ES2017<'a, 'ctx> {
impl<'a> Traverse<'a> for ES2017<'a, '_> {
fn exit_expression(&mut self, node: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.async_to_generator {
self.async_to_generator.exit_expression(node, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use oxc_traverse::{Ancestor, BoundIdentifier, TraverseCtx};
use super::AsyncGeneratorFunctions;
use crate::common::helper_loader::Helper;

impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
impl<'a> AsyncGeneratorFunctions<'a, '_> {
/// Check the parent node to see if multiple statements are allowed.
fn is_multiple_statements_allowed(ctx: &TraverseCtx<'a>) -> bool {
matches!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for AsyncGeneratorFunctions<'a, 'ctx> {
impl<'a> Traverse<'a> for AsyncGeneratorFunctions<'a, '_> {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
let new_expr = match expr {
Expression::AwaitExpression(await_expr) => {
Expand Down Expand Up @@ -152,7 +152,7 @@ impl<'a, 'ctx> Traverse<'a> for AsyncGeneratorFunctions<'a, 'ctx> {
}
}

impl<'a, 'ctx> AsyncGeneratorFunctions<'a, 'ctx> {
impl<'a> AsyncGeneratorFunctions<'a, '_> {
/// Transform `yield * argument` expression to `yield asyncGeneratorDelegate(asyncIterator(argument))`.
fn transform_yield_expression(
&self,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2018/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'a, 'ctx> ES2018<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ES2018<'a, 'ctx> {
impl<'a> Traverse<'a> for ES2018<'a, '_> {
fn exit_program(&mut self, program: &mut oxc_ast::ast::Program<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.object_rest_spread.is_some() {
self.object_rest_spread.exit_program(program, ctx);
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_transformer/src/es2018/object_rest_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
impl<'a> Traverse<'a> for ObjectRestSpread<'a, '_> {
// For excluded keys when destructuring inside a function.
// `function foo() { ({a, ...b} = c) }` -> `const _excluded = ["a"]; function foo() { ... }`
fn exit_program(&mut self, _node: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down Expand Up @@ -181,7 +181,7 @@ impl<'a, 'ctx> Traverse<'a> for ObjectRestSpread<'a, 'ctx> {
}
}

impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
impl<'a> ObjectRestSpread<'a, '_> {
// Transform `({ x, ..y } = foo)`.
// Transform `([{ x, ..y }] = foo)`.
fn transform_assignment_expression(
Expand Down Expand Up @@ -532,7 +532,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
}
}

impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
impl<'a> ObjectRestSpread<'a, '_> {
// Transform `function foo({...x}) {}`.
fn transform_function(func: &mut Function<'a>, ctx: &mut TraverseCtx<'a>) {
let scope_id = func.scope_id();
Expand Down Expand Up @@ -769,7 +769,7 @@ impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
}
}

impl<'a, 'ctx> ObjectRestSpread<'a, 'ctx> {
impl<'a> ObjectRestSpread<'a, '_> {
// Transform `let { x, ..y } = foo`.
// Transform `let [{ x, ..y }] = foo`.
fn transform_variable_declaration(
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2020/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'a, 'ctx> ES2020<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ES2020<'a, 'ctx> {
impl<'a> Traverse<'a> for ES2020<'a, '_> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.nullish_coalescing_operator {
self.nullish_coalescing_operator.enter_expression(expr, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'a, 'ctx> NullishCoalescingOperator<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for NullishCoalescingOperator<'a, 'ctx> {
impl<'a> Traverse<'a> for NullishCoalescingOperator<'a, '_> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
// left ?? right
if !matches!(expr, Expression::LogicalExpression(logical_expr) if logical_expr.operator == LogicalOperator::Coalesce)
Expand All @@ -64,7 +64,7 @@ impl<'a, 'ctx> Traverse<'a> for NullishCoalescingOperator<'a, 'ctx> {
}
}

impl<'a, 'ctx> NullishCoalescingOperator<'a, 'ctx> {
impl<'a> NullishCoalescingOperator<'a, '_> {
fn transform_logical_expression(
&mut self,
logical_expr: ArenaBox<'a, LogicalExpression<'a>>,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/es2020/optional_chaining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl<'a, 'ctx> OptionalChaining<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for OptionalChaining<'a, 'ctx> {
impl<'a> Traverse<'a> for OptionalChaining<'a, '_> {
// `#[inline]` because this is a hot path
#[inline]
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
Expand Down Expand Up @@ -121,7 +121,7 @@ impl<'a, 'ctx> Traverse<'a> for OptionalChaining<'a, 'ctx> {
}
}

impl<'a, 'ctx> OptionalChaining<'a, 'ctx> {
impl<'a> OptionalChaining<'a, '_> {
fn set_temp_binding(&mut self, binding: BoundIdentifier<'a>) {
self.temp_binding.replace(binding);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl<'a, 'ctx> LogicalAssignmentOperators<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for LogicalAssignmentOperators<'a, 'ctx> {
impl<'a> Traverse<'a> for LogicalAssignmentOperators<'a, '_> {
// `#[inline]` because this is a hot path, and most `Expression`s are not `AssignmentExpression`s
// with a logical operator. So we want to bail out as fast as possible for everything else,
// without the cost of a function call.
Expand All @@ -86,7 +86,7 @@ impl<'a, 'ctx> Traverse<'a> for LogicalAssignmentOperators<'a, 'ctx> {
}
}

impl<'a, 'ctx> LogicalAssignmentOperators<'a, 'ctx> {
impl<'a> LogicalAssignmentOperators<'a, '_> {
fn transform_logical_assignment(
&mut self,
expr: &mut Expression<'a>,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_transformer/src/es2021/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl<'a, 'ctx> ES2021<'a, 'ctx> {
}
}

impl<'a, 'ctx> Traverse<'a> for ES2021<'a, 'ctx> {
impl<'a> Traverse<'a> for ES2021<'a, '_> {
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if self.options.logical_assignment_operators {
self.logical_assignment_operators.enter_expression(expr, ctx);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use super::{
// Maybe force transform of static blocks if any static properties?
// Or alternatively could insert static property initializers into static blocks.

impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
impl<'a> ClassProperties<'a, '_> {
/// Perform first phase of transformation of class.
///
/// This is the only entry point into the transform upon entering class body.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub(super) struct ClassDetails<'a> {
pub bindings: ClassBindings<'a>,
}

impl<'a> ClassDetails<'a> {
impl ClassDetails<'_> {
/// Create dummy `ClassDetails`.
///
/// Used for dummy entry at top of `ClassesStack`.
Expand Down Expand Up @@ -269,7 +269,7 @@ pub(super) struct ResolvedGetSetPrivateProp<'a, 'b> {
}

// Shortcut methods to get current class
impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
impl<'a> ClassProperties<'a, '_> {
/// Get details of current class.
pub(super) fn current_class(&self) -> &ClassDetails<'a> {
self.classes_stack.last()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use oxc_traverse::TraverseCtx;

use super::{utils::create_assignment, ClassProperties};

impl<'a, 'ctx> ClassProperties<'a, 'ctx> {
impl<'a> ClassProperties<'a, '_> {
/// Substitute temp var for method computed key.
/// `class C { [x()]() {} }` -> `let _x; _x = x(); class C { [_x]() {} }`
/// This transform is only required if class has properties or a static block.
Expand Down
Loading

0 comments on commit 109b8fc

Please sign in to comment.