Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check rust code with dprint / clippy #2587

Open
michael-kerscher opened this issue Jan 24, 2025 · 0 comments
Open

Check rust code with dprint / clippy #2587

michael-kerscher opened this issue Jan 24, 2025 · 0 comments

Comments

@michael-kerscher
Copy link
Collaborator

michael-kerscher commented Jan 24, 2025

This issue collects infos from multiple places and some research I have done.

What

At the moment dprint is already used in the CI to check various file format for consistent formatting (config)

- name: Check formatting
uses: dprint/[email protected]

Pull requests #2025 and #2410 argue already for a check with cargo clippy to have an in-depth check of rust as well.

#2025 (comment) mentions that clippy-driver could do the checking as in contrast cargo clippy as mentioned in #2025 (comment)

clippy-driver

An easy implementation would include clippy-driver as an dprint-plugin-exec command like

      {
        "command": "clippy-driver --edition 2021 -Cpanic=abort {{file_path}}",
        "exts": ["rs"]
      }

As already mentioned in #2413 (comment) this creates some problems as dprint only works on individual files while cargo clippy looks at the entire context (e.g. dependency crates).

#2413 (comment) argues that the code examples inside markdown files are pretty self-contained as they rely on playground and its crates.

Problems

And while that might be true, it is not as easy as it looks, because the rust playground provides the top 100 most downloaded crates from crates.io (https://play.rust-lang.org/help#features-crates) (explicit list)

dependency crates

An example that is using that can be found at concurrency/async-exercises/chat-async/src/bin/client.rs.

Output from `clippy-driver --edition=2021 src/concurrency/async-exercises/chat-async/src/bin/client.rs`
$ clippy-driver --edition=2021 src/concurrency/async-exercises/chat-async/src/bin/client.rs
error[E0433]: failed to resolve: use of undeclared crate or module `futures_util`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:17:5
   |
17 | use futures_util::stream::StreamExt;
   |     ^^^^^^^^^^^^ use of undeclared crate or module `futures_util`

error[E0432]: unresolved import `futures_util`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:18:5
   |
18 | use futures_util::SinkExt;
   |     ^^^^^^^^^^^^ use of undeclared crate or module `futures_util`

error[E0433]: failed to resolve: use of undeclared crate or module `tokio`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:20:5
   |
20 | use tokio::io::{AsyncBufReadExt, BufReader};
   |     ^^^^^ use of undeclared crate or module `tokio`

error[E0432]: unresolved import `http`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:19:5
   |
19 | use http::Uri;
   |     ^^^^ use of undeclared crate or module `http`

error[E0432]: unresolved import `tokio_websockets`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:21:5
   |
21 | use tokio_websockets::{ClientBuilder, Message};
   |     ^^^^^^^^^^^^^^^^ use of undeclared crate or module `tokio_websockets`

error[E0433]: failed to resolve: use of undeclared crate or module `tokio`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:23:3
   |
23 | #[tokio::main]
   |   ^^^^^ use of undeclared crate or module `tokio`

error[E0433]: failed to resolve: use of undeclared crate or module `tokio`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:36:9
   |
36 |         tokio::select! {
   |         ^^^^^ use of undeclared crate or module `tokio`

error[E0433]: failed to resolve: use of undeclared crate or module `tokio`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:30:17
   |
30 |     let stdin = tokio::io::stdin();
   |                 ^^^^^ use of undeclared crate or module `tokio`
   |
help: consider importing this module
   |
17 + use std::io;
   |
help: if you import `io`, refer to it directly
   |
30 -     let stdin = tokio::io::stdin();
30 +     let stdin = io::stdin();
   |

error[E0752]: `main` function is not allowed to be `async`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:24:1
   |
24 | async fn main() -> Result<(), tokio_websockets::Error> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` function is not allowed to be `async`

error[E0433]: failed to resolve: use of undeclared crate or module `tokio_websockets`
  --> src/concurrency/async-exercises/chat-async/src/bin/client.rs:24:31
   |
24 | async fn main() -> Result<(), tokio_websockets::Error> {
   |                               ^^^^^^^^^^^^^^^^ use of undeclared crate or module `tokio_websockets`

error: aborting due to 10 previous errors

Some errors have detailed explanations: E0432, E0433, E0752.
For more information about an error, try `rustc --explain E0432`.

complex code

A solution should also take into account e.g. mdbook-course and other directories that are not as simple.

Other things

I also noticed that clippy-driver leaves some trash behind, etc. a binary dining-philosophers", whendprint` is checking that example it also gives an error that is not explaining a lot:

dprint check "src/concurrency/sync-exercises/dining-philosophers.rs" 
Error formatting /home/kerscher/workspace/comprehensive-rust/src/concurrency/sync-exercises/dining-philosophers.rs. Message: The original file text was greater than 100 characters, but the formatted text was empty. Perhaps dprint-plugin-exec has been misconfigured?
Had 1 error formatting.

Additionally it feels like abusing dprint a bit here. dprint is a formatting tool while clippy is a code linter. What is dprint fmt doing?

cargo clippy

the workspace Cargo.toml already defines all members of the workspace that are relevant.
We could use the workspace.lints-table to define what clippy rules we want and a cargo clippy run at the workspace level would check all the projects in that workspace

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant