Skip to content

Commit

Permalink
Allow the 'unused' category of lints (google#2571)
Browse files Browse the repository at this point in the history
These sort of warnings can be distracting when commenting out a few
lines of code or demonstrating some other concept. They can be
re-enabled for a code block with `warnunused`.

I filed rust-lang/mdBook#2527 to get behavior
like this upstream.
  • Loading branch information
djmitche authored Jan 20, 2025
1 parent 9fa1b64 commit 5f7e0c3
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/generics/trait-bounds.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn duplicate<T: Clone>(a: T) -> (T, T) {
(a.clone(), a.clone())
}
// struct NotCloneable;
struct NotCloneable;
fn main() {
let foo = String::from("foo");
Expand Down
14 changes: 5 additions & 9 deletions src/testing/lints.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ The Rust compiler produces fantastic error messages, as well as helpful built-in
lints. [Clippy](https://doc.rust-lang.org/clippy/) provides even more lints,
organized into groups that can be enabled per-project.

```rust,editable,should_panic
```rust,editable,should_panic,warnunused
#[deny(clippy::cast_possible_truncation)]
fn main() {
let x = 3;
let mut x = 3;
while (x < 70000) {
x *= 2;
}
Expand All @@ -21,13 +21,9 @@ fn main() {

<details>

Run the code sample and examine the error message. There are also lints visible
here, but those will not be shown once the code compiles. Switch to the
Playground site to show those lints.

After resolving the lints, run `clippy` on the playground site to show clippy
warnings. Clippy has extensive documentation of its lints, and adds new lints
(including default-deny lints) all the time.
There are compiler lints visible here, but not clippy lints. Run `clippy` on the
playground site to show clippy warnings. Clippy has extensive documentation of
its lints, and adds new lints (including default-deny lints) all the time.

Note that errors or warnings with `help: ...` can be fixed with `cargo fix` or
via your editor.
Expand Down
6 changes: 5 additions & 1 deletion src/types-and-values/variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ minutes: 5
Rust provides type safety via static typing. Variable bindings are made with
`let`:

```rust,editable
```rust,editable,warnunused
fn main() {
let x: i32 = 10;
println!("x: {x}");
Expand All @@ -21,6 +21,10 @@ fn main() {
- Uncomment the `x = 20` to demonstrate that variables are immutable by default.
Add the `mut` keyword to allow changes.

- Warnings are enabled for this slide, such as for unused variables or
unnecessary `mut`. These are omitted in most slides to avoid distracting
warnings. Try removing the mutation but leaving the `mut` keyword in place.

- The `i32` here is the type of the variable. This must be known at compile
time, but type inference (covered later) allows the programmer to omit it in
many cases.
Expand Down
5 changes: 5 additions & 0 deletions theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ function playground_text(playground, hidden = true) {

let text = playground_text(code_block);
let classes = code_block.querySelector('code').classList;
// Unless the code block has `warnunused`, allow all "unused" lints to avoid cluttering
// the output.
if(!classes.contains("warnunused")) {
text = '#![allow(unused)]\n' + text;
}
let edition = "2015";
if(classes.contains("edition2018")) {
edition = "2018";
Expand Down

0 comments on commit 5f7e0c3

Please sign in to comment.