rust-lang / rust-lang/rustfmt

rustfmt leaves behind a too-long line inside a closure

Open
#6,344 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-closures I-max-width I-poor-formatting
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

Here's some example code:

impl<'tcx> LateLintPass<'tcx> for UnqualifiedLocalImports {
    fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
        let hir::ItemKind::Use(path, _kind) = item.kind else { return };
        let is_local_import = |res: &Res| {
            matches!(res, hir::def::Res::Def(def_kind, def_id) if def_id.is_local() && !matches!(def_kind, DefKind::Macro(_)))
        };
    }
}

It's not the best code, that line is too long. So let's format it. Now we have:

impl<'tcx> LateLintPass<'tcx> for UnqualifiedLocalImports {
    fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx hir::Item<'tcx>) {
        let hir::ItemKind::Use(path, _kind) = item.kind else {
            return;
        };
        let is_local_import = |res: &Res| matches!(res, hir::def::Res::Def(def_kind, def_id) if def_id.is_local() && !matches!(def_kind, DefKind::Macro(_)));
    }
}

Now the line is even longer! It's way past the 100 character limit.

I guess rustfmt is reluctant to introduce linebreaks inside a macro, but then it should just avoid touching this code -- but instead it makes things worse by putting the entire closure on a single line.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by formatting the Rust snippet from the issue and compare the resulting closure and line length with the 100-character limit. Trace rustfmt's handling of closures containing matches! macros, then add a regression test for this input. Done means formatting no longer produces the longer single-line closure or otherwise violates the stated limit.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.