Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Defer changes for zero argument functions until style_edition=2027 #6362

Merged
merged 2 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion src/config/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,16 @@ impl From<StyleEdition> for rustc_span::edition::Edition {

impl PartialOrd for StyleEdition {
fn partial_cmp(&self, other: &StyleEdition) -> Option<std::cmp::Ordering> {
rustc_span::edition::Edition::partial_cmp(&(*self).into(), &(*other).into())
// FIXME(ytmimi): Update `StyleEdition::Edition2027` logic when
// `rustc_span::edition::Edition::Edition2027` becomes available in the compiler
match (self, other) {
(Self::Edition2027, Self::Edition2027) => Some(std::cmp::Ordering::Equal),
(_, Self::Edition2027) => Some(std::cmp::Ordering::Less),
(Self::Edition2027, _) => Some(std::cmp::Ordering::Greater),
(Self::Edition2015 | Self::Edition2018 | Self::Edition2021 | Self::Edition2024, _) => {
rustc_span::edition::Edition::partial_cmp(&(*self).into(), &(*other).into())
}
}
}
}

Expand Down Expand Up @@ -684,3 +693,41 @@ config_option_with_style_edition_default!(
MakeBackup, bool, _ => false;
PrintMisformattedFileNames, bool, _ => false;
);

#[test]
fn style_edition_comparisons() {
// Style Edition 2015
assert!(StyleEdition::Edition2015 == StyleEdition::Edition2015);
assert!(StyleEdition::Edition2015 < StyleEdition::Edition2018);
assert!(StyleEdition::Edition2015 < StyleEdition::Edition2021);
assert!(StyleEdition::Edition2015 < StyleEdition::Edition2024);
assert!(StyleEdition::Edition2015 < StyleEdition::Edition2027);

// Style Edition 2018
assert!(StyleEdition::Edition2018 > StyleEdition::Edition2015);
assert!(StyleEdition::Edition2018 == StyleEdition::Edition2018);
assert!(StyleEdition::Edition2018 < StyleEdition::Edition2021);
assert!(StyleEdition::Edition2018 < StyleEdition::Edition2024);
assert!(StyleEdition::Edition2018 < StyleEdition::Edition2027);

// Style Edition 2021
assert!(StyleEdition::Edition2021 > StyleEdition::Edition2015);
assert!(StyleEdition::Edition2021 > StyleEdition::Edition2018);
assert!(StyleEdition::Edition2021 == StyleEdition::Edition2021);
assert!(StyleEdition::Edition2021 < StyleEdition::Edition2024);
assert!(StyleEdition::Edition2021 < StyleEdition::Edition2027);

// Style Edition 2024
assert!(StyleEdition::Edition2024 > StyleEdition::Edition2015);
assert!(StyleEdition::Edition2024 > StyleEdition::Edition2018);
assert!(StyleEdition::Edition2024 > StyleEdition::Edition2021);
assert!(StyleEdition::Edition2024 == StyleEdition::Edition2024);
assert!(StyleEdition::Edition2024 < StyleEdition::Edition2027);

// Style Edition 2024
assert!(StyleEdition::Edition2027 > StyleEdition::Edition2015);
assert!(StyleEdition::Edition2027 > StyleEdition::Edition2018);
assert!(StyleEdition::Edition2027 > StyleEdition::Edition2021);
assert!(StyleEdition::Edition2027 > StyleEdition::Edition2024);
assert!(StyleEdition::Edition2027 == StyleEdition::Edition2027);
}
2 changes: 1 addition & 1 deletion src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2570,7 +2570,7 @@ fn rewrite_fn_base(
.last()
.map_or(false, |last_line| last_line.contains("//"));

if context.config.style_edition() >= StyleEdition::Edition2024 {
if context.config.style_edition() >= StyleEdition::Edition2027 {
if closing_paren_overflow_max_width {
result.push(')');
result.push_str(&indent.to_string_with_newline(context.config));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-style_edition: 2024
// rustfmt-style_edition: 2027

pub fn parse_conditional<'a, I: 'a>()
-> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
Expand Down
21 changes: 21 additions & 0 deletions tests/source/long-fn-1/style_edition_2027.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// rustfmt-style_edition: 2027
// Tests that a function which is almost short enough, but not quite, gets
// formatted correctly.

impl Foo {
fn some_input(&mut self, input: Input, input_path: Option<PathBuf>, ) -> (Input, Option<PathBuf>) {}

fn some_inpu(&mut self, input: Input, input_path: Option<PathBuf>) -> (Input, Option<PathBuf>) {}
}

// #1843
#[allow(non_snake_case)]
pub extern "C" fn Java_com_exonum_binding_storage_indices_ValueSetIndexProxy_nativeContainsByHash() -> bool {
false
}

// #3009
impl Something {
fn my_function_name_is_way_to_long_but_used_as_a_case_study_or_an_example_its_fine(
) -> Result< (), String > {}
}
8 changes: 8 additions & 0 deletions tests/target/issue-3278/style_edition_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-style_edition: 2024

pub fn parse_conditional<'a, I: 'a>(
) -> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
where
I: Stream<Item = char>,
{
}
8 changes: 8 additions & 0 deletions tests/target/issue-3278/style_edition_2027.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// rustfmt-style_edition: 2027

pub fn parse_conditional<'a, I: 'a>()
-> impl Parser<Input = I, Output = Expr, PartialState = ()> + 'a
where
I: Stream<Item = char>,
{
}
29 changes: 29 additions & 0 deletions tests/target/long-fn-1/style_edition_2024.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// rustfmt-style_edition: 2024
// Tests that a function which is almost short enough, but not quite, gets
// formatted correctly.

impl Foo {
fn some_input(
&mut self,
input: Input,
input_path: Option<PathBuf>,
) -> (Input, Option<PathBuf>) {
}

fn some_inpu(&mut self, input: Input, input_path: Option<PathBuf>) -> (Input, Option<PathBuf>) {
}
}

// #1843
#[allow(non_snake_case)]
pub extern "C" fn Java_com_exonum_binding_storage_indices_ValueSetIndexProxy_nativeContainsByHash(
) -> bool {
false
}

// #3009
impl Something {
fn my_function_name_is_way_to_long_but_used_as_a_case_study_or_an_example_its_fine(
) -> Result<(), String> {
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-style_edition: 2024
// rustfmt-style_edition: 2027
// Tests that a function which is almost short enough, but not quite, gets
// formatted correctly.

Expand Down
Loading