Skip to content

Commit 2e60553

Browse files
committed
add examples of extracting lints from Cargo
* adds standalone example using WORKSPACE * extends existing example using bzlmod
1 parent b92dc48 commit 2e60553

File tree

9 files changed

+89
-1
lines changed

9 files changed

+89
-1
lines changed

cargo/private/cargo_lints.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ extract_cargo_lints = rule(
6767
"_cargo_toml_info": attr.label(
6868
allow_single_file = True,
6969
executable = True,
70-
default = "@rules_rust//cargo/private/cargo_toml_info",
70+
default = Label("//cargo/private/cargo_toml_info:cargo_toml_info"),
7171
cfg = "exec",
7272
),
7373
},

examples/WORKSPACE.bazel

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")
2828

2929
rust_analyzer_dependencies()
3030

31+
load("@rules_rust//cargo:deps.bzl", "cargo_dependencies")
32+
33+
cargo_dependencies()
34+
3135
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
3236

3337
# We need to load rules_java before anything proto-related happens because otherwise it will pull in its own rules_java which isn't compatible with rules_jvm_external.

examples/bazel_env/rust/hello_world/BUILD.bazel

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
load("@crates//:defs.bzl", "all_crate_deps")
2+
load("@rules_rust//cargo:defs.bzl", "extract_cargo_lints")
23
load("@rules_rust//rust:defs.bzl", "rust_binary")
34

45
package(default_visibility = ["//visibility:public"])
@@ -8,8 +9,14 @@ exports_files([
89
"src/main.rs",
910
])
1011

12+
extract_cargo_lints(
13+
name = "cargo_lints",
14+
manifest = "Cargo.toml",
15+
)
16+
1117
rust_binary(
1218
name = "hello_world",
1319
srcs = ["src/main.rs"],
20+
lint_config = ":cargo_lints",
1421
deps = all_crate_deps(normal = True),
1522
)

examples/bazel_env/rust/hello_world/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ publish = false
66

77
[lib]
88
path = "fake.rs"
9+
10+
[lints.rust]
11+
dead_code = "allow"

examples/bazel_env/rust/hello_world/src/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,9 @@
1515
fn main() {
1616
println!("Hello, world!");
1717
}
18+
19+
// Note: Would normally cause Rustc to lint `dead_code` but we allow this lint in the Cargo.toml of
20+
// this crate.
21+
fn unused(x: usize) -> String {
22+
format!("I am unused {x}")
23+
}

examples/cargo_lints/BUILD.bazel

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
load("@rules_rust//cargo:defs.bzl", "extract_cargo_lints")
2+
load("@rules_rust//rust:defs.bzl", "rust_clippy", "rust_doc", "rust_library")
3+
4+
package(default_visibility = ["//visibility:public"])
5+
6+
extract_cargo_lints(
7+
name = "hello_world_lints",
8+
manifest = "Cargo.toml",
9+
)
10+
11+
rust_library(
12+
name = "hello_world",
13+
srcs = ["src/lib.rs"],
14+
edition = "2021",
15+
lint_config = ":hello_world_lints",
16+
)
17+
18+
rust_clippy(
19+
name = "hello_world_clippy",
20+
deps = [":hello_world"],
21+
)
22+
23+
rust_doc(
24+
name = "hello_world_doc",
25+
crate = ":hello_world",
26+
)

examples/cargo_lints/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cargo_lints/Cargo.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[package]
2+
name = "hello_world"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
8+
9+
[lints.rust]
10+
dead_code = "allow"
11+
12+
[lints.clippy]
13+
absurd_extreme_comparisons = "allow"
14+
needless_if = "allow"
15+
16+
[lints.rustdoc]
17+
invalid_html_tags = "allow"

examples/cargo_lints/src/lib.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! This module contains code that would normally lint for Rust, Clippy, or Rustdoc, but we
2+
//! explicitly 'allow' said lints in the Cargo.toml of this crate.
3+
4+
/// Would trigger Rustdoc's `invalid_html_tags` lint.
5+
///
6+
/// <h1>
7+
/// </script>
8+
pub fn add(a: usize, b: usize) -> usize {
9+
// Would trigger Clippy's `absurd_extreme_comparisons` and `needless_if` lints.
10+
if 100 > i32::MAX {}
11+
12+
a + b
13+
}
14+
15+
// Would trigger Rust's `dead_code` lint.
16+
fn sub(a: usize, b: usize) -> usize {
17+
a - b
18+
}

0 commit comments

Comments
 (0)