-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Deny warnings #47
Deny warnings #47
Changes from 4 commits
d827cec
b06eacd
8c0af98
81d5d4d
f08e132
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
# `#![deny(warnings)]` | ||
|
||
## Description | ||
|
||
A well-intentioned crate author wants to ensure their code builds without | ||
warnings. So they annotate their crate root with | ||
|
||
## Example | ||
|
||
```rust | ||
#![deny(warnings)] | ||
|
||
// All is well. | ||
``` | ||
|
||
## Advantages | ||
|
||
It is short and will stop the build if anything is amiss. | ||
|
||
## Drawbacks | ||
|
||
By disallowing the compiler to build with warnings, a crate author opts out of | ||
Rust's famed stability. Sometimes new features or old misfeatures need a change | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would like an example of such a change -- any lint from the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. added. |
||
in how things are done, thus lints are written that `warn` for a certain grace | ||
period before being turned to `deny`. Sometimes APIs get deprecated, so their | ||
use will emit a warning where before there was none. | ||
|
||
ALl this conspires to potentially break the build whenever something changes – | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: "ALl" should be "All" |
||
not only for the original developer, but also for those who wish to use their | ||
code. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't true: Cargo passes |
||
|
||
Furthermore, crates that supply additional lints (e.g. [rust-clippy]) can no | ||
longer be used unless the annotation is removed. | ||
|
||
## Alternatives | ||
|
||
luckily, there are two ways of tackling this problem: First, we can decouple | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the word "luckily" should be removed. These articles tend to have a sparse writing style, which discourages extra words like this. |
||
the build setting from the code, and second, we can name the lints we want to | ||
deny explicitly. | ||
|
||
The following command line will build with all warnings set to `deny`: | ||
|
||
```RUSTFLAGS="-D warnings" cargo build"``` | ||
|
||
This can be done by any individual developer (or be set in a CI tool like | ||
travis, but remember that this may break the build when something changes) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: "Travis" should be capitalized |
||
without requiring a change to the code. | ||
|
||
Alternatively, we can specify the lints that we want to `deny` in the code. | ||
Here is a list of warning lints that is (hopefully) safe to deny: | ||
|
||
```rust | ||
#[deny(bad-style, | ||
const-err, | ||
dead-code, | ||
extra-requirement-in-impl, | ||
improper-ctypes, | ||
legacy-directory-ownership, | ||
non-shorthand-field-patterns, | ||
no-mangle-generic-items, | ||
overflowing-literals, | ||
path-statements , | ||
patterns-in-fns-without-body, | ||
plugin-as-library, | ||
private-in-public, | ||
private-no-mangle-fns, | ||
private-no-mangle-statics, | ||
raw-pointer-derive, | ||
safe-extern-statics, | ||
unconditional-recursion, | ||
unions-with-drop-fields, | ||
unused, | ||
unused-allocation, | ||
unused-comparisons, | ||
unused-parens, | ||
while-true)] | ||
``` | ||
|
||
In addition, the following `allow`ed lints may be a good idea to `deny`: | ||
|
||
```rust | ||
#[deny(missing-docs, | ||
trivial-casts, | ||
trivial-numeric-casts, | ||
unused-extern-crates, | ||
unused-import-braces, | ||
unused-qualifications, | ||
unused-results)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd add There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
``` | ||
|
||
Note that we explicitly did not set the [deprecate attribute] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: there should be a period at the end of this sentence |
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add some discussion around the fact that usually in Rust, we recommend using in-crate attributes rather than command line flags (several reasons for this, starting with ensuring consistent build experiences without needing a build script on top of Cargo or whatever). However, the advice here is an exception to that rule and maybe explain why it makes sense (which I guess comes down to, deny(warnings) whould be a personal part of the build experience, not something you force onto your clients). It is probably worth stating the alternative of denying specific warnings as attributes which does not have the behaviour you describe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Is this true in general? Even when only specific warnings are chosen, the meanings of the warnings themselves can change over time. That was part of the motivation for rust-lang/rfcs#1193, after all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would guess it would be 99% true. Warnings very rarely change their meaning. Though they might expand slightly, it is usually to cover edge cases and is unlikely to cause breakage. I expect that any problems this causes is more than outweighed by the benefits of catching errors earlier in the development process and without needing a custom script to run the builds. ISTM that the hazard of the lint case is another lint silently appearing and this approach does not have that problem. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All right. I think the list of lints is now complete and usable. |
||
## See also | ||
|
||
- [deprecate attribute] documentation | ||
- Type `rustc -W help` for a list of lints on your system. Also type | ||
`rustc --help` for a general list of options | ||
- [rust-clippy] is a collection of lints for better Rust code | ||
|
||
[rust-clippy]: https://github.com/Manishearth/rust-clippy | ||
[deprecate attribute]: https://doc.rust-lang.org/reference.html#miscellaneous-attributes |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reading the rendered view, it's not obvious that the next example finishes this sentence. I'd rather have this written out in full instead.