Skip to content

Commit

Permalink
refactor(isolated_declarations, linter, minifier, prettier, semantic,…
Browse files Browse the repository at this point in the history
… transformer): remove unnecessary `ref` / `ref mut` syntax (#8643)

While working on #8641, I found a lot of places where we unnecessarily use `ref` / `ref mut` in match arms.

In many cases, we're creating double-references (turning a `&T` into a `&&T`). The compiler should be smart enough to remove them for us, but there doesn't seem much point in explicitly creating double-references when we don't actually want them, and relying on compiler to optimize them out again.
  • Loading branch information
overlookmotel committed Jan 21, 2025
1 parent 54d0fac commit e66da9f
Show file tree
Hide file tree
Showing 29 changed files with 63 additions and 67 deletions.
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl<'a> IsolatedDeclarations<'a> {
for element in &decl.body.body {
match element {
ClassElement::StaticBlock(_) => {}
ClassElement::MethodDefinition(ref method) => {
ClassElement::MethodDefinition(method) => {
if self.has_internal_annotation(method.span) {
continue;
}
Expand Down
16 changes: 7 additions & 9 deletions crates/oxc_isolated_declarations/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ impl<'a> IsolatedDeclarations<'a> {
// 2. Transform export declarations
// 3. Collect all bindings / reference from module declarations
// 4. Collect transformed indexes
for stmt in &stmts {
for &stmt in &stmts {
match stmt {
match_declaration!(Statement) => {
if let Statement::TSModuleDeclaration(decl) = stmt {
Expand Down Expand Up @@ -433,8 +433,8 @@ impl<'a> IsolatedDeclarations<'a> {
let mut last_function_name: Option<Atom<'a>> = None;
let mut is_export_default_function_overloads = false;

stmts.retain(move |stmt| match stmt {
Statement::FunctionDeclaration(ref func) => {
stmts.retain(move |&stmt| match stmt {
Statement::FunctionDeclaration(func) => {
let name = func
.id
.as_ref()
Expand All @@ -454,8 +454,8 @@ impl<'a> IsolatedDeclarations<'a> {
}
true
}
Statement::ExportNamedDeclaration(ref decl) => {
if let Some(Declaration::FunctionDeclaration(ref func)) = decl.declaration {
Statement::ExportNamedDeclaration(decl) => {
if let Some(Declaration::FunctionDeclaration(func)) = &decl.declaration {
let name = func
.id
.as_ref()
Expand All @@ -477,10 +477,8 @@ impl<'a> IsolatedDeclarations<'a> {
true
}
}
Statement::ExportDefaultDeclaration(ref decl) => {
if let ExportDefaultDeclarationKind::FunctionDeclaration(ref func) =
decl.declaration
{
Statement::ExportDefaultDeclaration(decl) => {
if let ExportDefaultDeclarationKind::FunctionDeclaration(func) = &decl.declaration {
if is_export_default_function_overloads && func.body.is_some() {
is_export_default_function_overloads = false;
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl IsolatedLintHandler {
// a diagnostics connected from related_info to original diagnostic
let mut inverted_diagnostics = vec![];
for d in &diagnostics {
let Some(ref related_info) = d.diagnostic.related_information else {
let Some(related_info) = &d.diagnostic.related_information else {
continue;
};
let related_information = Some(vec![DiagnosticRelatedInformation {
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/fixer/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ impl<'a> CompositeFix<'a> {
let mut output = String::new();

for fix in fixes {
let Fix { ref content, span } = fix;
let Fix { content, span } = fix;
// negative range or overlapping ranges is invalid
if span.start > span.end {
debug_assert!(false, "Negative range is invalid: {span:?}");
Expand All @@ -513,7 +513,7 @@ impl<'a> CompositeFix<'a> {

output.reserve(before.len() + content.len());
output.push_str(before);
output.push_str(content);
output.push_str(&content);
last_pos = span.end;
}

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/getter_return.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl GetterReturn {
// If the signature of function supports the return of the `undefined` value,
// you do not need to check this rule
if let AstKind::Function(func) = node.kind() {
if let Some(ref ret) = func.return_type {
if let Some(ret) = &func.return_type {
if ret.type_annotation.is_maybe_undefined() {
break 'returns true;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/no_regex_spaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<'a> Visit<'a> for ConsecutiveSpaceFinder {
if ch.value != u32::from(b' ') {
return;
}
if let Some(ref mut space_span) = self.last_space_span {
if let Some(space_span) = &mut self.last_space_span {
// If this is consecutive with the last space, extend it
if space_span.end == ch.span.start {
space_span.end = ch.span.end;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ where
IgnorePattern::Default => {
name.strip_prefix('_').map_or(".".into(), |name| format!(" to '{name}'."))
}
IgnorePattern::Some(ref r) => {
IgnorePattern::Some(r) => {
format!(" to match the pattern /{r}/.")
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ impl<R> IgnorePattern<R> {
match self {
Self::Default => IgnorePattern::Default,
Self::None => IgnorePattern::None,
Self::Some(ref r) => IgnorePattern::Some(r),
Self::Some(r) => IgnorePattern::Some(r),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/eslint/sort_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl SortImports {
let specifiers: Vec<_> = specifiers
.iter()
.filter_map(|specifier| {
if let ImportDeclarationSpecifier::ImportSpecifier(ref specifier) = specifier {
if let ImportDeclarationSpecifier::ImportSpecifier(specifier) = specifier {
Some(specifier)
} else {
None
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/import/first.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ impl Rule for First {

for statement in &program.body {
match statement {
Statement::TSImportEqualsDeclaration(decl) => match decl.module_reference {
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
Statement::TSImportEqualsDeclaration(decl) => match &decl.module_reference {
TSModuleReference::ExternalModuleReference(mod_ref) => {
if matches!(self.absolute_first, AbsoluteFirst::AbsoluteFirst) {
if is_relative_path(mod_ref.expression.value.as_str()) {
any_relative = true;
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/import/no_amd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Rule for NoAmd {
return;
}
if let AstKind::CallExpression(call_expr) = node.kind() {
if let Expression::Identifier(ref identifier) = &call_expr.callee {
if let Expression::Identifier(identifier) = &call_expr.callee {
if identifier.name != "define" && identifier.name != "require" {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
diagnostic(ctx, fn_expr.span, Message::UnexpectedDescribeArgument);
}

let Some(ref body) = fn_expr.body else {
let Some(body) = &fn_expr.body else {
return;
};
if let Some(span) = find_first_return_stmt_span(body) {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/jsdoc/require_returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl Rule for RequireReturns {
match parent_node.kind() {
AstKind::Function(_) | AstKind::ArrowFunctionExpression(_) => {
// Ignore `return;`
let Some(ref argument) = return_stmt.argument else {
let Some(argument) = &return_stmt.argument else {
continue 'visit_node;
};

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/nextjs/no_typos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Rule for NoTypos {
if let AstKind::ModuleDeclaration(ModuleDeclaration::ExportNamedDeclaration(en_decl)) =
node.kind()
{
if let Some(ref decl) = en_decl.declaration {
if let Some(decl) = &en_decl.declaration {
match decl {
Declaration::VariableDeclaration(decl) => {
for decl in &decl.declarations {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl Rule for NoAccumulatingSpread {
let AstKind::SpreadElement(spread) = node.kind() else {
return;
};
let Expression::Identifier(ref ident) = spread.argument else {
let Expression::Identifier(ident) = &spread.argument else {
return;
};

Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/react/jsx_no_undef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fn get_resolvable_ident<'a>(node: &'a JSXElementName<'a>) -> Option<&'a Identifi
JSXElementName::Identifier(_)
| JSXElementName::NamespacedName(_)
| JSXElementName::ThisExpression(_) => None,
JSXElementName::IdentifierReference(ref ident) => Some(ident),
JSXElementName::IdentifierReference(ident) => Some(ident),
JSXElementName::MemberExpression(expr) => get_member_ident(expr),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/typescript/ban_ts_comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ impl Rule for BanTsComment {
));
}

if let DirectiveConfig::DescriptionFormat(Some(ref re)) = config {
if let DirectiveConfig::DescriptionFormat(Some(re)) = config {
if !re.is_match(description) {
ctx.diagnostic(comment_description_not_match_pattern(
directive,
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/typescript/no_require_imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ impl Rule for NoRequireImports {

ctx.diagnostic(no_require_imports_diagnostic(call_expr.span));
}
AstKind::TSImportEqualsDeclaration(decl) => match decl.module_reference {
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
AstKind::TSImportEqualsDeclaration(decl) => match &decl.module_reference {
TSModuleReference::ExternalModuleReference(mod_ref) => {
if self.allow_as_import {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ impl Rule for TripleSlashReference {
if !refs_for_import.is_empty() {
for stmt in &program.body {
match stmt {
Statement::TSImportEqualsDeclaration(decl) => match decl.module_reference {
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
Statement::TSImportEqualsDeclaration(decl) => match &decl.module_reference {
TSModuleReference::ExternalModuleReference(mod_ref) => {
if let Some(v) = refs_for_import.get(mod_ref.expression.value.as_str())
{
ctx.diagnostic(triple_slash_reference_diagnostic(
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/unicorn/no_thenable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl Rule for NoThenable {
}
AstKind::ModuleDeclaration(ModuleDeclaration::ExportNamedDeclaration(decl)) => {
// check declaration
if let Some(ref decl) = decl.declaration {
if let Some(decl) = &decl.declaration {
match decl {
Declaration::VariableDeclaration(decl) => {
for decl in &decl.declarations {
Expand Down Expand Up @@ -242,8 +242,8 @@ fn check_expression(expr: &Expression, ctx: &LintContext<'_>) -> Option<oxc_span
let decl = ctx.semantic().nodes().get_node(symbols.get_declaration(symbol_id));
let var_decl = decl.kind().as_variable_declarator()?;

match var_decl.init {
Some(Expression::StringLiteral(ref lit)) => {
match &var_decl.init {
Some(Expression::StringLiteral(lit)) => {
if lit.value == "then" {
Some(lit.span)
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_linter/src/rules/unicorn/prefer_array_flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ fn check_array_reduce_case<'a>(call_expr: &CallExpression<'a>, ctx: &LintContext
&first_argument.params.items[1].pattern.kind,
) {
(
BindingPatternKind::BindingIdentifier(ref first_param),
BindingPatternKind::BindingIdentifier(first_param),
BindingPatternKind::BindingIdentifier(second_param),
) => Some((&first_param.name, &second_param.name)),

Expand Down
8 changes: 3 additions & 5 deletions crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ impl<'a> PeepholeOptimizations {
if sequence_expr.expressions.len() > 1 {
let span = expr.span();
let mut sequence = ctx.ast.move_expression(&mut expr.test);
let Expression::SequenceExpression(ref mut sequence_expr) = &mut sequence else {
let Expression::SequenceExpression(sequence_expr) = &mut sequence else {
unreachable!()
};
let test = sequence_expr.expressions.pop().unwrap();
Expand Down Expand Up @@ -587,15 +587,13 @@ impl<'a> PeepholeOptimizations {
{
let callee = ctx.ast.move_expression(&mut consequent.callee);
let consequent_first_arg = {
let Argument::SpreadElement(ref mut el) = &mut consequent.arguments[0]
else {
let Argument::SpreadElement(el) = &mut consequent.arguments[0] else {
unreachable!()
};
ctx.ast.move_expression(&mut el.argument)
};
let alternate_first_arg = {
let Argument::SpreadElement(ref mut el) = &mut alternate.arguments[0]
else {
let Argument::SpreadElement(el) = &mut alternate.arguments[0] else {
unreachable!()
};
ctx.ast.move_expression(&mut el.argument)
Expand Down
10 changes: 5 additions & 5 deletions crates/oxc_prettier/src/format/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1558,11 +1558,11 @@ impl<'a> Format<'a> for PrivateIdentifier<'a> {
impl<'a> Format<'a> for BindingPattern<'a> {
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
let mut parts = Vec::new_in(p.allocator);
parts.push(match self.kind {
BindingPatternKind::BindingIdentifier(ref ident) => ident.format(p),
BindingPatternKind::ObjectPattern(ref pattern) => pattern.format(p),
BindingPatternKind::ArrayPattern(ref pattern) => pattern.format(p),
BindingPatternKind::AssignmentPattern(ref pattern) => pattern.format(p),
parts.push(match &self.kind {
BindingPatternKind::BindingIdentifier(ident) => ident.format(p),
BindingPatternKind::ObjectPattern(pattern) => pattern.format(p),
BindingPatternKind::ArrayPattern(pattern) => pattern.format(p),
BindingPatternKind::AssignmentPattern(pattern) => pattern.format(p),
});

if self.optional {
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_semantic/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use crate::{

macro_rules! control_flow {
($self:ident, |$cfg:tt| $body:expr) => {
if let Some(ref mut $cfg) = $self.cfg {
if let Some($cfg) = &mut $self.cfg {
$body
} else {
Default::default()
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_semantic/src/dot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ impl DebugDot for BasicBlock {

impl DebugDot for Instruction {
fn debug_dot(&self, ctx: DebugDotContext) -> String {
match self.kind {
match &self.kind {
InstructionKind::Statement => {
self.node_id.map_or("None".to_string(), |id| ctx.debug_ast_kind(id))
}
Expand All @@ -136,7 +136,7 @@ impl DebugDot for Instruction {
ctx.try_eval_literal(id).unwrap_or_else(|| ctx.debug_ast_kind(id))
)
}),
InstructionKind::Iteration(ref kind) => {
InstructionKind::Iteration(kind) => {
format!(
"Iteration({} {} {})",
self.node_id.map_or("None".to_string(), |id| ctx.debug_ast_kind(id)),
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_transformer/src/jsx/refresh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ impl<'a> Traverse<'a> for ReactRefresh<'a, '_> {

let hook_name = match &call_expr.callee {
Expression::Identifier(ident) => ident.name,
Expression::StaticMemberExpression(ref member) => member.property.name,
Expression::StaticMemberExpression(member) => member.property.name,
_ => return,
};

Expand Down Expand Up @@ -434,7 +434,7 @@ impl<'a> ReactRefresh<'a, '_> {
ctx: &mut TraverseCtx<'a>,
) -> bool {
match expr {
Expression::Identifier(ref ident) => {
Expression::Identifier(ident) => {
// For case like:
// export const Something = hoc(Foo)
// we don't want to wrap Foo inside the call.
Expand All @@ -451,7 +451,7 @@ impl<'a> ReactRefresh<'a, '_> {
return false;
}
}
Expression::CallExpression(ref mut call_expr) => {
Expression::CallExpression(call_expr) => {
let allowed_callee = matches!(
call_expr.callee,
Expression::Identifier(_)
Expand Down Expand Up @@ -660,7 +660,7 @@ impl<'a> ReactRefresh<'a, '_> {
None
}
}
Statement::ExportDefaultDeclaration(ref mut stmt_decl) => {
Statement::ExportDefaultDeclaration(stmt_decl) => {
match &mut stmt_decl.declaration {
declaration @ match_expression!(ExportDefaultDeclarationKind) => {
let expression = declaration.to_expression_mut();
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_transformer/src/options/browserslist_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ impl BrowserslistQuery {
};

let result = match self {
BrowserslistQuery::Single(ref s) => {
BrowserslistQuery::Single(s) => {
if s.is_empty() {
browserslist::resolve(&["defaults"], &options)
} else {
browserslist::resolve(&[s], &options)
}
}
BrowserslistQuery::Multiple(ref s) => browserslist::resolve(s, &options),
BrowserslistQuery::Multiple(s) => browserslist::resolve(s, &options),
};

let result = match result {
Expand Down
Loading

0 comments on commit e66da9f

Please sign in to comment.