Skip to content

Commit 9726149

Browse files
committed
Rollup merge of #29086 - fhahn:book-update-lint-plugin-example, r=alexcrichton
2 parents 75a6725 + ac097f1 commit 9726149

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

src/doc/trpl/compiler-plugins.md

+22-8
Original file line numberDiff line numberDiff line change
@@ -170,31 +170,45 @@ starting point for an improved quasiquote as an ordinary plugin library.
170170

171171
Plugins can extend [Rust's lint
172172
infrastructure](../reference.html#lint-check-attributes) with additional checks for
173-
code style, safety, etc. You can see
174-
[`src/test/auxiliary/lint_plugin_test.rs`](https://github.com/rust-lang/rust/blob/master/src/test/auxiliary/lint_plugin_test.rs)
175-
for a full example, the core of which is reproduced here:
173+
code style, safety, etc. Now let's write a plugin [`lint_plugin_test.rs`](https://github.com/rust-lang/rust/blob/master/src/test/auxiliary/lint_plugin_test.rs)
174+
that warns about any item named `lintme`.
176175

177176
```ignore
178-
declare_lint!(TEST_LINT, Warn,
179-
"Warn about items named 'lintme'");
177+
#![feature(plugin_registrar)]
178+
#![feature(box_syntax, rustc_private)]
179+
180+
extern crate syntax;
181+
182+
// Load rustc as a plugin to get macros
183+
#[macro_use]
184+
extern crate rustc;
185+
186+
use rustc::lint::{EarlyContext, LintContext, LintPass, EarlyLintPass,
187+
EarlyLintPassObject, LintArray};
188+
use rustc::plugin::Registry;
189+
use syntax::ast;
190+
191+
declare_lint!(TEST_LINT, Warn, "Warn about items named 'lintme'");
180192
181193
struct Pass;
182194
183195
impl LintPass for Pass {
184196
fn get_lints(&self) -> LintArray {
185197
lint_array!(TEST_LINT)
186198
}
199+
}
187200
188-
fn check_item(&mut self, cx: &Context, it: &ast::Item) {
189-
if it.ident.name == "lintme" {
201+
impl EarlyLintPass for Pass {
202+
fn check_item(&mut self, cx: &EarlyContext, it: &ast::Item) {
203+
if it.ident.name.as_str() == "lintme" {
190204
cx.span_lint(TEST_LINT, it.span, "item is named 'lintme'");
191205
}
192206
}
193207
}
194208
195209
#[plugin_registrar]
196210
pub fn plugin_registrar(reg: &mut Registry) {
197-
reg.register_lint_pass(box Pass as LintPassObject);
211+
reg.register_early_lint_pass(box Pass as EarlyLintPassObject);
198212
}
199213
```
200214

0 commit comments

Comments
 (0)