Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Run cargo fmt on the whole code base #9394

Merged
19 commits merged into from
Jul 21, 2021
Merged

Run cargo fmt on the whole code base #9394

19 commits merged into from
Jul 21, 2021

Conversation

bkchr
Copy link
Member

@bkchr bkchr commented Jul 20, 2021

No description provided.

@bkchr bkchr added A2-insubstantial Pull request requires no code review (e.g., a sub-repository hash update). B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Jul 20, 2021
@athei
Copy link
Member

athei commented Jul 20, 2021

IMHO we should add a cargo fmt -- --check CI job in this PR. Because otherwise differently formatted code will slip in and make running cargo fmt a pain, again.

@bkchr bkchr requested a review from a team as a code owner July 20, 2021 09:19
Copy link
Contributor

@gilescope gilescope left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked over 1/4 of these changes and am happy with them.

@athei
Copy link
Member

athei commented Jul 20, 2021

One thing I noticed is that the auto generated weight files are reformatted, too. This will break the CI check whenever those are updated (by the bot).

I think we should either run rustfmt automatically on the generated weight files (not sure how) or skip those files in cargo fmt. Not sure how to do this either. Maybe we can add some #[rustfmt(skip)] to the weight template?

Copy link
Contributor

@expenses expenses left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might cause some issues for PR authors, but it needs to be done anyway

@andresilva
Copy link
Contributor

paritytech/polkadot@2a49df1

@bkchr Do you mind adding the same changes to rustfmt.toml here? It's explained here.

Maybe we can add some #[rustfmt(skip)] to the weight template?

This is probably the easiest and most sensible thing to do.

Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went through the first 60 files. Overall I think rustfmt does a very good job and there's not much to fix but I found some issues with weird formatting around: multi-line strings and really long expression lines (e.g. matching on the result of a function call). For multi-line strings maybe the format_strings option should help, for other cases introducing some temporary variables helps with making the code more readable. If we could somehow coordinate people to actually look at the diffs then we could probably catch all regressions, we only need 10 people to look at 100 files each :)

Comment on lines +125 to +126
You can enable it with `--features runtime-benchmarks`."
.into())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks weird. Wouldn't format_string = true help?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes format_strings would help but it messes up things like client/cli/src/commands/utils.rs:

			println!(
				"Network ID/version: {}\n  \
				 Public key (hex):   {}\n  \
				 Public key (SS58):  {}\n  \
				 Account ID:         {}\n  \
				 SS58 Address:       {}",

So our choice is to manually indent multiline strings (keeping control) or to add rustfmt::skips where we need more control. On the whole I think doing multi-line strings manually means less surprises.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's about 5 places in the codebase where it messes up (mostly snippits of json). Personally I'd not format strings but given there's only 5 places where it causes harm 🤷 ...

crate::mock::new_test_ext(),
crate::mock::Test,
);
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test,);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test,);
impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose this will happen in a bunch of other places. Maybe should be reported as an issue to rustfmt?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok so we can in general remove those commas as we find them and it won't put them back.
Maybe we have a separate follow-up PR that nurfs those extra commas at the end of macros?

Comment on lines +96 to +114
]
.iter()
{
for block_type in [
BlockType::RandomTransfersKeepAlive,
BlockType::RandomTransfersReaping,
BlockType::Noop,
].iter() {
for database_type in [BenchDataBaseType::RocksDb, BenchDataBaseType::ParityDb].iter() {
import_benchmarks.push((profile, size.clone(), block_type.clone(), database_type));
]
.iter()
{
for database_type in
[BenchDataBaseType::RocksDb, BenchDataBaseType::ParityDb].iter()
{
import_benchmarks.push((
profile,
size.clone(),
block_type.clone(),
database_type,
));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe introducing some variables for the arrays would make this code less ugly.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we want to keep this PR as small as possible in terms of manual code changes. Beautification should be a followup PR.

Comment on lines 98 to +100
Err("Benchmarking wasn't enabled when building the node. \
You can enable it with `--features runtime-benchmarks`.".into())
}
}
You can enable it with `--features runtime-benchmarks`."
.into())
Copy link
Contributor

@andresilva andresilva Jul 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as the earlier issue with formatting multi-line strings.

Comment on lines +166 to +168
Some(Subcommand::TryRuntime) => Err("TryRuntime wasn't enabled when building the node. \
You can enable it with `--features try-runtime`."
.into()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yuck.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's leaving untouched the leading spaces. If we manually fix the indenting of these lines then rustfmt will retain them.

Comment on lines +42 to +45
node_runtime::WASM_BINARY.expect(
"Development wasm binary is not available. \
Testing is only supported with the flag disabled.",
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong.

Comment on lines +48 to +49
"Development wasm binary is not available. \
Testing is only supported with the flag disabled.",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong.

Comment on lines 209 to +218
let header = match executor_call::<NeverNativeValue, fn() -> _>(
env,
"BlockBuilder_finalize_block",
&[0u8;0],
&[0u8; 0],
true,
None,
).0.unwrap() {
)
.0
.unwrap()
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Creating a temp var for the result of executor_call would probably make this less weird.

Comment on lines +104 to +107
"Development wasm binary is not available. This means the client is \
built with `SKIP_WASM_BUILD` flag and it is only usable for \
production chains. Please rebuild with the flag disabled.")
production chains. Please rebuild with the flag disabled.",
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong.

@@ -233,6 +233,14 @@ cargo-deny:
# FIXME: Temorarily allow to fail.
allow_failure: true

cargo-fmt:
stage: test
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as a debug step, just to avoid running everything else, move it to the previous stage

Suggested change
stage: test
stage: .pre

just do not forget to revert this at the end :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ty, but that works ;)

mem,
rx,
)
.unwrap_err();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again here if we pull these strings out into vars they will format fine.

Copy link
Contributor

@andresilva andresilva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's best not to do any manual changes in this PR otherwise it will make it harder to deal with conflicts on existing PRs.

@bkchr
Copy link
Member Author

bkchr commented Jul 21, 2021

bot merge

@ghost
Copy link

ghost commented Jul 21, 2021

Waiting for commit status.

@ghost ghost merged commit 1d5abf0 into master Jul 21, 2021
@ghost ghost deleted the bkchr-cargo-fmt branch July 21, 2021 14:32
@emostov emostov mentioned this pull request Jul 21, 2021
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A2-insubstantial Pull request requires no code review (e.g., a sub-repository hash update). B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants