From 3e48848538a49c66e3447a46346b96e8dc972531 Mon Sep 17 00:00:00 2001 From: Takayuki Nakata Date: Fri, 31 Jul 2020 23:53:05 +0900 Subject: [PATCH] Some fixes for `plugin.md` in unstable-book - sample codes not working - broken link --- .../src/language-features/plugin.md | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/src/doc/unstable-book/src/language-features/plugin.md b/src/doc/unstable-book/src/language-features/plugin.md index 1f010656bb8..38351131527 100644 --- a/src/doc/unstable-book/src/language-features/plugin.md +++ b/src/doc/unstable-book/src/language-features/plugin.md @@ -45,42 +45,40 @@ that warns about any item named `lintme`. extern crate rustc_ast; // Load rustc as a plugin to get macros -#[macro_use] -extern crate rustc; extern crate rustc_driver; +#[macro_use] +extern crate rustc_lint; +#[macro_use] +extern crate rustc_session; -use rustc::lint::{EarlyContext, LintContext, LintPass, EarlyLintPass, - EarlyLintPassObject, LintArray}; use rustc_driver::plugin::Registry; +use rustc_lint::{EarlyContext, EarlyLintPass, LintArray, LintContext, LintPass}; use rustc_ast::ast; - declare_lint!(TEST_LINT, Warn, "Warn about items named 'lintme'"); -struct Pass; - -impl LintPass for Pass { - fn get_lints(&self) -> LintArray { - lint_array!(TEST_LINT) - } -} +declare_lint_pass!(Pass => [TEST_LINT]); impl EarlyLintPass for Pass { fn check_item(&mut self, cx: &EarlyContext, it: &ast::Item) { - if it.ident.as_str() == "lintme" { - cx.span_lint(TEST_LINT, it.span, "item is named 'lintme'"); + if it.ident.name.as_str() == "lintme" { + cx.lint(TEST_LINT, |lint| { + lint.build("item is named 'lintme'").set_span(it.span).emit() + }); } } } #[plugin_registrar] pub fn plugin_registrar(reg: &mut Registry) { - reg.register_early_lint_pass(box Pass as EarlyLintPassObject); + reg.lint_store.register_lints(&[&TEST_LINT]); + reg.lint_store.register_early_pass(|| box Pass); } ``` Then code like ```rust,ignore +#![feature(plugin)] #![plugin(lint_plugin_test)] fn lintme() { } @@ -107,7 +105,7 @@ The components of a lint plugin are: Lint passes are syntax traversals, but they run at a late stage of compilation where type information is available. `rustc`'s [built-in -lints](https://github.com/rust-lang/rust/blob/master/src/librustc/lint/builtin.rs) +lints](https://github.com/rust-lang/rust/blob/master/src/librustc_session/lint/builtin.rs) mostly use the same infrastructure as lint plugins, and provide examples of how to access type information.