Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong suggestion from single_match #1404

Closed
killercup opened this issue Dec 27, 2016 · 10 comments
Closed

Wrong suggestion from single_match #1404

killercup opened this issue Dec 27, 2016 · 10 comments
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy T-macros Type: Issues with macros and macro expansion

Comments

@killercup
Copy link
Member

I'm trying to add clippy to diesel (again), and just got this:

error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
   --> /Users/pascal/Projekte/tools/diesel/diesel/src/types/impls/tuples.rs:145:25
    |
145 |                           match self.$idx {
    |  _________________________^ starting here...
146 | |                             ColumnInsertValue::Expression(_, ref value) => {
147 | |                                 try!(value.collect_binds(out));
148 | |                             }
149 | |                             _ => {}
150 | |                         }
    | |_________________________^ ...ending here
...
296 |   tuple_impls! {
    |   - in this macro invocation
    |
note: lint level defined here
   --> /Users/pascal/Projekte/tools/diesel/diesel/src/lib.rs:7:9
    |
7   | #![deny(warnings, missing_debug_implementations, missing_copy_implementations)]
    |         ^^^^^^^^
help: try this
    |                         if let ColumnInsertValue::Expression(_, ref value) = self.$idx {
    |                             ColumnInsertValue::Expression(_, ref value) => {
    |                                 try!(value.collect_binds(out));
    |                             }
    |                             _ => {}
    |                         }
...
    = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#single_match

The suggestion contains the correct line for if-let itself, but the body is an exact copy of the match arms, not the expression on the right side of the single match arm.

The correct suggestion would be:

if let ColumnInsertValue::Expression(_, ref value) = self.$idx {
    try!(value.collect_binds(out));
}

As you can see, this is inside a macro. I can try to make a smaller example to reproduce this if you want.

@oli-obk oli-obk added good-first-issue These issues are a good way to get started with Clippy C-bug Category: Clippy is not doing the correct thing labels Dec 27, 2016
@oli-obk
Copy link
Contributor

oli-obk commented Dec 27, 2016

We're probably just taking the wrong span. Since I'm sure this worked before, we probably messed up a rustup.

@oli-obk oli-obk added the L-tests Lint: Lints test code label Dec 27, 2016
@oli-obk
Copy link
Contributor

oli-obk commented Dec 27, 2016

We should probably add a test with a single line match to produce a single line if let so we can test te suggestion

@killercup
Copy link
Member Author

killercup commented Jan 28, 2017

There are tests for this, but the problem seems to be that this is in a macro. Investigating further, it seems that the snippet helper is not doing what it should: This call evaluates to

"t.$idx { Foo::A(ref val) => { println!(\"42\"); }, _ => {} }\n            //~^ ERROR you seem to be trying to use match\n            //~| HELP try\n            //~| SUGGESTION if let Foo::A(ref val) = t.$idx { println!(\"4\"); };\n        }}\n    }\n\n    issue_1404!(0"
"{ println!(\"42\"); }"

when given this example file to lint:

#![feature(plugin)]

#![plugin(clippy)]
#![deny(clippy)]
#![allow(unused, if_let_redundant_pattern_matching)]

fn main() {
    #[derive(Debug)]
    enum Foo {
        A(String),
        B,
    }

    struct Thingy(Foo);

    macro_rules! issue_1404 {
        ($idx:tt) => {{
            let thingy = Thingy(Foo::A("Foo".into()));
            let t = &thingy;

            match t.$idx { Foo::A(ref val) => { println!("42"); }, _ => {} }
        }}
    }

    issue_1404!(0)
}

which causes it to show this error:

error: you seem to be trying to use match for destructuring a single pattern. Consider using `if let`
  --> tmp/repl.rs:21:13
   |
21 |             match t.$idx { Foo::A(ref val) => { println!("42"); }, _ => {} }
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...
25 |     issue_1404!(0)
   |     -------------- in this macro invocation
   |
note: lint level defined here
  --> tmp/repl.rs:4:9
   |
4  | #![deny(clippy)]
   |         ^^^^^^
help: try this
   |             if let Foo::A(ref val) = t.$idx { Foo::A(ref val) => { println!("42"); }, _ => {} }
   |         }}
   |     }
   |
   |     issue_1404!(0 { println!("42"); }
   = help: for further information visit https://github.com/Manishearth/rust-clippy/wiki#single_match

error: aborting due to previous error

@oli-obk oli-obk self-assigned this Jan 28, 2017
@oli-obk
Copy link
Contributor

oli-obk commented Jan 28, 2017

Thanks for the detailed report and minimal example! Cooking up a fix now.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 28, 2017

ok... looks like rustc is generating the wrong span here.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 28, 2017

ok, so I know what's going on.

rustc expands issue_1404!(0) to macro_tokens 0 macro_tokens. When these tokens are parsed, the parser stores a tokens position, parses on, and creates a span from the stored position to where it is now. But when you parse 0 inside those macro_tokens, you get a span from inside the macro_rules to behind 0, which makes no sense.

Not sure how to fix it yet though.

@oli-obk
Copy link
Contributor

oli-obk commented Jan 28, 2017

hmm... looking at it again. Rustc is not doing anything wrong.

We have two options:

  1. don't give suggestions when an expansion is involved
  2. figure out a way to find the macro_rules arm, which was responsible for this expansion and lint somewhere in there. This has the problem that some expansions might lint, some not, depending on the types involved.

Anyone against simply removing the suggestion if macros are involved? Or even go as far as to not lint at all if macros are involved, since we can't guarantee that changing the macro won't have unintended side-effects (like in this case, suddenly having more arms, so the lint stops applying)

@killercup
Copy link
Member Author

killercup commented Jan 28, 2017 via email

@phansch phansch added T-macros Type: Issues with macros and macro expansion and removed L-tests Lint: Lints test code labels Jul 17, 2018
@ThibsG
Copy link
Contributor

ThibsG commented Oct 24, 2020

It seems we cannot reproduce this (Playground).

I think that the code for single_match lint (here and here) prevents to lint from a macro or an external macro.

@oli-obk
Copy link
Contributor

oli-obk commented Oct 25, 2020

Ah great, thanks!

@oli-obk oli-obk closed this as completed Oct 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing good-first-issue These issues are a good way to get started with Clippy T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

No branches or pull requests

5 participants