|
| 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 the following: |
| 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`. |
| 26 | + |
| 27 | +For example, it was discovered that a type could have two `impl`s with the same |
| 28 | +method. This was deemed a bad idea, but in order to make the transition smooth, |
| 29 | +the `overlapping-inherent-impls` lint was introduced to give a warning to those |
| 30 | +stumbling on this fact, before it becomes a hard error in a future release. |
| 31 | + |
| 32 | +Also sometimes APIs get deprecated, so their use will emit a warning where |
| 33 | +before there was none. |
| 34 | + |
| 35 | +All this conspires to potentially break the build whenever something changes. |
| 36 | + |
| 37 | +Furthermore, crates that supply additional lints (e.g. [rust-clippy]) can no |
| 38 | +longer be used unless the annotation is removed. |
| 39 | + |
| 40 | +## Alternatives |
| 41 | + |
| 42 | +There are two ways of tackling this problem: First, we can decouple the build |
| 43 | +setting from the code, and second, we can name the lints we want to deny |
| 44 | +explicitly. |
| 45 | + |
| 46 | +The following command line will build with all warnings set to `deny`: |
| 47 | + |
| 48 | +```RUSTFLAGS="-D warnings" cargo build"``` |
| 49 | + |
| 50 | +This can be done by any individual developer (or be set in a CI tool like |
| 51 | +Travis, but remember that this may break the build when something changes) |
| 52 | +without requiring a change to the code. |
| 53 | + |
| 54 | +Alternatively, we can specify the lints that we want to `deny` in the code. |
| 55 | +Here is a list of warning lints that is (hopefully) safe to deny: |
| 56 | + |
| 57 | +```rust |
| 58 | +#[deny(bad-style, |
| 59 | + const-err, |
| 60 | + dead-code, |
| 61 | + extra-requirement-in-impl, |
| 62 | + improper-ctypes, |
| 63 | + legacy-directory-ownership, |
| 64 | + non-shorthand-field-patterns, |
| 65 | + no-mangle-generic-items, |
| 66 | + overflowing-literals, |
| 67 | + path-statements , |
| 68 | + patterns-in-fns-without-body, |
| 69 | + plugin-as-library, |
| 70 | + private-in-public, |
| 71 | + private-no-mangle-fns, |
| 72 | + private-no-mangle-statics, |
| 73 | + raw-pointer-derive, |
| 74 | + safe-extern-statics, |
| 75 | + unconditional-recursion, |
| 76 | + unions-with-drop-fields, |
| 77 | + unused, |
| 78 | + unused-allocation, |
| 79 | + unused-comparisons, |
| 80 | + unused-parens, |
| 81 | + while-true)] |
| 82 | +``` |
| 83 | + |
| 84 | +In addition, the following `allow`ed lints may be a good idea to `deny`: |
| 85 | + |
| 86 | +```rust |
| 87 | +#[deny(missing-debug-implementations, |
| 88 | + missing-docs, |
| 89 | + trivial-casts, |
| 90 | + trivial-numeric-casts, |
| 91 | + unused-extern-crates, |
| 92 | + unused-import-braces, |
| 93 | + unused-qualifications, |
| 94 | + unused-results)] |
| 95 | +``` |
| 96 | + |
| 97 | +Some may also want to add `missing-copy-implementations` to their list. |
| 98 | + |
| 99 | +Note that we explicitly did not add the `deprecated` lint, as it is fairly |
| 100 | +certain that there will be more deprecated APIs in the future. |
| 101 | + |
| 102 | +## See also |
| 103 | + |
| 104 | +- [deprecate attribute] documentation |
| 105 | +- Type `rustc -W help` for a list of lints on your system. Also type |
| 106 | +`rustc --help` for a general list of options |
| 107 | +- [rust-clippy] is a collection of lints for better Rust code |
| 108 | + |
| 109 | +[rust-clippy]: https://github.com/Manishearth/rust-clippy |
| 110 | +[deprecate attribute]: https://doc.rust-lang.org/reference.html#miscellaneous-attributes |
0 commit comments