rust-lang / rust-lang/rustfmt

Add option to preserve/format-with nesting inside function call arguments

Open
#4,754 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-feature-request P-low
Dominant language
Rust
Stars
7k
Forks
1.1k
Avg merge
2d 13h
Merged PRs (30d)
24

Description

I like to write function calls, that even when take a single argument, but that argument itself is multiline on a new and separate line, like this:

tokio::spawn(
	Self::future(
		arg1,
		arg2,
		arg3
	)?
);

But as of current:

$ rustfmt --version
rustfmt 1.4.36-nightly (7de6968e 2021-02-07)

The output of formatting is as follows:

tokio::spawn(Self::future(
	arg1,
	arg2,
	arg3
)?);

which I personally find less readable. The first is more clear to me that tokio::spawn takes a single argument, and that arg1, arg2, arg3 are arguments to Self::future and not to tokio::spawn. I mean as in readability, since in this particular case ofc I know how many arguments each function takes.

I think visually what I'm after is https://github.com/rust-lang/rustfmt/blob/master/Configurations.md#combine_control_expr being set to false, but for function arguments as well.

I don't know how to call this feature exactly, I'd gladly rename my issue for other people to find it more easily. If such an option was to be added I'd like to see the following options:

  1. Default
    Does what is done right now.
  2. Preserve
    Same as default but doesn't remove your custom indentation if you took the deliberate time to make it look however you like it to look.
  3. My option (multi-line)
    Formats calls to a function that takes a single argument in a nested fashion if that single argument itself is multi-line. To be more precise, I want the nesting to be the same for both: a function that takes two arguments, and a function that takes a single argument.

num3 also made me think about how maybe sometimes I visually nest even arguments to function calls which take even a single argument on a single line, and I've come to the conclusion that indeed I do:

function_call(Some(Ok(())));

I think manually I would format it as follows:

function_call(
	Some(
		Ok(())
	)
)

I think personally I would use the preserve option, as the heuristic of the single function argument needing to be multi-line would not fit all of my use cases (as shown in the above snippet). Therefore I'd be happy with just two options: default, and preserve. This would require manual change each time, but it would work in all cases.

Also, I wasn't sure to add this as a Formatting Issue, cause it's more a matter of preference I guess.

Sorry if this was asked before, I made a quick search and could find anything, but maybe I used wrong keywords.

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 examining rustfmt's existing configuration for combine_control_expr and the formatter behavior for nested function-call arguments. Compare the reported input and current output with the requested default, preserve, and multi-line behaviors. Done means the chosen option is specified, implemented, and covered by formatting tests for nested calls and manually preserved indentation.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.