Skip to content

Add doc and examples for attributes allow/warn/deny/forbid. #7823

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 71 additions & 2 deletions doc/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -1417,14 +1417,83 @@ names are effectively reserved. Some significant attributes include:
* The `lang` attribute, for custom definitions of traits and functions that are known to the Rust compiler (see [Language items](#language-items)).
* The `link` attribute, for describing linkage metadata for a crate.
* The `test` attribute, for marking functions as unit tests.
* The `allow`, `warn`, `forbid`, and `deny` attributes, for controlling lint checks. Lint checks supported
by the compiler can be found via `rustc -W help`.
* The `allow`, `warn`, `forbid`, and `deny` attributes, for
controlling lint checks (see [Lint check attributes](#lint-check-attributes)).
* The `deriving` attribute, for automatically generating
implementations of certain traits.
* The `static_assert` attribute, for asserting that a static bool is true at compiletime

Other attributes may be added or removed during development of the language.

### Lint check attributes

A lint check names a potentially undesirable coding pattern, such as
unreachable code or omitted documentation, for the static entity to
which the attribute applies.

For any lint check `C`:

* `warn(C)` warns about violations of `C` but continues compilation,
* `deny(C)` signals an error after encountering a violation of `C`,
* `allow(C)` overrides the check for `C` so that violations will go
unreported,
* `forbid(C)` is the same as `deny(C)`, but also forbids uses of
`allow(C)` within the entity.

The lint checks supported by the compiler can be found via `rustc -W help`,
along with their default settings.

~~~{.xfail-test}
mod m1 {
// Missing documentation is ignored here
#[allow(missing_doc)]
pub fn undocumented_one() -> int { 1 }

// Missing documentation signals a warning here
#[warn(missing_doc)]
pub fn undocumented_too() -> int { 2 }

// Missing documentation signals an error here
#[deny(missing_doc)]
pub fn undocumented_end() -> int { 3 }
}
~~~

This example shows how one can use `allow` and `warn` to toggle
a particular check on and off.

~~~
#[warn(missing_doc)]
mod m2{
#[allow(missing_doc)]
mod nested {
// Missing documentation is ignored here
pub fn undocumented_one() -> int { 1 }

// Missing documentation signals a warning here,
// despite the allow above.
#[warn(missing_doc)]
pub fn undocumented_two() -> int { 2 }
}

// Missing documentation signals a warning here
pub fn undocumented_too() -> int { 3 }
}
~~~

This example shows how one can use `forbid` to disallow uses
of `allow` for that lint check.

~~~{.xfail-test}
#[forbid(missing_doc)]
mod m3 {
// Attempting to toggle warning signals an error here
#[allow(missing_doc)]
/// Returns 2.
pub fn undocumented_too() -> int { 2 }
}
~~~

### Language items

Some primitive Rust operations are defined in Rust code,
Expand Down