14

I have an application split into several crates. I want to deny or allow a specific lint in all crates. For example:

#![deny(clippy::print_stdout)]

It seems I have to add this to lib.rs in each of the crates.

There is an ticket with Cargo to allow configuring this somehow, but it has been open for several years with no clear conclusion.

Is there a workaround to avoid having these allow/deny/warn lines duplicated per crate?

One idea I had was to include! the lines by creating a clippy_config.rs at the workspace root, then in each crate's lib.rs adding

include!("../../clippy_config.rs");

However this fails with

error: an inner attribute is not permitted in this context
 --> app/src/../../clippy_config.rs:1:1
  |
1 | #![deny(clippy::print_stdout)]
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  |
  = note: inner attributes, like `#![no_std]`, annotate the item enclosing them, and are usually found at the beginning of source files. Outer attributes, like `#[test]`, annotate the item following them.

My other thought to use a macro also does not work for much the same reason.

Is there a simple way to do this, except writing an external script to modify the Rust files to automate the duplication? (as mentioned in this comment describing Embark Studio's setup).

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
dbr
  • 165,801
  • 69
  • 278
  • 343

1 Answers1

8

Clippy offers 3 configuration modes:

  • Attributes: #[deny(clippy::print_stdout)]
  • Flags: -Dclippy::print-stdout
  • Configuration file: clippy.toml, though restricted to a subset of configurable lints.

For project-wide configuration, across crates, the configuration file is the best option if it works.

Otherwise, the second best (hacky) option is to use flags indirectly. That is, rather than specifying RUSTFLAGS=-Dclippy::print-stdout in front of your compiler invocation, you can let Cargo do it, and configure Cargo project-wide.

At the root of your project, create a .cargo/config.toml file with the following content:

[build]
rustflags = ["-Dclippy::print-stdout"]

Cargo will then pass this flag to clippy when it invokes it.

Note: in a workspace setting, .cargo/config.toml in individual crate folders is ignored when invoking Cargo from the root of the workspace, so best place this in the .cargo at the root.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • In a workspace setting, `clippy.toml` in the root of the workspace is (as of this writing) ignored when invoking `cargo clippy` from within the individual crate folder. I've worked around this by creating symlinks to `workspace_root/clippy.toml` in each crate folder. – Jasha Mar 19 '23 at 03:39
  • 1
    Note only a small subset of clippy lints can be configured via `clippy.toml`. – Jasha Mar 19 '23 at 03:43