Formatting config option: `force_explicit_return`

未关闭
#7,155 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
45/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
活跃
技术栈
rust
领域
tooling

调研方向

Start by locating the existing force_explicit_abi configuration option and the formatter entry points for function bodies and branches. Done means the new option consistently adds explicit return statements for the shown value-returning and unit-returning examples, with coverage for branching cases.

由索引模型根据 Issue 内容生成。

描述

C-feature-request P-low

Feature Request

Summary

I'd like the option to force all branches of all functions to use the optional return keyword.

Motivation

I think explicitly stating return rather than relying on the implicit behavior of {} blocks makes code easier to understand at a glance, and especially if there are complicated branching statements inside. (Admittedly, this is mostly a style preference, so I understand that it probably won't be a high-priority change.)

For a toy example, the proposed formatting option should change this:

fn add_one(x: u32) -> u32 {
    x + 1
}

fn foo() -> () {
    println!("This function has no return value!");
}

to this:

fn add_one(x: u32) -> u32 {
    return x + 1;
}

fn foo() -> () {
    println!("This function has no return value!");
    return;
}

This syntax is more familiar to developers with backgrounds in other languages, such as C/C++, and follows the common Rust pattern that explicit is better than implicit.

Related configuration options

I wasn't able to find anything that was directly related to my proposed option, but it would follow in the spirit of options like force_explicit_abi.

主要语言
Rust
星标
7.1k
派生
1.2k
平均合并
1 天 4 小时
30 天内合并 PR
20

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/rustfmt 的其他 Issue

查看 rust-lang/rustfmt 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。