Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I fix unused imports in Rust automatically?

Is there any way to fix ONLY unused imports in Rust automatically? I have seen cargo fix, which does work, but it makes a whole bunch of other fixes too. Is there any way to tell cargo fix to only fix unused imports (preferably in a specified file) and nothing else?

Side question: Is there any way to make IntelliJ do this? The usual shortcut (Cmd+Option+O) works in other languages, but for Rust, it just re-orders the imports.

like image 896
Akshay Avatar asked Apr 07 '20 10:04

Akshay


2 Answers

Individual lints are currently only configurable via attributes in your source code. There is no way to tell cargo check or cargo fix to ignore certain lint rules through the command line tool.

From my experience with JetBrains tools, it's unlikely that they'd use cargo fix under the hood anyway. They have built their own Rust tooling for parsing and analysing code, and that is where this feature would go.

I suggest submitting a feature request to JetBrains.

like image 135
Peter Hall Avatar answered Oct 19 '22 20:10

Peter Hall


According to this pull request, JetBrains' Rust plugin should be able to remove unused imports already but it is not enabled by default. According to this comment, it seems like the dev team need to wait until org.rust.macros.proc (currently an experimental feature disabled by default) to be enabled by default.

like image 31
Leung Yau Ming Avatar answered Oct 19 '22 18:10

Leung Yau Ming