Skip to content

Add per-target clippy_config attribute to Rust rules#4007

Open
zertosh wants to merge 1 commit intobazelbuild:mainfrom
zertosh:asuarez/clippy_config
Open

Add per-target clippy_config attribute to Rust rules#4007
zertosh wants to merge 1 commit intobazelbuild:mainfrom
zertosh:asuarez/clippy_config

Conversation

@zertosh
Copy link
Copy Markdown

@zertosh zertosh commented Apr 30, 2026

Problem

rules_rust exposes the clippy configuration file only via the global
@rules_rust//rust/settings:clippy.toml build setting. There is no way
to attach a different clippy.toml to a specific target.

In a large workspace, a root clippy.toml is often supplemented by
per-subtree configs that scope disallowed-methods, disallowed-types,
and similar config-only lints to specific subsystems. Under Cargo,
clippy-driver walks parent directories from CARGO_MANIFEST_DIR and
finds the nearest config. Under Bazel only the global file applies, so
those subtree-specific lints never fire.

Solution

Add a clippy_config attribute to the common Rust rule attributes
(rust_library, rust_binary, rust_test, rust_shared_library,
rust_static_library, rust_proc_macro). When set, rust_clippy_aspect
points CLIPPY_CONF_DIR at this file's directory instead of the global
build setting. The existing basename validation and input registration
in rust_clippy_action are reused unchanged.

An alternative was to extend rust_lint_config with a clippy_toml
attribute populating LintsInfo.clippy_lint_files. That keeps the
config on the existing lint_config attribute, but CLIPPY_CONF_DIR
accepts a single directory so the aspect would still pick exactly one
file, and the indirection makes BUILD files less obvious than
clippy_config = ":clippy.toml" on the target.

Testing

  • Analysis tests in //test/unit/clippy assert CLIPPY_CONF_DIR
    resolves to the global default when clippy_config is unset, to the
    per-target directory when set, that a rust_test(crate = ...) does
    not inherit the setting from the crate under test, and that it honors
    its own clippy_config.
  • //test/clippy:clippy_failure_tester covers a per-target source
    config, a per-target generated config, and precedence of
    clippy_config over the global label flag.

This also fixes the BAD_CLIPPY_TOML label in
clippy_failure_tester.sh, which previously made the existing assertion
pass on a package-resolution error rather than the intended lint
failure.

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

Successfully merging this pull request may close these issues.

1 participant