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

chore: Format code in doc comments #3222

Closed

Conversation

umgefahren
Copy link
Contributor

Description

I discovered there is an unstable option to format code in doc comments. Although the option is unstable the resulting changes seem very reasonable.

To reproduce:

  1. Add file named rustfmt.toml to project root
  2. Fill it with the following:
    unstable_features = true
    format_code_in_doc_comments = true
  3. Run: cargo fmt --all

Notes

As soon as this is stabilized, we should add it to our CI. Linking the PR here: rust-lang/rustfmt#3348

Links to any relevant issues

None.

Open Questions

None.

Change checklist

  • I have performed a self-review of my own code
  • A changelog entry has been made in the appropriate crates

@mergify
Copy link
Contributor

mergify bot commented Dec 9, 2022

This pull request has merge conflicts. Could you please resolve them @umgefahren? 🙏

@thomaseizinger
Copy link
Contributor

  1. Add file named rustfmt.toml to project root
  2. Fill it with the following:
    unstable_features = true
    format_code_in_doc_comments = true

Unstable options are unavailable on stable Rust, right? You can hack around that by passing the options via the commandline last time I checked :)

We could do the same thing as we do for clippy and make a cargo alias for formatting.

See here for example: https://github.com/Restioson/xtra/blob/master/.cargo/config.toml

@umgefahren
Copy link
Contributor Author

  1. Add file named rustfmt.toml to project root
  2. Fill it with the following:
    unstable_features = true
    format_code_in_doc_comments = true

Unstable options are unavailable on stable Rust, right? You can hack around that by passing the options via the commandline last time I checked :)

We could do the same thing as we do for clippy and make a cargo alias for formatting.

See here for example: https://github.com/Restioson/xtra/blob/master/.cargo/config.toml

That"s a good idea. I was looking for passing with options and probably overlooked it. I will add a custom cargo fmt command alias. Although it should be noted that this feature is not bug-free. Last time I looked it could case an ICE on empty doc comment code blocks.

@mergify
Copy link
Contributor

mergify bot commented Dec 12, 2022

This pull request has merge conflicts. Could you please resolve them @umgefahren? 🙏

@@ -1,3 +1,4 @@
[alias]
# Temporary solution to have clippy config in a single place until https://github.com/rust-lang/rust-clippy/blob/master/doc/roadmap-2021.md#lintstoml-configuration is shipped.
custom-clippy = "clippy --workspace --all-features --all-targets -- -A clippy::type_complexity -A clippy::pedantic -D warnings"
custom-fmt = "fmt -- --config format_code_in_doc_comments=true"
Copy link
Contributor

Choose a reason for hiding this comment

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

If we do this, I"d like to enforce it in CI as well.

@thomaseizinger
Copy link
Contributor

That"s a good idea. I was looking for passing with options and probably overlooked it. I will add a custom cargo fmt command alias. Although it should be noted that this feature is not bug-free. Last time I looked it could case an ICE on empty doc comment code blocks.

That is not ideal but I don"t think it is a big issue. We can go back to regular rustfmt if it ever causes problems.

@thomaseizinger
Copy link
Contributor

It would be good to get some more opinions on this though. cc @libp2p/rust-libp2p-maintainers

@jxs
Copy link
Member

jxs commented Dec 13, 2022

fmt -- --config format_code_in_doc_comments=true

I think it makes sense! Can we also stage the rustfmt.toml so when we cargo fmt it runs with such options?

@umgefahren
Copy link
Contributor Author

fmt -- --config format_code_in_doc_comments=true

I think it makes sense! Can we also stage the rustfmt.toml so when we cargo fmt it runs with such options?

We could, but it would be a bit inconsistent with how we do things in this repo. Since we could could configure clippy via clippy.toml as well. But not a strong opinion.

@thomaseizinger
Copy link
Contributor

thomaseizinger commented Dec 13, 2022

fmt -- --config format_code_in_doc_comments=true

I think it makes sense! Can we also stage the rustfmt.toml so when we cargo fmt it runs with such options?

We could, but it would be a bit inconsistent with how we do things in this repo. Since we could could configure clippy via clippy.toml as well. But not a strong opinion.

Configuring clippy lints via a config file is unimplemented so we can"t use it.

Activating unstable fmt options on stable Rust also doesn"t work so we can"t use it :)

@thomaseizinger thomaseizinger added the need/author-input Needs input from the original author label Feb 1, 2023
@github-actions github-actions bot added the Stale label Apr 3, 2023
@github-actions github-actions bot closed this Apr 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need/author-input Needs input from the original author Stale
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants