Skip to content

Commit

Permalink
make function to check absolute path declaration and fix logic error …
Browse files Browse the repository at this point in the history
…regarding to it
  • Loading branch information
FwP-IDN committed Feb 13, 2025
1 parent 6befafc commit 2c8d6bd
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
31 changes: 17 additions & 14 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,9 @@ impl Rewrite for ast::Local {
let mut infix = String::with_capacity(32);

if let Some(ref ty) = self.ty {

let force_space_after_colon = match ty.clone().into_inner().kind {
ast::TyKind::Path(None, _) => true,
_ => false,
};

let force_space_after_colon = is_ty_kind_with_global_decl(&ty.clone().into_inner().kind);
let separator = type_annotation_separator(context.config, force_space_after_colon);

let ty_shape = if pat_str.contains('\n') {
shape.with_max_width(context.config)
} else {
Expand Down Expand Up @@ -1945,10 +1941,7 @@ pub(crate) fn rewrite_struct_field(
return Ok(context.snippet(field.span()).to_owned());
}

let force_space_after_colon = match field.ty.clone().into_inner().kind {
ast::TyKind::Path(None, _) => true,
_ => false,
};
let force_space_after_colon = is_ty_kind_with_global_decl(&field.ty.clone().into_inner().kind);
let type_annotation_spacing = type_annotation_spacing(context.config, force_space_after_colon);
let prefix = rewrite_struct_field_prefix(context, field)?;

Expand Down Expand Up @@ -2088,6 +2081,19 @@ impl<'a> StaticParts<'a> {
}
}

fn is_ty_kind_with_global_decl(ty_kind: &ast::TyKind) -> bool {
match ty_kind {
ast::TyKind::Path(None, ast_path) => {
let segments = &ast_path.segments;
match segments.first() {
Some(path_segment) => path_segment.ident.name == symbol::kw::PathRoot,
None => false,
}
},
_ => false,
}
}

fn rewrite_static(
context: &RewriteContext<'_>,
static_parts: &StaticParts<'_>,
Expand All @@ -2103,10 +2109,7 @@ fn rewrite_static(

// if after a semicolon is absolute path declaration (::) need to force
// space after colon, because ::: syntax cannot compile
let force_space_after_colon = match static_parts.ty.kind {
ast::TyKind::Path(None, _) => true,
_ => false,
};
let force_space_after_colon = is_ty_kind_with_global_decl(&static_parts.ty.kind);
let colon = colon_spaces(context.config, force_space_after_colon);

let mut prefix = format!(
Expand Down
8 changes: 4 additions & 4 deletions tests/target/issue-6470/case-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ const THING: ::some_crate::SomeType = ::some_crate::SomeType::default();

fn main() {
let x: ::some_crate::SomeType = ::some_crate::SomeType::default();
let a1: int;
let a2: int;
let a3: int;
let a4: int;
let a1:int;
let a2:int;
let a3:int;
let a4:int;
}

0 comments on commit 2c8d6bd

Please sign in to comment.