rust-lang / rust-lang/rust-clippy

Reparse all suggestions to prevent suggesting syntactically invalid code

Open
#6,919 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

I frequently see Clippy make attempted suggestions involving macro calls, such as the following:

// [dependencies]
// derivative = "=1.0.4"

use derivative::Derivative;

#[derive(Derivative)]
#[derivative(Debug)]
struct S { x: () }
warning: this match could be written as a `let` statement
 --> src/main.rs:7:1
  |
7 | #[derivative(Debug)]
  | ^
  |
  = note: `#[warn(clippy::match_single_binding)]` on by default
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#match_single_binding
help: consider using `let` statement
  |
7 | let Derivative = #;
8 | Derivative[derivative(Debug)]
  |

Often it's possible to fix these by tweaking some spans in the macro implementation or by putting codepaths into Clippy to block certain lints in macro-generated code. However, I wonder whether it would be better to fix this category of broken suggestions by passing all suggested code through a parser prior to emitting a suggestion. In the above, it should be very clear that Clippy's suggested replacement code does not parse, irrespective of what's going on with spans.

use derivative::Derivative;

#[derive(Derivative)]
let Derivative = #;
Derivative[derivative(Debug)]
struct S { x: () }

Since the above suggested code does not parse, Clippy should emit the lint without a suggestion instead.


Meta
  • cargo clippy -V: clippy 0.1.52 (107896c 2021-03-15)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the issue's cargo clippy example and examine how Clippy currently emits suggestions for macro calls. Investigate where suggested replacement code could be passed through a parser before emission. Done means syntactically invalid suggestions are suppressed while the lint itself remains available.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.