Skip to content

Commit e66da9f

Browse files
committed
refactor(isolated_declarations, linter, minifier, prettier, semantic, 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.
1 parent 54d0fac commit e66da9f

File tree

29 files changed

+63
-67
lines changed

29 files changed

+63
-67
lines changed

crates/oxc_isolated_declarations/src/class.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ impl<'a> IsolatedDeclarations<'a> {
368368
for element in &decl.body.body {
369369
match element {
370370
ClassElement::StaticBlock(_) => {}
371-
ClassElement::MethodDefinition(ref method) => {
371+
ClassElement::MethodDefinition(method) => {
372372
if self.has_internal_annotation(method.span) {
373373
continue;
374374
}

crates/oxc_isolated_declarations/src/lib.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl<'a> IsolatedDeclarations<'a> {
195195
// 2. Transform export declarations
196196
// 3. Collect all bindings / reference from module declarations
197197
// 4. Collect transformed indexes
198-
for stmt in &stmts {
198+
for &stmt in &stmts {
199199
match stmt {
200200
match_declaration!(Statement) => {
201201
if let Statement::TSModuleDeclaration(decl) = stmt {
@@ -433,8 +433,8 @@ impl<'a> IsolatedDeclarations<'a> {
433433
let mut last_function_name: Option<Atom<'a>> = None;
434434
let mut is_export_default_function_overloads = false;
435435

436-
stmts.retain(move |stmt| match stmt {
437-
Statement::FunctionDeclaration(ref func) => {
436+
stmts.retain(move |&stmt| match stmt {
437+
Statement::FunctionDeclaration(func) => {
438438
let name = func
439439
.id
440440
.as_ref()
@@ -454,8 +454,8 @@ impl<'a> IsolatedDeclarations<'a> {
454454
}
455455
true
456456
}
457-
Statement::ExportNamedDeclaration(ref decl) => {
458-
if let Some(Declaration::FunctionDeclaration(ref func)) = decl.declaration {
457+
Statement::ExportNamedDeclaration(decl) => {
458+
if let Some(Declaration::FunctionDeclaration(func)) = &decl.declaration {
459459
let name = func
460460
.id
461461
.as_ref()
@@ -477,10 +477,8 @@ impl<'a> IsolatedDeclarations<'a> {
477477
true
478478
}
479479
}
480-
Statement::ExportDefaultDeclaration(ref decl) => {
481-
if let ExportDefaultDeclarationKind::FunctionDeclaration(ref func) =
482-
decl.declaration
483-
{
480+
Statement::ExportDefaultDeclaration(decl) => {
481+
if let ExportDefaultDeclarationKind::FunctionDeclaration(func) = &decl.declaration {
484482
if is_export_default_function_overloads && func.body.is_some() {
485483
is_export_default_function_overloads = false;
486484
return false;

crates/oxc_language_server/src/linter/isolated_lint_handler.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl IsolatedLintHandler {
5151
// a diagnostics connected from related_info to original diagnostic
5252
let mut inverted_diagnostics = vec![];
5353
for d in &diagnostics {
54-
let Some(ref related_info) = d.diagnostic.related_information else {
54+
let Some(related_info) = &d.diagnostic.related_information else {
5555
continue;
5656
};
5757
let related_information = Some(vec![DiagnosticRelatedInformation {

crates/oxc_linter/src/fixer/fix.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ impl<'a> CompositeFix<'a> {
491491
let mut output = String::new();
492492

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

514514
output.reserve(before.len() + content.len());
515515
output.push_str(before);
516-
output.push_str(content);
516+
output.push_str(&content);
517517
last_pos = span.end;
518518
}
519519

crates/oxc_linter/src/rules/eslint/getter_return.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl GetterReturn {
227227
// If the signature of function supports the return of the `undefined` value,
228228
// you do not need to check this rule
229229
if let AstKind::Function(func) = node.kind() {
230-
if let Some(ref ret) = func.return_type {
230+
if let Some(ret) = &func.return_type {
231231
if ret.type_annotation.is_maybe_undefined() {
232232
break 'returns true;
233233
}

crates/oxc_linter/src/rules/eslint/no_regex_spaces.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ impl<'a> Visit<'a> for ConsecutiveSpaceFinder {
158158
if ch.value != u32::from(b' ') {
159159
return;
160160
}
161-
if let Some(ref mut space_span) = self.last_space_span {
161+
if let Some(space_span) = &mut self.last_space_span {
162162
// If this is consecutive with the last space, extend it
163163
if space_span.end == ch.span.start {
164164
space_span.end = ch.span.end;

crates/oxc_linter/src/rules/eslint/no_unused_vars/diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ where
4545
IgnorePattern::Default => {
4646
name.strip_prefix('_').map_or(".".into(), |name| format!(" to '{name}'."))
4747
}
48-
IgnorePattern::Some(ref r) => {
48+
IgnorePattern::Some(r) => {
4949
format!(" to match the pattern /{r}/.")
5050
}
5151
};

crates/oxc_linter/src/rules/eslint/no_unused_vars/options.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl<R> IgnorePattern<R> {
267267
match self {
268268
Self::Default => IgnorePattern::Default,
269269
Self::None => IgnorePattern::None,
270-
Self::Some(ref r) => IgnorePattern::Some(r),
270+
Self::Some(r) => IgnorePattern::Some(r),
271271
}
272272
}
273273
}

crates/oxc_linter/src/rules/eslint/sort_imports.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl SortImports {
266266
let specifiers: Vec<_> = specifiers
267267
.iter()
268268
.filter_map(|specifier| {
269-
if let ImportDeclarationSpecifier::ImportSpecifier(ref specifier) = specifier {
269+
if let ImportDeclarationSpecifier::ImportSpecifier(specifier) = specifier {
270270
Some(specifier)
271271
} else {
272272
None

crates/oxc_linter/src/rules/import/first.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ impl Rule for First {
121121

122122
for statement in &program.body {
123123
match statement {
124-
Statement::TSImportEqualsDeclaration(decl) => match decl.module_reference {
125-
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
124+
Statement::TSImportEqualsDeclaration(decl) => match &decl.module_reference {
125+
TSModuleReference::ExternalModuleReference(mod_ref) => {
126126
if matches!(self.absolute_first, AbsoluteFirst::AbsoluteFirst) {
127127
if is_relative_path(mod_ref.expression.value.as_str()) {
128128
any_relative = true;

crates/oxc_linter/src/rules/import/no_amd.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl Rule for NoAmd {
5656
return;
5757
}
5858
if let AstKind::CallExpression(call_expr) = node.kind() {
59-
if let Expression::Identifier(ref identifier) = &call_expr.callee {
59+
if let Expression::Identifier(identifier) = &call_expr.callee {
6060
if identifier.name != "define" && identifier.name != "require" {
6161
return;
6262
}

crates/oxc_linter/src/rules/jest/valid_describe_callback.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ fn run<'a>(possible_jest_node: &PossibleJestNode<'a, '_>, ctx: &LintContext<'a>)
131131
diagnostic(ctx, fn_expr.span, Message::UnexpectedDescribeArgument);
132132
}
133133

134-
let Some(ref body) = fn_expr.body else {
134+
let Some(body) = &fn_expr.body else {
135135
return;
136136
};
137137
if let Some(span) = find_first_return_stmt_span(body) {

crates/oxc_linter/src/rules/jsdoc/require_returns.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl Rule for RequireReturns {
149149
match parent_node.kind() {
150150
AstKind::Function(_) | AstKind::ArrowFunctionExpression(_) => {
151151
// Ignore `return;`
152-
let Some(ref argument) = return_stmt.argument else {
152+
let Some(argument) = &return_stmt.argument else {
153153
continue 'visit_node;
154154
};
155155

crates/oxc_linter/src/rules/nextjs/no_typos.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ impl Rule for NoTypos {
6969
if let AstKind::ModuleDeclaration(ModuleDeclaration::ExportNamedDeclaration(en_decl)) =
7070
node.kind()
7171
{
72-
if let Some(ref decl) = en_decl.declaration {
72+
if let Some(decl) = &en_decl.declaration {
7373
match decl {
7474
Declaration::VariableDeclaration(decl) => {
7575
for decl in &decl.declarations {

crates/oxc_linter/src/rules/oxc/no_accumulating_spread.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl Rule for NoAccumulatingSpread {
129129
let AstKind::SpreadElement(spread) = node.kind() else {
130130
return;
131131
};
132-
let Expression::Identifier(ref ident) = spread.argument else {
132+
let Expression::Identifier(ident) = &spread.argument else {
133133
return;
134134
};
135135

crates/oxc_linter/src/rules/react/jsx_no_undef.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn get_resolvable_ident<'a>(node: &'a JSXElementName<'a>) -> Option<&'a Identifi
4141
JSXElementName::Identifier(_)
4242
| JSXElementName::NamespacedName(_)
4343
| JSXElementName::ThisExpression(_) => None,
44-
JSXElementName::IdentifierReference(ref ident) => Some(ident),
44+
JSXElementName::IdentifierReference(ident) => Some(ident),
4545
JSXElementName::MemberExpression(expr) => get_member_ident(expr),
4646
}
4747
}

crates/oxc_linter/src/rules/typescript/ban_ts_comment.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ impl Rule for BanTsComment {
202202
));
203203
}
204204

205-
if let DirectiveConfig::DescriptionFormat(Some(ref re)) = config {
205+
if let DirectiveConfig::DescriptionFormat(Some(re)) = config {
206206
if !re.is_match(description) {
207207
ctx.diagnostic(comment_description_not_match_pattern(
208208
directive,

crates/oxc_linter/src/rules/typescript/no_require_imports.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ impl Rule for NoRequireImports {
193193

194194
ctx.diagnostic(no_require_imports_diagnostic(call_expr.span));
195195
}
196-
AstKind::TSImportEqualsDeclaration(decl) => match decl.module_reference {
197-
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
196+
AstKind::TSImportEqualsDeclaration(decl) => match &decl.module_reference {
197+
TSModuleReference::ExternalModuleReference(mod_ref) => {
198198
if self.allow_as_import {
199199
return;
200200
}

crates/oxc_linter/src/rules/typescript/triple_slash_reference.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ impl Rule for TripleSlashReference {
134134
if !refs_for_import.is_empty() {
135135
for stmt in &program.body {
136136
match stmt {
137-
Statement::TSImportEqualsDeclaration(decl) => match decl.module_reference {
138-
TSModuleReference::ExternalModuleReference(ref mod_ref) => {
137+
Statement::TSImportEqualsDeclaration(decl) => match &decl.module_reference {
138+
TSModuleReference::ExternalModuleReference(mod_ref) => {
139139
if let Some(v) = refs_for_import.get(mod_ref.expression.value.as_str())
140140
{
141141
ctx.diagnostic(triple_slash_reference_diagnostic(

crates/oxc_linter/src/rules/unicorn/no_thenable.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl Rule for NoThenable {
8484
}
8585
AstKind::ModuleDeclaration(ModuleDeclaration::ExportNamedDeclaration(decl)) => {
8686
// check declaration
87-
if let Some(ref decl) = decl.declaration {
87+
if let Some(decl) = &decl.declaration {
8888
match decl {
8989
Declaration::VariableDeclaration(decl) => {
9090
for decl in &decl.declarations {
@@ -242,8 +242,8 @@ fn check_expression(expr: &Expression, ctx: &LintContext<'_>) -> Option<oxc_span
242242
let decl = ctx.semantic().nodes().get_node(symbols.get_declaration(symbol_id));
243243
let var_decl = decl.kind().as_variable_declarator()?;
244244

245-
match var_decl.init {
246-
Some(Expression::StringLiteral(ref lit)) => {
245+
match &var_decl.init {
246+
Some(Expression::StringLiteral(lit)) => {
247247
if lit.value == "then" {
248248
Some(lit.span)
249249
} else {

crates/oxc_linter/src/rules/unicorn/prefer_array_flat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ fn check_array_reduce_case<'a>(call_expr: &CallExpression<'a>, ctx: &LintContext
145145
&first_argument.params.items[1].pattern.kind,
146146
) {
147147
(
148-
BindingPatternKind::BindingIdentifier(ref first_param),
148+
BindingPatternKind::BindingIdentifier(first_param),
149149
BindingPatternKind::BindingIdentifier(second_param),
150150
) => Some((&first_param.name, &second_param.name)),
151151

crates/oxc_minifier/src/peephole/minimize_conditions.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ impl<'a> PeepholeOptimizations {
339339
if sequence_expr.expressions.len() > 1 {
340340
let span = expr.span();
341341
let mut sequence = ctx.ast.move_expression(&mut expr.test);
342-
let Expression::SequenceExpression(ref mut sequence_expr) = &mut sequence else {
342+
let Expression::SequenceExpression(sequence_expr) = &mut sequence else {
343343
unreachable!()
344344
};
345345
let test = sequence_expr.expressions.pop().unwrap();
@@ -587,15 +587,13 @@ impl<'a> PeepholeOptimizations {
587587
{
588588
let callee = ctx.ast.move_expression(&mut consequent.callee);
589589
let consequent_first_arg = {
590-
let Argument::SpreadElement(ref mut el) = &mut consequent.arguments[0]
591-
else {
590+
let Argument::SpreadElement(el) = &mut consequent.arguments[0] else {
592591
unreachable!()
593592
};
594593
ctx.ast.move_expression(&mut el.argument)
595594
};
596595
let alternate_first_arg = {
597-
let Argument::SpreadElement(ref mut el) = &mut alternate.arguments[0]
598-
else {
596+
let Argument::SpreadElement(el) = &mut alternate.arguments[0] else {
599597
unreachable!()
600598
};
601599
ctx.ast.move_expression(&mut el.argument)

crates/oxc_prettier/src/format/js.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1558,11 +1558,11 @@ impl<'a> Format<'a> for PrivateIdentifier<'a> {
15581558
impl<'a> Format<'a> for BindingPattern<'a> {
15591559
fn format(&self, p: &mut Prettier<'a>) -> Doc<'a> {
15601560
let mut parts = Vec::new_in(p.allocator);
1561-
parts.push(match self.kind {
1562-
BindingPatternKind::BindingIdentifier(ref ident) => ident.format(p),
1563-
BindingPatternKind::ObjectPattern(ref pattern) => pattern.format(p),
1564-
BindingPatternKind::ArrayPattern(ref pattern) => pattern.format(p),
1565-
BindingPatternKind::AssignmentPattern(ref pattern) => pattern.format(p),
1561+
parts.push(match &self.kind {
1562+
BindingPatternKind::BindingIdentifier(ident) => ident.format(p),
1563+
BindingPatternKind::ObjectPattern(pattern) => pattern.format(p),
1564+
BindingPatternKind::ArrayPattern(pattern) => pattern.format(p),
1565+
BindingPatternKind::AssignmentPattern(pattern) => pattern.format(p),
15661566
});
15671567

15681568
if self.optional {

crates/oxc_semantic/src/builder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::{
3939

4040
macro_rules! control_flow {
4141
($self:ident, |$cfg:tt| $body:expr) => {
42-
if let Some(ref mut $cfg) = $self.cfg {
42+
if let Some($cfg) = &mut $self.cfg {
4343
$body
4444
} else {
4545
Default::default()

crates/oxc_semantic/src/dot.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl DebugDot for BasicBlock {
124124

125125
impl DebugDot for Instruction {
126126
fn debug_dot(&self, ctx: DebugDotContext) -> String {
127-
match self.kind {
127+
match &self.kind {
128128
InstructionKind::Statement => {
129129
self.node_id.map_or("None".to_string(), |id| ctx.debug_ast_kind(id))
130130
}
@@ -136,7 +136,7 @@ impl DebugDot for Instruction {
136136
ctx.try_eval_literal(id).unwrap_or_else(|| ctx.debug_ast_kind(id))
137137
)
138138
}),
139-
InstructionKind::Iteration(ref kind) => {
139+
InstructionKind::Iteration(kind) => {
140140
format!(
141141
"Iteration({} {} {})",
142142
self.node_id.map_or("None".to_string(), |id| ctx.debug_ast_kind(id)),

crates/oxc_transformer/src/jsx/refresh.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ impl<'a> Traverse<'a> for ReactRefresh<'a, '_> {
313313

314314
let hook_name = match &call_expr.callee {
315315
Expression::Identifier(ident) => ident.name,
316-
Expression::StaticMemberExpression(ref member) => member.property.name,
316+
Expression::StaticMemberExpression(member) => member.property.name,
317317
_ => return,
318318
};
319319

@@ -434,7 +434,7 @@ impl<'a> ReactRefresh<'a, '_> {
434434
ctx: &mut TraverseCtx<'a>,
435435
) -> bool {
436436
match expr {
437-
Expression::Identifier(ref ident) => {
437+
Expression::Identifier(ident) => {
438438
// For case like:
439439
// export const Something = hoc(Foo)
440440
// we don't want to wrap Foo inside the call.
@@ -451,7 +451,7 @@ impl<'a> ReactRefresh<'a, '_> {
451451
return false;
452452
}
453453
}
454-
Expression::CallExpression(ref mut call_expr) => {
454+
Expression::CallExpression(call_expr) => {
455455
let allowed_callee = matches!(
456456
call_expr.callee,
457457
Expression::Identifier(_)
@@ -660,7 +660,7 @@ impl<'a> ReactRefresh<'a, '_> {
660660
None
661661
}
662662
}
663-
Statement::ExportDefaultDeclaration(ref mut stmt_decl) => {
663+
Statement::ExportDefaultDeclaration(stmt_decl) => {
664664
match &mut stmt_decl.declaration {
665665
declaration @ match_expression!(ExportDefaultDeclarationKind) => {
666666
let expression = declaration.to_expression_mut();

crates/oxc_transformer/src/options/browserslist_query.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ impl BrowserslistQuery {
3333
};
3434

3535
let result = match self {
36-
BrowserslistQuery::Single(ref s) => {
36+
BrowserslistQuery::Single(s) => {
3737
if s.is_empty() {
3838
browserslist::resolve(&["defaults"], &options)
3939
} else {
4040
browserslist::resolve(&[s], &options)
4141
}
4242
}
43-
BrowserslistQuery::Multiple(ref s) => browserslist::resolve(s, &options),
43+
BrowserslistQuery::Multiple(s) => browserslist::resolve(s, &options),
4444
};
4545

4646
let result = match result {

0 commit comments

Comments
 (0)