Skip to content

Commit

Permalink
add examples of extracting lints from Cargo
Browse files Browse the repository at this point in the history
* adds standalone example using WORKSPACE
* extends existing example using bzlmod
  • Loading branch information
ParkMyCar committed Dec 14, 2024
1 parent b92dc48 commit e5acd95
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 2 deletions.
2 changes: 1 addition & 1 deletion cargo/private/cargo_lints.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ extract_cargo_lints = rule(
"_cargo_toml_info": attr.label(
allow_single_file = True,
executable = True,
default = "@rules_rust//cargo/private/cargo_toml_info",
default = Label("//cargo/private/cargo_toml_info:cargo_toml_info"),
cfg = "exec",
),
},
Expand Down
4 changes: 4 additions & 0 deletions examples/WORKSPACE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ load("@rules_rust//tools/rust_analyzer:deps.bzl", "rust_analyzer_dependencies")

rust_analyzer_dependencies()

load("@rules_rust//cargo:deps.bzl", "cargo_dependencies")

cargo_dependencies()

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

# 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.
Expand Down
21 changes: 20 additions & 1 deletion examples/bazel_env/rust/hello_world/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
load("@crates//:defs.bzl", "all_crate_deps")
load("@rules_rust//rust:defs.bzl", "rust_binary")
load("@rules_rust//cargo:defs.bzl", "extract_cargo_lints")
load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_lint_config")

package(default_visibility = ["//visibility:public"])

Expand All @@ -8,8 +9,26 @@ exports_files([
"src/main.rs",
])

rust_lint_config(
name = "lints",
rustc = {"dead_code": "allow"},
)

rust_binary(
name = "hello_world",
srcs = ["src/main.rs"],
lint_config = ":lints",
deps = all_crate_deps(normal = True),
)

extract_cargo_lints(
name = "cargo_lints",
manifest = "Cargo.toml",
)

rust_binary(
name = "hello_world_with_lints",
srcs = ["src/main.rs"],
lint_config = ":hello_world_lints",
deps = all_crate_deps(normal = True),
)
3 changes: 3 additions & 0 deletions examples/bazel_env/rust/hello_world/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ publish = false

[lib]
path = "fake.rs"

[lints.rust]
dead_code = "deny"
4 changes: 4 additions & 0 deletions examples/bazel_env/rust/hello_world/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,7 @@
fn main() {
println!("Hello, world!");
}

fn unused(x: usize) -> String {
format!("I am unused {x}")
}
26 changes: 26 additions & 0 deletions examples/cargo_lints/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
load("@rules_rust//cargo:defs.bzl", "extract_cargo_lints")
load("@rules_rust//rust:defs.bzl", "rust_clippy", "rust_doc", "rust_library")

package(default_visibility = ["//visibility:public"])

extract_cargo_lints(
name = "hello_world_lints",
manifest = "Cargo.toml",
)

rust_library(
name = "hello_world",
srcs = ["src/lib.rs"],
edition = "2021",
lint_config = ":hello_world_lints",
)

rust_clippy(
name = "hello_world_clippy",
deps = [":hello_world"],
)

rust_doc(
name = "hello_world_doc",
crate = ":hello_world",
)
7 changes: 7 additions & 0 deletions examples/cargo_lints/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions examples/cargo_lints/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "hello_world"
version = "0.1.0"
edition = "2021"

[dependencies]


[lints.rust]
unknown_lints = "warn"
dead_code = "deny"

[lints.clippy]
box_default = "deny"

[lints.rustdoc]
invalid_html_tags = "deny"
10 changes: 10 additions & 0 deletions examples/cargo_lints/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// <h1>
/// </script>
pub fn add(a: usize, b: usize) -> usize {
let _unused: Box<String> = Box::new(Default::default());
a + b
}

fn sub(a: usize, b: usize) -> usize {
a - b
}

0 comments on commit e5acd95

Please sign in to comment.