clippy_toolchain.bzl

clippy_toolchain

load("@rules_rust//rust:clippy_toolchain.bzl", "clippy_toolchain")

clippy_toolchain(name, cargo_clippy, channel, clippy_driver, extra_clippy_flags, iso_date,
                 rust_toolchain, version)

A toolchain providing Clippy.

rust_clippy_aspect, rust_clippy and rust_clippy_test resolve @rules_rust//rust/clippy:toolchain_type and run the clippy-driver it provides.

The toolchain derives its sysroot from the one of its rust_toolchain: the same rustc, libraries and standard library, plus bin/clippy-driver and bin/cargo-clippy. clippy-driver is a rustc driver which loads librustc_driver from beside itself, so actions running Clippy use the derived sysroot, and that sysroot is complete even where symlinks are materialized as copies.

clippy-driver must come from the same Rust release as rustc. The rust module extension declares one clippy_toolchain in the repository of every rust_toolchain it registers, bound to it through rust_toolchain, and registers the two together. A clippy_toolchain which leaves rust_toolchain unset derives from the rust_toolchain resolved for @rules_rust//rust:toolchain_type instead and states its release through version, iso_date and channel, which the resolved toolchain must agree with; the two ways exclude each other. Either way the toolchain advertises the version, iso_date and channel of the rust_toolchain it derives from. A rule which runs Clippy resolves both toolchain types and find_clippy checks that the clippy_toolchain derives from the rust_toolchain the rule resolved. When it does not, as when a rust_toolchain is registered on its own ahead of the generated ones, the rule falls back to the deprecated clippy_driver and cargo_clippy of that rust_toolchain, or fails when they are unset. Those attributes are deprecated in favor of this rule and are consulted only in that case or when no clippy_toolchain is registered at all.

$(CLIPPY_DRIVER), $(CLIPPY_DRIVER_RLOCATIONPATH) and, when cargo_clippy is set, $(CARGO_CLIPPY) and $(CARGO_CLIPPY_RLOCATIONPATH) are available as Make variables to targets which list @rules_rust//rust/toolchain:current_clippy_toolchain, a clippy_toolchain_alias, in toolchains.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
cargo_clippyThe cargo-clippy binary. Can be a direct source or a filegroup containing one item.LabeloptionalNone
channelThe Rust release channel clippy comes from (stable, nightly, or beta), as in rust_toolchain.channel.Stringoptional""
clippy_driverThe clippy-driver binary. Can be a direct source or a filegroup containing one item.Labelrequired
extra_clippy_flagsExtra flags to pass to clippy-driver. They follow the rust_toolchain's extra_rustc_flags and precede the lints of the target and the flags of the command line. Subject to Make variable expansion with respect to the rust_toolchain's variables (RUST_SYSROOT, RUSTC, ...) and CLIPPY_DRIVER.List of stringsoptional[]
iso_dateThe ISO date of the nightly or beta release clippy comes from, as in rust_toolchain.iso_date. Empty for stable releases.Stringoptional""
rust_toolchainThe rust_toolchain whose sysroot this toolchain derives its own from, normally the one declared beside it over the same release. Exclusive with version, iso_date and channel: when unset the rust_toolchain resolved for @rules_rust//rust:toolchain_type is used instead and version is required, so that the resolved toolchain can be checked to come from the same release.LabeloptionalNone
versionThe version of the Rust release clippy comes from (e.g. 1.94.1), as in rust_toolchain.version. Required when rust_toolchain is unset, and not allowed with it. The resolved rust_toolchain must agree on version, iso_date and channel wherever both state them.Stringoptional""

clippy_toolchain_alias

load("@rules_rust//rust:clippy_toolchain.bzl", "clippy_toolchain_alias")

clippy_toolchain_alias(name, mandatory)

A target standing in for the clippy_toolchain resolved for the current platform.

The alias forwards the toolchain's providers, its Make variables ($(CLIPPY_DRIVER), $(CARGO_CLIPPY), ...) and its files: the sysroot clippy-driver runs from, so that it runs from a genrule's tools on any executor. @rules_rust//rust/toolchain:current_clippy_toolchain is the alias rules_rust declares.

This follows cc_toolchain_alias from rules_cc.

ATTRIBUTES

NameDescriptionTypeMandatoryDefault
nameA unique name for this target.Namerequired
mandatoryWhether to fail when no toolchain is registered. When False, the target provides nothing instead.BooleanoptionalTrue

find_clippy

load("@rules_rust//rust:clippy_toolchain.bzl", "find_clippy")

find_clippy(ctx, rust_toolchain)

The clippy_toolchain a rule should run Clippy from.

The registered clippy_toolchain is preferred when its sysroot derives from the rust_toolchain the rule resolved. When it derives from another one, or none is registered, an equivalent toolchain is made from the deprecated clippy_driver and cargo_clippy of the rust_toolchain, running from that toolchain's sysroot, which is how rules found Clippy before @rules_rust//rust/clippy:toolchain_type existed. A clippy_toolchain of another rust_toolchain with nothing to fall back to is an error: its clippy-driver would load that other rustc's libraries.

PARAMETERS

NameDescriptionDefault Value
ctxThe rule context. The rule must list config_common.toolchain_type(rust_common.clippy_toolchain_type, mandatory = False) in toolchains.none
rust_toolchainThe resolved rust_toolchain, or None.none

RETURNS

ToolchainInfo: A clippy_toolchain, or None when neither toolchain provides Clippy.