From f0066ea4cc7bd4f465b783f3f2053eea9c83bdfa Mon Sep 17 00:00:00 2001 From: Boshen Date: Thu, 27 Apr 2023 22:21:16 +0800 Subject: [PATCH] refactor: unify the usage of `std::fmt` import --- crates/oxc_ast/src/ast/js.rs | 10 +++++----- crates/oxc_ast/src/ast/operator.rs | 22 +++++++++++----------- crates/oxc_ast/src/serialize.rs | 4 +++- crates/oxc_hir/src/hir.rs | 10 +++++----- crates/oxc_linter/src/rule.rs | 8 ++++---- tasks/common/src/test_file.rs | 4 ++-- 6 files changed, 30 insertions(+), 28 deletions(-) diff --git a/crates/oxc_ast/src/ast/js.rs b/crates/oxc_ast/src/ast/js.rs index 2ce4e4c95..55a45f990 100644 --- a/crates/oxc_ast/src/ast/js.rs +++ b/crates/oxc_ast/src/ast/js.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use std::fmt; use num_bigint::BigUint; use oxc_allocator::{Box, Vec}; @@ -947,8 +947,8 @@ impl VariableDeclarationKind { } } -impl Display for VariableDeclarationKind { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl fmt::Display for VariableDeclarationKind { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match self { Self::Var => "var", Self::Const => "const", @@ -1805,8 +1805,8 @@ pub enum ModuleExportName { StringLiteral(StringLiteral), } -impl Display for ModuleExportName { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl fmt::Display for ModuleExportName { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match self { Self::Identifier(identifier) => identifier.name.to_string(), Self::StringLiteral(literal) => literal.value.to_string(), diff --git a/crates/oxc_ast/src/ast/operator.rs b/crates/oxc_ast/src/ast/operator.rs index bd359346e..e9d394842 100644 --- a/crates/oxc_ast/src/ast/operator.rs +++ b/crates/oxc_ast/src/ast/operator.rs @@ -1,4 +1,4 @@ -use std::fmt::{Display, Formatter, Result}; +use std::fmt; #[cfg(feature = "serde")] use serde::Serialize; @@ -135,8 +135,8 @@ impl AssignmentOperator { } } -impl Display for AssignmentOperator { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { +impl fmt::Display for AssignmentOperator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let operator = self.as_str(); write!(f, "{operator}") } @@ -278,8 +278,8 @@ impl BinaryOperator { } } -impl Display for BinaryOperator { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { +impl fmt::Display for BinaryOperator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let operator = self.as_str(); write!(f, "{operator}") } @@ -307,8 +307,8 @@ impl LogicalOperator { } } -impl Display for LogicalOperator { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { +impl fmt::Display for LogicalOperator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let operator = self.as_str(); write!(f, "{operator}") } @@ -368,8 +368,8 @@ impl UnaryOperator { } } -impl Display for UnaryOperator { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { +impl fmt::Display for UnaryOperator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let operator = self.as_str(); write!(f, "{operator}") } @@ -394,8 +394,8 @@ impl UpdateOperator { } } -impl Display for UpdateOperator { - fn fmt(&self, f: &mut Formatter<'_>) -> Result { +impl fmt::Display for UpdateOperator { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let operator = self.as_str(); write!(f, "{operator}") } diff --git a/crates/oxc_ast/src/serialize.rs b/crates/oxc_ast/src/serialize.rs index 9929e1406..109157147 100644 --- a/crates/oxc_ast/src/serialize.rs +++ b/crates/oxc_ast/src/serialize.rs @@ -1,3 +1,5 @@ +use std::fmt; + use serde::{ser::Serializer, Serialize}; use crate::ast::{Program, RegExpFlags}; @@ -29,7 +31,7 @@ impl<'a> Program<'a> { pub fn serialize_bigint(value: &T, s: S) -> Result where - T: std::fmt::Display, + T: fmt::Display, S: serde::Serializer, { s.collect_str(&format_args!("{value}n")) diff --git a/crates/oxc_hir/src/hir.rs b/crates/oxc_hir/src/hir.rs index 4293fea74..f0765b638 100644 --- a/crates/oxc_hir/src/hir.rs +++ b/crates/oxc_hir/src/hir.rs @@ -1,4 +1,4 @@ -use std::fmt::Display; +use std::fmt; use oxc_allocator::{Box, Vec}; use oxc_ast::ast::{ @@ -951,8 +951,8 @@ impl VariableDeclarationKind { } } -impl Display for VariableDeclarationKind { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl fmt::Display for VariableDeclarationKind { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match self { Self::Var => "var", Self::Const => "const", @@ -1809,8 +1809,8 @@ pub enum ModuleExportName { StringLiteral(StringLiteral), } -impl Display for ModuleExportName { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { +impl fmt::Display for ModuleExportName { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let s = match self { Self::Identifier(identifier) => identifier.name.to_string(), Self::StringLiteral(literal) => literal.value.to_string(), diff --git a/crates/oxc_linter/src/rule.rs b/crates/oxc_linter/src/rule.rs index 92946f21e..5a6b7be18 100644 --- a/crates/oxc_linter/src/rule.rs +++ b/crates/oxc_linter/src/rule.rs @@ -1,10 +1,10 @@ -use std::fmt::{Debug, Display}; +use std::fmt; use oxc_semantic::Symbol; use crate::{context::LintContext, AstNode}; -pub trait Rule: Sized + Default + Debug { +pub trait Rule: Sized + Default + fmt::Debug { /// Initialize from eslint json configuration #[must_use] fn from_configuration(_value: serde_json::Value) -> Self { @@ -54,8 +54,8 @@ impl RuleCategory { } } -impl Display for RuleCategory { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl fmt::Display for RuleCategory { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Self::Correctness => write!(f, "Correctness"), Self::Restriction => write!(f, "Restriction"), diff --git a/tasks/common/src/test_file.rs b/tasks/common/src/test_file.rs index e5ca35313..2b4f4430a 100644 --- a/tasks/common/src/test_file.rs +++ b/tasks/common/src/test_file.rs @@ -1,4 +1,4 @@ -use std::str::FromStr; +use std::{fmt, str::FromStr}; use crate::project_root; @@ -79,6 +79,6 @@ impl TestFile { } } -fn err_to_string(e: E) -> String { +fn err_to_string(e: E) -> String { format!("{e:?}") }