Global configuration of Clippy

Hi,

I am new to rust and still learning. I was going through a video by NoBoilerplate about their Rust toolkit recommendations.

[Build your Rust lightsaber (my Rust toolkit recommendations) - YouTube](Video Here) and [https://github.com/0atman/noboilerplate/blob/main/scripts/Build%20Your%20Rust%20lightsaber.md](Transcript Here)

They mention configuring clippy to be as strict as possible, giving the following recommendation:

cargo clippy --fix -- \
-W clippy::pedantic \
-W clippy::nursery \
-W clippy::unwrap_used \
-W clippy::expect_used

I was wondering:

  1. If there is a way to set these setting globally so I don't have to do it per project
  2. Or is there a way to generate project skeletons where such defaults are set.

For some of my projects, I've just used github template repos on my account to use. Is that a good way to go?

Thanks.

One way to set up these options globally is to create a .cargo/config.toml file in your home directory. In that file you can define aliases for cargo commands that can be used in any project. For example:

[alias]
nag = "clippy -- -W clippy::pedantic -W clippy::nursery -W clippy::unwrap_used -W clippy::expect_used"
fixnags = "clippy --fix -- -W clippy::pedantic -W clippy::nursery -W clippy::unwrap_used -W clippy::expect_used"

To use an alias, run cargo nag, for example.

If you also have a .cargo/config.toml file in a project directory, those settings will override global setting within that project. You could certainly create this file as part of a project skeleton.

I bet project templates are useful in some cases (e.g. wasm), but for learning Rust, I've found that I'm mostly just typing cargo new when I want to try something out.

Thank you. That is really helpful.

You should probably read what each of these lint do before enabling them blindly.

Read:

Those lints are disabled by default for good reasons for most people and most projects.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.