Skip to content

Commit ef05957

Browse files
committed
refactor(formatter): remove all unused code (#15941)
This PR aims to remove the global `#![allow(unused)]`. There are a few main things that were done: 1. Removed all unused imports 2. Renamed the unused variable to add a `_` prefix 3. Removed unused statements 4. Marked `#[expect(unused)]` for some structs that are not sure if they would be used in the future
1 parent 28f112d commit ef05957

File tree

87 files changed

+336
-697
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+336
-697
lines changed

crates/oxc_formatter/src/ast_nodes/generated/ast_nodes.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
// Auto-generated code, DO NOT EDIT DIRECTLY!
22
// To edit this generated file you have to edit `tasks/ast_tools/src/generators/formatter/ast_nodes.rs`.
33

4-
use std::{fmt, mem::transmute, ops::Deref};
4+
use std::mem::transmute;
55

6-
use oxc_allocator::{Allocator, Box, Vec};
6+
use oxc_allocator::Vec;
77
use oxc_ast::ast::*;
8-
use oxc_span::{GetSpan, SPAN};
8+
use oxc_span::GetSpan;
99

1010
use crate::ast_nodes::AstNode;
11-
use crate::{
12-
formatter::{
13-
Buffer, Format, FormatResult, Formatter,
14-
trivia::{format_leading_comments, format_trailing_comments},
15-
},
16-
parentheses::NeedsParentheses,
17-
write::FormatWrite,
11+
use crate::formatter::{
12+
Format, FormatResult, Formatter,
13+
trivia::{format_leading_comments, format_trailing_comments},
1814
};
1915

2016
#[inline]
2117
pub(super) fn transmute_self<'a, T>(s: &AstNode<'a, T>) -> &'a AstNode<'a, T> {
22-
/// * SAFETY: `s` is already allocated in Arena, so transmute from `&` to `&'a` is safe.
18+
#[expect(clippy::undocumented_unsafe_blocks)]
2319
unsafe {
2420
transmute(s)
2521
}
@@ -2166,7 +2162,8 @@ impl<'a> AstNode<'a, AssignmentTarget<'a>> {
21662162
#[inline]
21672163
pub fn as_ast_nodes(&self) -> &AstNodes<'a> {
21682164
let parent = self.parent;
2169-
let node = match self.inner {
2165+
#[expect(clippy::needless_return)]
2166+
match self.inner {
21702167
it @ match_simple_assignment_target!(AssignmentTarget) => {
21712168
return self
21722169
.allocator
@@ -2189,8 +2186,7 @@ impl<'a> AstNode<'a, AssignmentTarget<'a>> {
21892186
})
21902187
.as_ast_nodes();
21912188
}
2192-
};
2193-
self.allocator.alloc(node)
2189+
}
21942190
}
21952191
}
21962192

crates/oxc_formatter/src/ast_nodes/generated/format.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55
use oxc_ast::ast::*;
66
use oxc_span::GetSpan;
77

8-
use super::ast_nodes::transmute_self;
98
use crate::{
10-
ast_nodes::{AstNode, AstNodes},
11-
formatter::{Buffer, Format, FormatResult, Formatter, trivia::FormatTrailingComments},
9+
ast_nodes::AstNode,
10+
formatter::{Format, FormatResult, Formatter},
1211
parentheses::NeedsParentheses,
1312
utils::{suppressed::FormatSuppressedNode, typecast::format_type_cast_comment_node},
1413
write::{FormatFunctionOptions, FormatJsArrowFunctionExpressionOptions, FormatWrite},

crates/oxc_formatter/src/ast_nodes/node.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<'a> AstNode<'a, ImportExpression<'a>> {
176176
inner: arguments_ref,
177177
allocator: self.allocator,
178178
parent: self.allocator.alloc(AstNodes::ImportExpression({
179-
/// * SAFETY: `self` is already allocated in Arena, so transmute from `&` to `&'a` is safe.
179+
// SAFETY: `self` is already allocated in Arena, so transmute from `&` to `&'a` is safe.
180180
unsafe {
181181
transmute::<
182182
&AstNode<'_, ImportExpression<'_>>,

crates/oxc_formatter/src/detect_code_removal/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
22

33
use oxc_allocator::Allocator;
44
use oxc_ast::{AstKind, ast};
5-
use oxc_parser::{ParseOptions, Parser, ParserReturn};
5+
use oxc_parser::{Parser, ParserReturn};
66
use oxc_semantic::{AstNode, Semantic, SemanticBuilder};
77
use oxc_span::SourceType;
88

crates/oxc_formatter/src/formatter/buffer.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,13 @@ pub struct PreambleBuffer<'a, 'buf, Preamble> {
331331
}
332332

333333
impl<'ast, 'buf, Preamble> PreambleBuffer<'ast, 'buf, Preamble> {
334+
#[expect(unused)]
334335
pub fn new(inner: &'buf mut dyn Buffer<'ast>, preamble: Preamble) -> Self {
335336
Self { inner, preamble, empty: true }
336337
}
337338

338339
/// Returns `true` if the preamble has been written, `false` otherwise.
340+
#[expect(unused)]
339341
pub fn did_write_preamble(&self) -> bool {
340342
!self.empty
341343
}
@@ -531,8 +533,7 @@ fn clean_interned<'ast>(
531533
FormatElement::Line(LineMode::Soft | LineMode::SoftOrSpace)
532534
| FormatElement::Tag(Tag::StartConditionalContent(_) | Tag::EndConditionalContent)
533535
| FormatElement::BestFitting(_) => {
534-
let mut cleaned =
535-
ArenaVec::from_iter_in(interned[..index].iter().cloned(), allocator);
536+
let cleaned = ArenaVec::from_iter_in(interned[..index].iter().cloned(), allocator);
536537
Some((cleaned, &interned[index..]))
537538
}
538539
FormatElement::Interned(inner) => {
@@ -667,6 +668,7 @@ impl<'ast> Buffer<'ast> for RemoveSoftLinesBuffer<'_, 'ast> {
667668
pub trait BufferExtensions<'ast>: Buffer<'ast> + Sized {
668669
/// Returns a new buffer that calls the passed inspector for every element that gets written to the output
669670
#[must_use]
671+
#[expect(unused)]
670672
fn inspect<'inner, F>(&'inner mut self, inspector: F) -> Inspect<'ast, 'inner, F>
671673
where
672674
F: FnMut(&FormatElement),
@@ -751,6 +753,7 @@ where
751753
}
752754

753755
#[inline(always)]
756+
#[expect(unused)]
754757
pub fn write_element(&mut self, element: FormatElement<'ast>) -> FormatResult<()> {
755758
self.buffer.write_element(element)
756759
}

crates/oxc_formatter/src/formatter/builders.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{backtrace, cell::Cell, num::NonZeroU8};
1+
use std::{cell::Cell, num::NonZeroU8};
22

33
use Tag::{
44
EndAlign, EndConditionalContent, EndDedent, EndEntry, EndFill, EndGroup, EndIndent,
@@ -8,10 +8,9 @@ use Tag::{
88
};
99
use oxc_allocator::Vec as ArenaVec;
1010
use oxc_span::{GetSpan, Span};
11-
use oxc_syntax::identifier::{is_identifier_name, is_line_terminator, is_white_space_single_line};
1211

1312
use super::{
14-
Argument, Arguments, Buffer, Comments, GroupId, TextSize, VecBuffer,
13+
Argument, Arguments, Buffer, GroupId, VecBuffer,
1514
format_element::{
1615
self, TextWidth,
1716
tag::{Condition, Tag},
@@ -336,6 +335,7 @@ impl std::fmt::Debug for Text<'_> {
336335
}
337336

338337
#[track_caller]
338+
#[expect(unused)]
339339
fn debug_assert_no_newlines(text: &str) {
340340
debug_assert!(
341341
!text.contains('\r'),

crates/oxc_formatter/src/formatter/comments.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,19 +97,10 @@
9797
//! ## References
9898
//! - [Prettier handles special comments](https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/language-js/comments/handle-comments.js)
9999
//! - [Prettier pre-processes comments](https://github.com/prettier/prettier/blob/7584432401a47a26943dd7a9ca9a8e032ead7285/src/main/comments/attach.js)
100-
use std::ops::{ControlFlow, Deref};
101-
102-
use oxc_allocator::Vec;
103-
use oxc_ast::{
104-
Comment, CommentContent, CommentKind,
105-
ast::{self, CallExpression, NewExpression},
106-
};
100+
use oxc_ast::{Comment, CommentContent};
107101
use oxc_span::{GetSpan, Span};
108102

109-
use crate::{
110-
Format, FormatResult,
111-
formatter::{Formatter, SourceText},
112-
};
103+
use crate::formatter::SourceText;
113104

114105
#[derive(Debug, Clone)]
115106
pub struct Comments<'a> {
@@ -292,7 +283,7 @@ impl<'a> Comments<'a> {
292283
&self,
293284
enclosing_span: Span,
294285
preceding_span: Span,
295-
mut following_span: Option<Span>,
286+
following_span: Option<Span>,
296287
) -> &'a [Comment] {
297288
let comments = self.unprinted_comments();
298289
if comments.is_empty() {

crates/oxc_formatter/src/formatter/context.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
1-
use std::cell::RefCell;
2-
31
use oxc_allocator::Allocator;
4-
use oxc_ast::{
5-
Comment,
6-
ast::{FunctionBody, Program},
7-
};
2+
use oxc_ast::Comment;
83
use oxc_span::{GetSpan, SourceType, Span};
94
use rustc_hash::FxHashMap;
105

116
use crate::{
12-
ast_nodes::AstNode, embedded_formatter::EmbeddedFormatter, formatter::FormatElement,
13-
options::FormatOptions,
7+
embedded_formatter::EmbeddedFormatter, formatter::FormatElement, options::FormatOptions,
148
};
159

1610
use super::{Comments, SourceText};

crates/oxc_formatter/src/formatter/format_element/document.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,12 @@ use cow_utils::CowUtils;
33
use std::ops::Deref;
44

55
use oxc_allocator::{Allocator, Vec as ArenaVec};
6-
use oxc_ast::Comment;
7-
use oxc_span::SourceType;
86
use rustc_hash::FxHashMap;
97

108
use super::super::prelude::*;
119
use super::tag::Tag;
12-
use crate::formatter::TextSize;
1310
use crate::formatter::prelude::tag::{DedentMode, GroupMode};
14-
use crate::{
15-
Format, FormatOptions, FormatResult, IndentStyle, IndentWidth, LineEnding, LineWidth,
16-
formatter::FormatContext, formatter::Formatter,
17-
};
11+
use crate::{Format, FormatResult, formatter::FormatContext, formatter::Formatter};
1812

1913
use crate::{format, write};
2014

@@ -119,7 +113,7 @@ impl Document<'_> {
119113
false
120114
}
121115
// `FormatElement::Token` cannot contain line breaks
122-
FormatElement::Text { text, width } => width.is_multiline(),
116+
FormatElement::Text { text: _, width } => width.is_multiline(),
123117
FormatElement::ExpandParent
124118
| FormatElement::Line(LineMode::Hard | LineMode::Empty) => true,
125119
_ => false,
@@ -210,7 +204,7 @@ impl<'a> Format<'a> for &[FormatElement<'a>] {
210204
let new_element = match element {
211205
// except for static text because source_position is unknown
212206
FormatElement::Token { .. } => element.clone(),
213-
FormatElement::Text { text, width } => {
207+
FormatElement::Text { text, width: _ } => {
214208
let text = text.cow_replace('"', "\\\"");
215209
FormatElement::Text {
216210
text: f.context().allocator().alloc_str(&text),
@@ -277,7 +271,7 @@ impl<'a> Format<'a> for &[FormatElement<'a>] {
277271
}
278272

279273
FormatElement::Interned(interned) => {
280-
let mut interned_elements = f.state_mut().printed_interned_elements();
274+
let interned_elements = f.state_mut().printed_interned_elements();
281275
match interned_elements.get(interned).copied() {
282276
None => {
283277
let index = interned_elements.len();

crates/oxc_formatter/src/formatter/format_element/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ pub mod tag;
55
// use biome_rowan::static_assert;
66
use std::hash::{Hash, Hasher};
77
use std::ptr;
8-
use std::{borrow::Cow, ops::Deref, rc::Rc};
8+
use std::{borrow::Cow, ops::Deref};
99

1010
use unicode_width::{UnicodeWidthChar, UnicodeWidthStr};
1111

12-
use oxc_allocator::{Address, Box as ArenaBox, Vec as ArenaVec};
12+
use oxc_allocator::Vec as ArenaVec;
1313

14-
use crate::{IndentWidth, TabWidth};
14+
use crate::IndentWidth;
1515

1616
use super::{
17-
TagKind, TextSize,
17+
TagKind,
1818
format_element::tag::{LabelId, Tag},
1919
};
2020

@@ -185,11 +185,15 @@ impl<'a> Deref for Interned<'a> {
185185
}
186186

187187
const LINE_SEPARATOR: char = '\u{2028}';
188+
188189
const PARAGRAPH_SEPARATOR: char = '\u{2029}';
190+
191+
#[expect(unused)]
189192
pub const LINE_TERMINATORS: [char; 3] = ['\r', LINE_SEPARATOR, PARAGRAPH_SEPARATOR];
190193

191194
/// Replace the line terminators matching the provided list with "\n"
192195
/// since its the only line break type supported by the printer
196+
#[expect(unused)]
193197
pub fn normalize_newlines<const N: usize>(text: &str, terminators: [char; N]) -> Cow<'_, str> {
194198
let mut result = String::new();
195199
let mut last_end = 0;
@@ -257,7 +261,7 @@ impl FormatElements for FormatElement<'_> {
257261
FormatElement::ExpandParent => true,
258262
FormatElement::Tag(Tag::StartGroup(group)) => !group.mode().is_flat(),
259263
FormatElement::Line(line_mode) => line_mode.will_break(),
260-
FormatElement::Text { text, width } => width.is_multiline(),
264+
FormatElement::Text { text: _, width } => width.is_multiline(),
261265
FormatElement::Interned(interned) => interned.will_break(),
262266
// Traverse into the most flat version because the content is guaranteed to expand when even
263267
// the most flat version contains some content that forces a break.
@@ -376,10 +380,12 @@ pub trait FormatElements {
376380
/// Returns the start tag of `kind` if:
377381
/// * the last element is an end tag of `kind`.
378382
/// * there's a matching start tag in this document (may not be true if this slice is an interned element and the `start` is in the document storing the interned element).
383+
#[expect(unused)]
379384
fn start_tag(&self, kind: TagKind) -> Option<&Tag>;
380385

381386
/// Returns the end tag if:
382387
/// * the last element is an end tag of `kind`
388+
#[expect(unused)]
383389
fn end_tag(&self, kind: TagKind) -> Option<&Tag>;
384390
}
385391

0 commit comments

Comments
 (0)