Skip to content

Instantly share code, notes, and snippets.

@kmcallister
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmcallister/3409ece44ead6d280b8e to your computer and use it in GitHub Desktop.
Save kmcallister/3409ece44ead6d280b8e to your computer and use it in GitHub Desktop.
Lint plugin demo

Using my lint branch and the example lint from my RFC to compile this program:

#![feature(phase)]

#[phase(plugin)]
extern crate lipogram;

fn hello() { }

fn main() { hello() }

The result is:

$ rustc lipogram.rs

$ rustc -L . test.rs
test.rs:6:1: 6:15 warning: item name contains the letter 'e', #[warn(letter_e)] on by default
test.rs:6 fn hello() { }
          ^~~~~~~~~~~~~~

Lint plugins work with the usual attributes:

$ (echo '#![allow(letter_e)]'; cat test.rs) | rustc -L . -

$ (echo '#![deny(letter_e)]'; cat test.rs) | rustc -L . -
<anon>:7:1: 7:15 error: item name contains the letter 'e'
<anon>:7 fn hello() { }
         ^~~~~~~~~~~~~~
<anon>:1:9: 1:17 note: lint level defined here
<anon>:1 #![deny(letter_e)]
                 ^~~~~~~~
error: aborting due to previous error

And they work with command-line flags:

$ rustc -L . test.rs -A letter-e

$ rustc -L . test.rs -D letter-e
test.rs:6:1: 6:15 error: item name contains the letter 'e' [-D letter-e]
test.rs:6 fn hello() { }
          ^~~~~~~~~~~~~~
error: aborting due to previous error

With no crate filename, -W help lists builtin lints:

$ rustc -W help
...
Lint checks provided by rustc:

                             name  default  meaning
                             ----  -------  -------
          deprecated-owned-vector  allow    use of a `~[T]` vector
                      heap-memory  allow    use of any (Box type or @ type) heap memory
...
               unknown-crate-type  deny     unknown crate type found in #[crate_type] directive
                 unknown-features  deny     unknown features found in crate-level #[feature] directives


Compiler plugins can provide additional lints. To see a listing of these, re-run `rustc -W help` with a crate filename.

With a crate filename, it shows plugins too:

$ rustc -W help -L . test.rs
...
Lint checks provided by rustc:

                             name  default  meaning
                             ----  -------  -------
          deprecated-owned-vector  allow    use of a `~[T]` vector
                      heap-memory  allow    use of any (Box type or @ type) heap memory
...
               unknown-crate-type  deny     unknown crate type found in #[crate_type] directive
                 unknown-features  deny     unknown features found in crate-level #[feature] directives


Lint checks provided by plugins loaded by this crate:

                         letter-e  warn     forbid use of the letter 'e'

or indicates that there aren't any:

$ echo 'fn main() { }' | rustc -W help -
...
Lint checks provided by rustc:

                             name  default  meaning
                             ----  -------  -------
          deprecated-owned-vector  allow    use of a `~[T]` vector
                      heap-memory  allow    use of any (Box type or @ type) heap memory
...
               unknown-crate-type  deny     unknown crate type found in #[crate_type] directive
                 unknown-features  deny     unknown features found in crate-level #[feature] directives


This crate does not load any lint plugins.

Lint flags are checked as soon as possible:

$ rustc -Z time-passes -L . test.rs -A zzzz
time: 0.001 s   parsing
time: 0.000 s   gated feature checking
time: 0.000 s   crate injection
time: 0.000 s   configuration 1
time: 0.038 s   plugin loading
time: 0.000 s   plugin registration
error: unknown allow flag: zzzz
error: aborting due to previous error
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment