Skip to content

Commit c01818b

Browse files
committed
feat: add comprehensive clippy support for Rust WASM components
Add full clippy (Rust linter) integration to enable code quality checks across all Rust targets in the project. Features added: - Global clippy configuration via .bazelrc with --config=clippy flag - Strict linting enabled by default (warnings, all, correctness, style, complexity, perf) - New rust_wasm_component_clippy() rule for targeted clippy runs - rust_clippy_all() helper for running clippy on multiple targets - CI integration to run clippy on all pull requests - Convenience script at scripts/clippy.sh Configuration: - Enable globally: bazel build --config=clippy //... - Run on specific target: bazel test //path:target_clippy - Customize via .bazelrc or per-target attributes Documentation: - Added comprehensive clippy guide at docs/clippy.md - Updated example BUILD files to show clippy usage This ensures consistent code quality and catches common Rust issues early in the development process.
1 parent bce28d8 commit c01818b

File tree

9 files changed

+208
-3
lines changed

9 files changed

+208
-3
lines changed

.bazelrc

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,9 @@ build:wasi --platforms=@rules_rust//rust/platform:wasm32-wasi
2929
# Performance
3030
build --jobs=auto
3131
build --local_resources=memory=HOST_RAM*.8
32-
build --local_resources=cpu=HOST_CPUS*.8
32+
build --local_resources=cpu=HOST_CPUS*.8
33+
34+
# Clippy configuration - only enable with --config=clippy
35+
build:clippy --aspects=@rules_rust//rust:defs.bzl%rust_clippy_aspect
36+
build:clippy --output_groups=+clippy_checks
37+
build:clippy --@rules_rust//:clippy_flags=-D,warnings,-D,clippy::all,-D,clippy::correctness,-D,clippy::style,-D,clippy::complexity,-D,clippy::perf

.github/workflows/ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ jobs:
5151
wasm32-wasip2
5252
wasm32-unknown-unknown
5353
override: true
54+
components: clippy
5455

5556
- name: Install WASM tools
5657
run: |
@@ -65,6 +66,9 @@ jobs:
6566
- name: Run Tests
6667
run: bazel test //... --test_output=errors
6768

69+
- name: Run Clippy
70+
run: bazel build --config=clippy //...
71+
6872
- name: Build Examples
6973
run: |
7074
bazel build //examples/basic:hello_component

docs/clippy.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Clippy Support
2+
3+
This project includes built-in support for running [Clippy](https://github.com/rust-lang/rust-clippy), the Rust linter, on all Rust WASM components.
4+
5+
## Running Clippy
6+
7+
### On All Targets
8+
9+
To run clippy on all Rust targets in the project:
10+
11+
```bash
12+
bazel build --config=clippy //...
13+
```
14+
15+
Or use the provided script:
16+
17+
```bash
18+
./scripts/clippy.sh
19+
```
20+
21+
### On Specific Targets
22+
23+
To run clippy on a specific target, you can use the `rust_wasm_component_clippy` rule:
24+
25+
```starlark
26+
load("@rules_wasm_component//rust:defs.bzl", "rust_wasm_component", "rust_wasm_component_clippy")
27+
28+
rust_wasm_component(
29+
name = "my_component",
30+
srcs = ["src/lib.rs"],
31+
# ...
32+
)
33+
34+
rust_wasm_component_clippy(
35+
name = "my_component_clippy",
36+
target = ":my_component",
37+
)
38+
```
39+
40+
Then run:
41+
42+
```bash
43+
bazel test //path/to:my_component_clippy
44+
```
45+
46+
## Configuration
47+
48+
### Default Lints
49+
50+
By default, clippy is configured with the following lints as errors:
51+
- `warnings` - All compiler warnings
52+
- `clippy::all` - All default clippy lints
53+
- `clippy::correctness` - Code that is likely incorrect or useless
54+
- `clippy::style` - Code that should be written in a more idiomatic way
55+
- `clippy::complexity` - Code that does something simple but in a complex way
56+
- `clippy::perf` - Code that can be written more efficiently
57+
58+
### Custom Configuration
59+
60+
You can customize clippy behavior by modifying `.bazelrc`:
61+
62+
```bash
63+
# Add custom clippy flags
64+
build:clippy --@rules_rust//:clippy_flags=-D,warnings,-W,clippy::pedantic
65+
```
66+
67+
### Per-Target Configuration
68+
69+
You can also configure clippy on a per-target basis:
70+
71+
```starlark
72+
rust_wasm_component_clippy(
73+
name = "my_component_clippy",
74+
target = ":my_component",
75+
tags = ["manual"], # Don't run with //...
76+
)
77+
```
78+
79+
## CI Integration
80+
81+
Clippy is automatically run in CI for all pull requests. To ensure your code passes CI:
82+
83+
1. Run clippy locally before pushing: `bazel build --config=clippy //...`
84+
2. Fix any issues reported by clippy
85+
3. Commit your changes
86+
87+
## Suppressing Lints
88+
89+
If you need to suppress a specific lint:
90+
91+
```rust
92+
// Suppress for entire file
93+
#![allow(clippy::specific_lint)]
94+
95+
// Suppress for specific code block
96+
#[allow(clippy::specific_lint)]
97+
fn my_function() {
98+
// ...
99+
}
100+
```
101+
102+
However, please use suppressions sparingly and document why they're necessary.

examples/basic/BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""Basic example of building a WASM component"""
22

33
load("@rules_wasm_component//wit:defs.bzl", "wit_library")
4-
load("@rules_wasm_component//rust:defs.bzl", "rust_wasm_component", "rust_wasm_component_test")
4+
load("@rules_wasm_component//rust:defs.bzl", "rust_wasm_component", "rust_wasm_component_test", "rust_wasm_component_clippy")
55

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

@@ -27,4 +27,10 @@ rust_wasm_component(
2727
rust_wasm_component_test(
2828
name = "hello_component_test",
2929
component = ":hello_component",
30+
)
31+
32+
# Run clippy on the component
33+
rust_wasm_component_clippy(
34+
name = "hello_component_clippy",
35+
target = ":hello_component",
3036
)

rust/clippy.bzl

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""Clippy configuration and rules for Rust WASM components"""
2+
3+
load("@rules_rust//rust:defs.bzl", "rust_clippy")
4+
5+
def rust_wasm_component_clippy(name, target, **kwargs):
6+
"""Run clippy on a rust_wasm_component target.
7+
8+
Args:
9+
name: Name of the clippy test target
10+
target: The rust_wasm_component target to run clippy on
11+
**kwargs: Additional arguments passed to rust_clippy
12+
"""
13+
rust_clippy(
14+
name = name,
15+
deps = [target],
16+
**kwargs
17+
)
18+
19+
def rust_clippy_all(name, targets, **kwargs):
20+
"""Run clippy on multiple Rust targets.
21+
22+
Args:
23+
name: Name of the test suite
24+
targets: List of Rust targets to run clippy on
25+
**kwargs: Additional arguments passed to test_suite
26+
"""
27+
clippy_targets = []
28+
for target in targets:
29+
clippy_name = "{}_clippy".format(target.split(":")[-1])
30+
rust_clippy(
31+
name = clippy_name,
32+
deps = [target],
33+
)
34+
clippy_targets.append(":" + clippy_name)
35+
36+
native.test_suite(
37+
name = name,
38+
tests = clippy_targets,
39+
**kwargs
40+
)

rust/defs.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ load(
88
"//rust:rust_wasm_component_test.bzl",
99
_rust_wasm_component_test = "rust_wasm_component_test",
1010
)
11+
load(
12+
"//rust:clippy.bzl",
13+
_rust_wasm_component_clippy = "rust_wasm_component_clippy",
14+
_rust_clippy_all = "rust_clippy_all",
15+
)
1116

1217
# Re-export public rules
1318
rust_wasm_component = _rust_wasm_component
14-
rust_wasm_component_test = _rust_wasm_component_test
19+
rust_wasm_component_test = _rust_wasm_component_test
20+
rust_wasm_component_clippy = _rust_wasm_component_clippy
21+
rust_clippy_all = _rust_clippy_all

scripts/clippy.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
# Run clippy on all Rust targets in the project
3+
4+
set -e
5+
6+
echo "Running clippy on all Rust targets..."
7+
8+
# Run clippy using the clippy configuration
9+
bazel build --config=clippy //...
10+
11+
echo "Clippy checks completed successfully!"

test/clippy/BUILD.bazel

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Test clippy configuration"""
2+
3+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_clippy")
4+
5+
rust_library(
6+
name = "test_lib",
7+
srcs = ["test.rs"],
8+
edition = "2021",
9+
)
10+
11+
rust_clippy(
12+
name = "test_lib_clippy",
13+
deps = [":test_lib"],
14+
)

test/clippy/test.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//! Test file for clippy
2+
3+
#[allow(dead_code)]
4+
fn main() {
5+
// This should trigger clippy warnings if not suppressed
6+
let x = 1;
7+
let y = 1;
8+
if x == 1 && y == 1 {
9+
println!("test");
10+
}
11+
}
12+
13+
#[allow(dead_code)]
14+
fn unused_function() {
15+
// This would normally trigger dead_code warning
16+
}

0 commit comments

Comments
 (0)