`clippy_lints/src/foo_functions.rs` and import some initial things we need:
```rust
-use rustc::lint::{LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::lint::{LintArray, LintPass, EarlyLintPass};
+use rustc::{declare_lint_pass, declare_tool_lint};
```
The next step is to provide a lint declaration. Lints are declared using the
// .. imports and lint declaration ..
-#[derive(Copy, Clone)]
-pub struct FooFunctionsPass;
-
-impl LintPass for FooFunctionsPass {
- fn get_lints(&self) -> LintArray {
- lint_array!(
- FOO_FUNCTIONS,
- )
- }
+declare_lint_pass!(FooFunctions => [FOO_FUNCTIONS]);
- fn name(&self) -> &'static str {
- "FooFunctions"
- }
-}
+impl EarlyLintPass for FooFunctions {}
```
Don't worry about the `name` method here. As long as it includes the name of the
in `clippy_lints/src/lib.rs`:
```rust
-reg.register_early_lint_pass(box foo_functions::FooFunctionsPass);
+reg.register_early_lint_pass(box foo_functions::FooFunctions);
```
This should fix the `unknown clippy lint: clippy::foo_functions` error that we
With UI tests and the lint declaration in place, we can start working on the
implementation of the lint logic.
-Let's start by implementing the `EarlyLintPass` for our `FooFunctionsPass`:
+Let's start by implementing the `EarlyLintPass` for our `FooFunctions`:
```rust
-impl EarlyLintPass for FooFunctionsPass {
+impl EarlyLintPass for FooFunctions {
fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
// TODO: Emit lint here
}
automatically. This is how it looks:
```rust
-impl EarlyLintPass for Pass {
+impl EarlyLintPass for FooFunctions {
fn check_fn(&mut self, cx: &EarlyContext<'_>, _: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
span_help_and_lint(
cx,
With that we can expand our `check_fn` method to:
```rust
-impl EarlyLintPass for Pass {
+impl EarlyLintPass for FooFunctions {
fn check_fn(&mut self, cx: &EarlyContext<'_>, fn_kind: FnKind<'_>, _: &FnDecl, span: Span, _: NodeId) {
if is_foo_fn(fn_kind) {
span_help_and_lint(
`cargo test` (as opposed to `cargo uitest`) will also ensure that our lint
implementation is not violating any Clippy lints itself.
-If you are still following the example, you will see that `FooFunctionsPass`
-violates a Clippy lint. So we are going to rename that struct to just `Pass`:
-
-```rust
-#[derive(Copy, Clone)]
-pub struct Pass;
-
-impl LintPass for Pass { /* .. */ }
-```
-
That should be it for the lint implementation. Running `cargo test` should now
pass.
- [ ] `cargo test` passes locally
- [ ] Executed `util/dev update_lints`
- [ ] Added lint documentation
+- [ ] Run `cargo fmt`
### Cheatsheet