如何自动运行“货物修复”在 VSCode 中保存?
- 目前,我在编写 Rust 时使用 VSCode 作为 IDE。
- 我使用 rust-analyzer VSCode 扩展,因为它似乎是 Rust 最好的扩展(截至 2022 年)。
我在 VSCode 的“settings.json”中有以下两个条目:
{
"rust-analyzer.checkOnSave.command": "clippy",
"[rust]": {
"editor.formatOnSave": true,
},
}
- 这使得
clippy
(linter)在按 Ctrl + s 保存文件时自动运行。 - 它还使得 rustfmt(格式化程序)在按 Ctrl + s 保存文件时自动运行。
除了这两件事之外,我还希望 cargo fix
在按 Ctrl + s 保存文件时自动运行,因为这样做会自动清理未使用的导入。
我怎样才能做到这一点?
(我希望自动清理未使用的导入,就像我想要自动格式化程序一样。手动删除未使用的导入非常乏味,完全浪费我的时间,就像手动添加适当数量的制表符或空格一样。 )
- Currently, I use VSCode as my IDE when programming Rust.
- I use the
rust-analyzer
VSCode extension, since it seems to be the best one out there for Rust (as of 2022).
I have the following two entries in my "settings.json" for VSCode:
{
"rust-analyzer.checkOnSave.command": "clippy",
"[rust]": {
"editor.formatOnSave": true,
},
}
- This makes it so that
clippy
(the linter) runs automatically upon pressing Ctrl + s to save the file. - It also makes it so that
rustfmt
(the formatter) runs automatically upon pressing Ctrl + s to save the file.
In addition to these two things, I also want cargo fix
to run automatically upon pressing Ctrl + s to save the file, because doing that automatically cleans up unused imports.
How can I accomplish this?
(I want unused imports to be automatically cleaned up for the same reason that I want an automatic formatter. Manually removing unused imports is tedium and a complete waste of my time in the same way that manually adding the appropriate amount of tabs or spaces is.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Clippy 有一个
--fix
选项,可以自动应用建议的修复。因此,您所需要做的就是更改保存时检查命令。但是,您需要两个参数clippy
和--fix
,为此您不能使用rust-analyzer.check.command
并且必须重写完整命令:但是请注意,它将应用所有自动适用的建议,而不仅仅是删除未使用的导入。
所有这些标志都是 rust-analyzer 自动添加到 rust-analyzer.check.command 的内容,但是当您使用 rust-analyzer.check.overrideCommand 时,您必须指定它们你自己。这是它们的简短描述(文档有更多详细信息):
--workspace
- 将命令应用于 Cargo 工作区中的所有成员,而不仅仅是一个包。--message-format=json
- 发出 JSON 作为响应而不是人类可读的输出,以便 rust-analyzer 可以分析响应并显示错误。--all-targets
- 检查所有内容,即二进制文件、库、示例、测试和基准。--allow-dirty
- 即使工作区发生更改也可以修复。Clippy has a
--fix
option, that applies the suggested fixes automatically. So all you need is to change the the on-save check command. However, you need two argumentsclippy
and--fix
, and for that you cannot userust-analyzer.check.command
and have to override the full command:Beware, however, that it will apply all automatically-applicable suggestions, not just remove unused imports.
All of these flags are what rust-analyzer automatically adds to
rust-analyzer.check.command
, but when you userust-analyzer.check.overrideCommand
you have to specify them yourself. Here's a short description of them (the docs has more details):--workspace
- apply the command to all members in the Cargo workspace, and not just one package.--message-format=json
- emit JSON as a response instead of human-readable output, so that rust-analyzer can analyze the response and show the errors.--all-targets
- check all things, i.e. binaries, libraries, examples, tests and benchmarks.--allow-dirty
- fix even if workspace has changes.