Skip to content

Commit

Permalink
add change, and testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
FwP-IDN committed Feb 12, 2025
1 parent 054efdd commit 9255320
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ pub(crate) fn wrap_struct_field(
}

pub(crate) fn struct_lit_field_separator(config: &Config) -> &str {
colon_spaces(config)
colon_spaces(config, false)
}

pub(crate) fn rewrite_field(
Expand Down
42 changes: 32 additions & 10 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::borrow::Cow;
use std::cmp::{Ordering, max, min};
use std::backtrace::Backtrace;

use regex::Regex;
use rustc_ast::visit;
Expand Down Expand Up @@ -42,8 +43,8 @@ const DEFAULT_VISIBILITY: ast::Visibility = ast::Visibility {
tokens: None,
};

fn type_annotation_separator(config: &Config) -> &str {
colon_spaces(config)
fn type_annotation_separator(config: &Config, force_space_after_colon: bool) -> &str {
colon_spaces(config, force_space_after_colon)
}

// Statements of the form
Expand Down Expand Up @@ -96,7 +97,13 @@ impl Rewrite for ast::Local {
let mut infix = String::with_capacity(32);

if let Some(ref ty) = self.ty {
let separator = type_annotation_separator(context.config);

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

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 @@ -1890,10 +1897,10 @@ fn rewrite_ty<R: Rewrite>(
Ok(result)
}

fn type_annotation_spacing(config: &Config) -> (&str, &str) {
fn type_annotation_spacing(config: &Config, force_space_after_colon: bool) -> (&str, &str) {
(
if config.space_before_colon() { " " } else { "" },
if config.space_after_colon() { " " } else { "" },
if force_space_after_colon || config.space_after_colon() { " " } else { "" },
)
}

Expand All @@ -1903,7 +1910,7 @@ pub(crate) fn rewrite_struct_field_prefix(
) -> RewriteResult {
let vis = format_visibility(context, &field.vis);
let safety = format_safety(field.safety);
let type_annotation_spacing = type_annotation_spacing(context.config);
let type_annotation_spacing = type_annotation_spacing(context.config, false);
Ok(match field.ident {
Some(name) => format!(
"{vis}{safety}{}{}:",
Expand All @@ -1924,6 +1931,10 @@ impl Rewrite for ast::FieldDef {
}
}

use std::sync::atomic::{AtomicUsize};

static CALL_COUNT: AtomicUsize = AtomicUsize::new(0);

pub(crate) fn rewrite_struct_field(
context: &RewriteContext<'_>,
field: &ast::FieldDef,
Expand All @@ -1939,7 +1950,11 @@ pub(crate) fn rewrite_struct_field(
return Ok(context.snippet(field.span()).to_owned());
}

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

let attrs_str = field.attrs.rewrite_result(context, shape)?;
Expand Down Expand Up @@ -2091,7 +2106,14 @@ fn rewrite_static(
return None;
}

let colon = colon_spaces(context.config);
// 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 colon = colon_spaces(context.config, force_space_after_colon);

let mut prefix = format!(
"{}{}{}{} {}{}{}",
format_visibility(context, static_parts.vis),
Expand Down Expand Up @@ -2294,7 +2316,7 @@ impl Rewrite for ast::Param {
let (before_comment, after_comment) =
get_missing_param_comments(context, self.pat.span, self.ty.span, shape);
result.push_str(&before_comment);
result.push_str(colon_spaces(context.config));
result.push_str(colon_spaces(context.config, false));
result.push_str(&after_comment);
let overhead = last_line_width(&result);
let max_width = shape
Expand Down Expand Up @@ -2322,7 +2344,7 @@ impl Rewrite for ast::Param {
!has_multiple_attr_lines,
)?;
result.push_str(&before_comment);
result.push_str(colon_spaces(context.config));
result.push_str(colon_spaces(context.config, false));
result.push_str(&after_comment);
let overhead = last_line_width(&result);
let max_width = shape
Expand Down
2 changes: 1 addition & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ where
}

fn type_bound_colon(context: &RewriteContext<'_>) -> &'static str {
colon_spaces(context.config)
colon_spaces(context.config, false)
}

// If the return type is multi-lined, then force to use multiple lines for
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@ pub(crate) fn filtered_str_fits(snippet: &str, max_width: usize, shape: Shape) -
}

#[inline]
pub(crate) fn colon_spaces(config: &Config) -> &'static str {
pub(crate) fn colon_spaces(config: &Config, force_space_after_colon: bool) -> &'static str {
let before = config.space_before_colon();
let after = config.space_after_colon();
let after = force_space_after_colon || config.space_after_colon();
match (before, after) {
(true, true) => " : ",
(true, false) => " :",
Expand Down
15 changes: 15 additions & 0 deletions tests/source/issue-6470/case-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// rustfmt-space_after_colon: false

struct SomeStruct {
some_field: ::some_crate::Thing,
}

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;
}
15 changes: 15 additions & 0 deletions tests/target/issue-6470/case-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// rustfmt-space_after_colon: false

struct SomeStruct {
some_field: ::some_crate::Thing,
}

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;
}

0 comments on commit 9255320

Please sign in to comment.