Skip to content

Cannot prohibit long code lines while allowing long doc lines #5477

Open
@detly

Description

@detly

I have code that contains this:

//! If that's what you have and it works, you don't need this crate at all — you
//! can just use [`CARGO_BIN_EXE_<name>`][cargo-env].
//!
//!   [cargo-env]: https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates

The last line is 132 characters long. The code also contains this:

        fn get_cargo_env_or_panic(key: &str) -> OsString {
            std::env::var_os(key).unwrap_or_else(||
                panic!("The environment variable '{key}' is not set, is this running under a 'cargo test' command?")
            )
        }

The panic!() line is 117 characters long.

I would like to prohibit the overly long code line, because it can be fixed so that developers don't have to scroll the line to read it. This can be done with the following flags:

cargo fmt --all -- --check --config error_on_line_overflow=true --config error_on_unformatted=true

(Both are necessary.)

The good part is that these flags catch the line of code, which I can change to be formatted automatically. The bad part is that this flag also catches the URL in the comment, which I can't change to be formatted automatically.

To be honest, I'm not really sure what I'd expect, so here are some notes about it in no particular order:

  • This seems related to Do not break long urls in comments. #506, which also mentions that Markdown should allow breaking a URL across lines. I can't find any mention of this in any Markdown flavour documentation, and I can't find out how to get Rustdoc to do it. Arguably this is an issue with Rustdoc, if such a thing should be possible but either isn't, or isn't documented.
  • Also related is Gives up on chains if any line is too long. #3863.
  • This is for running in CI, so using the flags and ignoring the error I don't care about won't work in that context.
  • Perhaps what would fix this is more fine-grained options, but I don't know what for. Overlong comments vs overlong code? But I still want to catch overlong comments that can be reflowed. Should Rustfmt identify that it's a URL and it can't be reflowed? That seems extremely specific. Does Rustfmt even "understand" Markdown?

Versions:

  • Cargo 1.62
  • Rustfmt 1.4.38-stable
  • Rustdoc 1.62.1

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions