|
| 1 | +# `#![deny(warnings)]` |
| 2 | + |
| 3 | +## Description |
| 4 | + |
| 5 | +A well-intentioned crate author wants to ensure their code builds without |
| 6 | +warnings. So they annotate their crate root with |
| 7 | + |
| 8 | +## Example |
| 9 | + |
| 10 | +```rust |
| 11 | +#![deny(warnings)] |
| 12 | + |
| 13 | +// All is well. |
| 14 | +``` |
| 15 | + |
| 16 | +## Advantages |
| 17 | + |
| 18 | +It is short and will stop the build if anything is amiss. |
| 19 | + |
| 20 | +## Drawbacks |
| 21 | + |
| 22 | +By disallowing the compiler to build with warnings, a crate author opts out of |
| 23 | +Rust's famed stability. Sometimes new features or old misfeatures need a change |
| 24 | +in how things are done, thus lints are written that `warn` for a certain grace |
| 25 | +period before being turned to `deny`. Sometimes APIs get deprecated, so their |
| 26 | +use will emit a warning where before there was none. |
| 27 | + |
| 28 | +ALl this conspires to potentially break the build whenever something changes – |
| 29 | +not only for the original developer, but also for those who wish to use their |
| 30 | +code. |
| 31 | + |
| 32 | +Furthermore, crates that supply additional lints (e.g. [rust-clippy]) can no |
| 33 | +longer be used unless the annotation is removed. |
| 34 | + |
| 35 | +## Alternatives |
| 36 | + |
| 37 | +luckily, there are two ways of tackling this problem: First, we can decouple |
| 38 | +the build setting from the code, and second, we can name the lints we want to |
| 39 | +deny explicitly. |
| 40 | + |
| 41 | +The following command line will build with all warnings set to `deny`: |
| 42 | + |
| 43 | +```RUSTFLAGS="-D warnings" cargo build"``` |
| 44 | + |
| 45 | +This can be done by any individual developer (or be set in a CI tool like |
| 46 | +travis, but remember that this may break the build when something changes) |
| 47 | +without requiring a change to the code. |
| 48 | + |
| 49 | +Alternatively, we can specify the lints that we want to `deny` in the code. |
| 50 | +Here is a list of warning lints that is (hopefully) safe to deny: |
| 51 | + |
| 52 | +```rust |
| 53 | +#[deny(bad-style, |
| 54 | + const-err, |
| 55 | + dead-code, |
| 56 | + extra-requirement-in-impl, |
| 57 | + improper-ctypes, |
| 58 | + legacy-directory-ownership, |
| 59 | + non-shorthand-field-patterns, |
| 60 | + no-mangle-generic-items, |
| 61 | + overflowing-literals, |
| 62 | + path-statements , |
| 63 | + patterns-in-fns-without-body, |
| 64 | + plugin-as-library, |
| 65 | + private-in-public, |
| 66 | + private-no-mangle-fns, |
| 67 | + private-no-mangle-statics, |
| 68 | + raw-pointer-derive, |
| 69 | + safe-extern-statics, |
| 70 | + unconditional-recursion, |
| 71 | + unions-with-drop-fields, |
| 72 | + unused, |
| 73 | + unused-allocation, |
| 74 | + unused-comparisons, |
| 75 | + unused-parens, |
| 76 | + while-true)] |
| 77 | +``` |
| 78 | + |
| 79 | +In addition, the following `allow`ed lints may be a good idea to `deny`: |
| 80 | + |
| 81 | +```rust |
| 82 | +#[deny(missing-docs, |
| 83 | + trivial-casts, |
| 84 | + trivial-numeric-casts, |
| 85 | + unused-extern-crates, |
| 86 | + unused-import-braces, |
| 87 | + unused-qualifications, |
| 88 | + unused-results)] |
| 89 | +``` |
| 90 | + |
| 91 | +Note that we explicitly did not set `deprecated`. |
| 92 | + |
| 93 | +## See also |
| 94 | + |
| 95 | +- Type `rustc -W help` for a list of lints on your system. Also type |
| 96 | +`rustc --help` for a general list of options |
| 97 | +- [rust-clippy] is a collection of lints for better Rust code |
| 98 | + |
| 99 | +[rust-clippy]: https://github.com/Manishearth/rust-clippy |
0 commit comments