Coder Social home page Coder Social logo

presentation-rustfix-fosdem's Introduction

title subtitle author date categories theme progress slideNumber history
rustfix
…and the journey to getting better code automatically
Pascal Hertleif
2018-02-04
rust
presentation
solarized
true
true
true

Hi, I'm Pascal Hertleif

::: notes

Thanks for the kind introduction!

  • I've been working with Rust since early 2014
  • If you ever happen to be in Cologne, drop by at one of our monthly meetups!
  • And with that out of the way, let's get started!

:::

Rust programmers like compiler errors

::: notes

Let's start with something uncontroversial: Rust programmers like compiler errors

:::


error[E0507]: cannot move out of borrowed content
   --> src/lib.rs:126:29
    |
126 |         if let Some(code) = diagnostic.code {
    |                     ----    ^^^^^^^^^^ cannot move out of borrowed content
    |                     |
    |                     hint: to prevent move, use `ref code` or `ref mut code`

Why?

Rust is all about compile-time checks

But also: Rust's errors are pretty and helpful

Warnings, too

  • Lints
  • Prevent errors
  • Examples
    • unused Results or Iterators
    • Missing docs

Clippy

It looks like you're enjoying errors. Would you like some more?

  • 208 lints written by the community
  • Find common bugs
    • For example: if_same_then_else

::: notes

Clippy was originally written by Manish Goregaokar but there are a lot of people who contributed to it. Oliver here, for example!

:::

Lints as a teaching tool


iter.filter(|x| x == 0).next()

. . .

Did you know about iter.find(|x| x == 0)?

::: notes

Are there any seasoned Rust developers here who know a better way to write this?

:::


if !m.contains_key(&k) { m.insert(k, v) }

. . .

Have you heard the good news about the Entry API?

m.entry(k).or_insert(v);

::: notes

Okay, you get another chance! How about this code? What would you change?

The Entry API only needs to compute the hash once!

:::


  • Learn new APIs when you are almost using them
  • Each clippy lint is documented with examples

::: notes

This is really powerful, as you get the chance to learn something very close to when you where thinking about how to solve this problem. So you are not reading a book and much later try to apply you knowledge, but get to learn things by working on your own code.

:::

Making this interactive


Suggestions

error: this range is empty so this for loop will never run
   --> $DIR/for_loop.rs:148:14
    |
148 |     for i in 10..0 {
    |              ^^^^^
    |
    = note: `-D reverse-range-loop` implied by `-D warnings`
help: consider using the following if you are attempting to iterate over this range in reverse
    |
148 |     for i in (0..10).rev() {
    |              ^^^^^^^^^^^^^

::: notes

The compiler can give you suggestions based on your code. Many of these suggestions you can copy and paste to your code

:::

Why fix it yourself?

I'm sold. These suggestions are great. But…

  • don't make me type
  • don't make me wait
  • just fix my code already

Presenting: rustfix


It's already there

No magic! Just:

  • Take existing errors
  • Parse the compiler output (JSON)
  • Apply suggestions (search & replace)

::: notes

The rustfix tool is really basic. It uses the already existing building blocks. I can't take any credit for those!

:::

Make it interactive (1)

rustfix as CLI tool is interactive

  • for each error (that has a suggestion) it
    • shows you the error and the fix(es)
    • asks what you want to do
    • fixes your code

Make it interactive (2)

You need to manually call the CLI tool

You do that after working on the code

We can do better

Fixes via IDE


RLS

  • Rust Language Server exposes warnings and suggestions
  • VSCode plugin can already apply fixes


Demo


Laptop didn't work?

Totally saw that coming.

– Past Me









Deep dive!

How does this work?

  • Compiler plugin
  • Find nodes and items of interest
  • 'Annotate' spans of code
  • Compiler generates human/JSON output

What is auto-fixable?

  • A lot!
  • Do we need to make a list?
  • Rust PR #47540 adds flag for "approximate suggestions"
  • rustfix-like tools will use this in the future
  • UPDATE Merged! 🎉

Thanks!

You can contribute lints and suggestions!

Search for "Rust Clippy" or go to https://github.com/rust-lang-nursery/rust-clippy

There are a lot of issues labelled good first issue

You get to call yourself a compiler hacker!

Any questions?

Visit rust-lang.org

Follow me on Twitter: @killercup

Slides available at git.io/fosdem-rustfix

presentation-rustfix-fosdem's People

Contributors

killercup avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.