Writing your own Rust linter

by Guillaume Gomez

Who am I?

Rust language reviewer and contributor. Member of:
  • rustdoc team (team leader)
  • docs.rs team
  • dev-tools team
  • clippy-contributors team
Organizer of the monthly Paris rust meetup Huawei engineer
## What is a linter?
## How is a rust linter actually working?
## Why does it need to be a rust compiler extension?
## Limitations * Can only work on crates compiled with the same rustc version * rustc API is not stable * Tied to a specific rustc version * Annoying to wrap in a cargo command
## Compiler passes (simplified) * AST * HIR * MIR * Codegen
## Lint passes Early vs Late
## `rustc-tools` Provides many helpers for setting up lints and cargo integration (and other things...) ```toml [dependencies] rustc-tools = "0.3" ```
## Setup `rust-toolchain` ```toml [toolchain] channel = "nightly-2023-10-06" components = ["rustc-dev", "rustfmt", "llvm-tools-preview"] ```

Declaring a lint


Implementing a lint


main.rs


## Installing our linter ```console $ cargo install --path [project path] ```

Running our linter


Running our linter


## Running our linter (but better) ```console $ cargo +nightly-2023-10-06 tools-inner ```

Wrapping the wrapper

## Running our linter (but better better) ```console $ cargo install --path [project path] $ cargo tools ```

Blog post version


blog.guillaume-gomez.fr/articles/2024-01-18+Writing+your+own+Rust+linter

Thank you for listening!

More Rust things on
< blog.guillaume-gomez.fr >

< guillaume1.gomez@gmail.com >

@GuillaumeGomez
@imperioworld_
@imperio@toot.cat