Skip to content

Commit

Permalink
fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
ShuiRuTian committed Dec 3, 2024
1 parent 6604759 commit 8c93101
Show file tree
Hide file tree
Showing 14 changed files with 29 additions and 65 deletions.
7 changes: 1 addition & 6 deletions crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,12 +215,7 @@ impl<'a> AstBuilder<'a> {
/// `0`
#[inline]
pub fn number_0(self) -> Expression<'a> {
self.expression_numeric_literal(
Span::default(),
0.0,
None,
NumberBase::Decimal,
)
self.expression_numeric_literal(Span::default(), 0.0, None, NumberBase::Decimal)
}

/// `void 0`
Expand Down
13 changes: 2 additions & 11 deletions crates/oxc_ast/src/ast_impl/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,8 @@ impl NumericLiteral<'_> {
/// Provide a fallback for converting NumericLiteral's value to a string if `raw` is None
pub fn raw_str(&self) -> Cow<str> {
match self.raw.as_ref() {
Some(raw) => return Cow::Borrowed(raw),
None => {
let value = self.value;
let s = match self.base {
NumberBase::Binary => format!("{:#b}", value as i64),
NumberBase::Decimal | NumberBase::Float => format!("{}", value),
NumberBase::Hex => format!("{:#x}", value as i64),
NumberBase::Octal => format!("{:#o}", value as i64),
};
Cow::Owned(s)
}
Some(raw) => Cow::Borrowed(raw),
None => Cow::Owned(format!("{}", self.value)),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ast/src/ast_kind_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ impl AstKind<'_> {
Self::StringLiteral(s) => format!("StringLiteral({})", s.value).into(),
Self::BooleanLiteral(b) => format!("BooleanLiteral({})", b.value).into(),
Self::NullLiteral(_) => "NullLiteral".into(),
Self::BigIntLiteral(b) => format!("BigIntLiteral({:?})", b.raw).into(),
Self::BigIntLiteral(b) => format!("BigIntLiteral({})", b.raw).into(),
Self::RegExpLiteral(r) => format!("RegExpLiteral({})", r.regex).into(),
Self::TemplateLiteral(t) => format!(
"TemplateLiteral({})",
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_ast/src/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'a> From<&'a NumericLiteral<'a>> for ESTreeLiteral<'a, f64> {
Self {
span: value.span,
value: value.value,
raw: value.raw.as_ref().map(|raw| raw.as_str()),
raw: value.raw.as_ref().map(oxc_span::Atom::as_str),
bigint: None,
regex: None,
}
Expand All @@ -64,7 +64,7 @@ impl<'a> From<&'a StringLiteral<'a>> for ESTreeLiteral<'a, &'a str> {
Self {
span: value.span,
value: &value.value,
raw: value.raw.as_ref().map(|raw| raw.as_str()),
raw: value.raw.as_ref().map(oxc_span::Atom::as_str),
bigint: None,
regex: None,
}
Expand All @@ -73,7 +73,7 @@ impl<'a> From<&'a StringLiteral<'a>> for ESTreeLiteral<'a, &'a str> {

impl<'a> From<&'a BigIntLiteral<'a>> for ESTreeLiteral<'a, ()> {
fn from(value: &'a BigIntLiteral) -> Self {
let src = &value.raw.as_str().strip_suffix('n').unwrap().cow_replace('_', "");
let src = &value.raw.strip_suffix('n').unwrap().cow_replace('_', "");

let src = match value.base {
BigintBase::Decimal => src,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl<'a> From<&'a RegExpLiteral<'a>> for ESTreeLiteral<'a, Option<EmptyObject>>
fn from(value: &'a RegExpLiteral) -> Self {
Self {
span: value.span,
raw: value.raw.as_ref().map(|raw| raw.as_str()),
raw: value.raw.as_ref().map(oxc_span::Atom::as_str),
value: match &value.regex.pattern {
RegExpPattern::Pattern(_) => Some(EmptyObject {}),
_ => None,
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_ecmascript/src/to_big_int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl<'a> ToBigInt<'a> for Expression<'a> {
impl<'a> ToBigInt<'a> for BigIntLiteral<'a> {
fn to_big_int(&self) -> Option<BigInt> {
let value = self.raw.as_str().trim_end_matches('n').string_to_big_int();
debug_assert!(value.is_some(), "Failed to parse {:?}", self.raw);
debug_assert!(value.is_some(), "Failed to parse {}", self.raw);
value
}
}
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/enum.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc_hash::FxHashMap;

use oxc_allocator::{CloneIn, IntoIn};
use oxc_allocator::CloneIn;
use oxc_ast::ast::*;
use oxc_ecmascript::ToInt32;
use oxc_span::{Atom, GetSpan, SPAN};
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_linter/src/rules/eslint/func_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,7 @@ fn get_property_key_name<'a>(key: &PropertyKey<'a>) -> Option<Cow<'a, str>> {
PropertyKey::RegExpLiteral(regex) => {
Some(Cow::Owned(format!("/{}/{}", regex.regex.pattern, regex.regex.flags)))
}
PropertyKey::BigIntLiteral(bigint) => {
Some(Cow::Borrowed(bigint.raw.as_str()))
}
PropertyKey::BigIntLiteral(bigint) => Some(Cow::Borrowed(bigint.raw.as_str())),
PropertyKey::TemplateLiteral(template) => {
if template.expressions.len() == 0 && template.quasis.len() == 1 {
if let Some(cooked) = &template.quasis[0].value.cooked {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,7 @@ impl PeepholeReplaceKnownMethods {
};

#[expect(clippy::cast_precision_loss)]
Some(ctx.ast.expression_numeric_literal(
span,
result as f64,
None,
NumberBase::Decimal,
))
Some(ctx.ast.expression_numeric_literal(span, result as f64, None, NumberBase::Decimal))
}

fn try_fold_string_substring_or_slice<'a>(
Expand Down Expand Up @@ -219,12 +214,7 @@ impl PeepholeReplaceKnownMethods {
let result = string_lit.value.as_str().char_code_at(Some(char_at_index))?;

#[expect(clippy::cast_lossless)]
Some(ctx.ast.expression_numeric_literal(
span,
result as f64,
None,
NumberBase::Decimal,
))
Some(ctx.ast.expression_numeric_literal(span, result as f64, None, NumberBase::Decimal))
}
fn try_fold_string_replace_or_string_replace_all<'a>(
span: Span,
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_minifier/src/node_util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::ops::Deref;

use oxc_allocator::IntoIn;
use oxc_ast::ast::*;
use oxc_ecmascript::constant_evaluation::{ConstantEvaluation, ConstantValue};
use oxc_semantic::{IsGlobalReference, SymbolTable};
Expand Down Expand Up @@ -39,8 +38,7 @@ impl<'a> Ctx<'a, '_> {
self.ast.expression_numeric_literal(span, n, None, number_base)
}
ConstantValue::BigInt(n) => {
let raw: &str = (n.to_string() + "n").into_in(&self.ast.allocator);
self.ast.expression_big_int_literal(span, raw, BigintBase::Decimal)
self.ast.expression_big_int_literal(span, n.to_string() + "n", BigintBase::Decimal)
}
ConstantValue::String(s) => self.ast.expression_string_literal(span, s, None),
ConstantValue::Boolean(b) => self.ast.expression_boolean_literal(span, b),
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/tests/ecmascript/array_join.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn test() {
elements.push(ArrayExpressionElement::NumericLiteral(ast.alloc(ast.numeric_literal(
SPAN,
42f64,
"42",
None,
NumberBase::Decimal,
))));
elements.push(ArrayExpressionElement::StringLiteral(
Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,11 @@ impl<'a> ParserImpl<'a> {
pat.map_or_else(|| RegExpPattern::Invalid(pattern_text), RegExpPattern::Pattern)
},
);
Ok(self.ast.reg_exp_literal(self.end_span(span), RegExp { pattern, flags }, Some(Atom::from(raw))))
Ok(self.ast.reg_exp_literal(
self.end_span(span),
RegExp { pattern, flags },
Some(Atom::from(raw)),
))
}

fn parse_regex_pattern(
Expand Down
6 changes: 1 addition & 5 deletions crates/oxc_prettier/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2125,11 +2125,7 @@ impl<'a> Format<'a> for PropertyKey<'a> {
if need_quote {
dynamic_text!(
p,
string::print_string(
p,
&literal.raw_str(),
p.options.single_quote
)
string::print_string(p, &literal.raw_str(), p.options.single_quote)
)
} else {
literal.format(p)
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_semantic/src/checker/javascript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,8 @@ pub fn check_number_literal(lit: &NumericLiteral, ctx: &SemanticBuilder<'_>) {
// NumericLiteral :: legacy_octalIntegerLiteral
// DecimalIntegerLiteral :: NonOctalDecimalIntegerLiteral
// * It is a Syntax Error if the source text matched by this production is strict mode code.
fn leading_zero(s: &Option<Atom>) -> bool {
if let Some(s) = s.as_ref() {
fn leading_zero(s: Option<&Atom>) -> bool {
if let Some(s) = s {
let mut chars = s.bytes();
if let Some(first) = chars.next() {
if let Some(second) = chars.next() {
Expand All @@ -239,10 +239,10 @@ pub fn check_number_literal(lit: &NumericLiteral, ctx: &SemanticBuilder<'_>) {

if ctx.strict_mode() {
match lit.base {
NumberBase::Octal if leading_zero(&lit.raw) => {
NumberBase::Octal if leading_zero(lit.raw.as_ref()) => {
ctx.error(legacy_octal(lit.span));
}
NumberBase::Decimal | NumberBase::Float if leading_zero(&lit.raw) => {
NumberBase::Decimal | NumberBase::Float if leading_zero(lit.raw.as_ref()) => {
ctx.error(leading_zero_decimal(lit.span));
}
_ => {}
Expand Down
16 changes: 4 additions & 12 deletions crates/oxc_transformer/src/jsx/jsx_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,24 +154,16 @@ impl<'a, 'ctx> JsxSource<'a, 'ctx> {

let line_number = {
let key = ctx.ast.property_key_identifier_name(SPAN, "lineNumber");
let value = ctx.ast.expression_numeric_literal(
SPAN,
line as f64,
None,
NumberBase::Decimal,
);
let value =
ctx.ast.expression_numeric_literal(SPAN, line as f64, None, NumberBase::Decimal);
ctx.ast
.object_property_kind_object_property(SPAN, kind, key, value, false, false, false)
};

let column_number = {
let key = ctx.ast.property_key_identifier_name(SPAN, "columnNumber");
let value = ctx.ast.expression_numeric_literal(
SPAN,
column as f64,
None,
NumberBase::Decimal,
);
let value =
ctx.ast.expression_numeric_literal(SPAN, column as f64, None, NumberBase::Decimal);
ctx.ast
.object_property_kind_object_property(SPAN, kind, key, value, false, false, false)
};
Expand Down

0 comments on commit 8c93101

Please sign in to comment.