Skip to content

toplevel_ref_arg triggers on macro-generated code #5849

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

Closed
jplatte opened this issue Jul 27, 2020 · 4 comments · Fixed by #6212
Closed

toplevel_ref_arg triggers on macro-generated code #5849

jplatte opened this issue Jul 27, 2020 · 4 comments · Fixed by #6212
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

@jplatte
Copy link
Contributor

jplatte commented Jul 27, 2020

Not sure whether this is intended behaviour, but currently pretty much all SQLx query_as! invocations trigger this lint, e.g.

sqlx::query_as!(SomeType, "SELECT * FROM table WHERE id = $1", id)
    .fetch_one(pool)
    .await

ref on an entire let pattern is discouraged, take a reference with & instead

Meta

  • cargo clippy -V: clippy 0.0.212 (6c8927b 2020-07-26)
  • rustc -Vv:
    rustc 1.47.0-nightly (6c8927b0c 2020-07-26)
    binary: rustc
    commit-hash: 6c8927b0cf80ceee19386026cf9d7fd4fd9d486f
    commit-date: 2020-07-26
    host: x86_64-unknown-linux-gnu
    release: 1.47.0-nightly
    LLVM version: 10.0
    
@abonander
Copy link

This can be trivially fixed in SQLx, but in general it would be nice to know how we could avoid triggering lint warnings in generated code which the user has no control over.

@abonander
Copy link

It's also worth mentioning that this warning was produced with sqlx 0.4.0-beta.1

mehcode pushed a commit to launchbadge/sqlx that referenced this issue Jul 28, 2020
@flip1995 flip1995 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 O-macos T-macros Type: Issues with macros and macro expansion and removed O-macos labels Jul 28, 2020
@Ryan1729
Copy link
Contributor

it would be nice to know how we could avoid triggering lint warnings in generated code which the user has no control over.

Depending on what form the macro output takes, you could add an #[allow(clippy::foo)] annotation to it.

For instance, if the output is an expression, you can wrap the output in a block and add an inner attribute at the top.

Playground example

@ebroto
Copy link
Member

ebroto commented Sep 4, 2020

To fix this issue, we can use the utils::in_macro function to check if we have to bail out. This lint is found in the misc.rs module.

For the tests, we should create two macros, and use them:

  • one that generates a function using ref in parameter position. Example:
  • another that generates a let binding using ref.

For example:

macro_rules! gen_binding {
    () => {
        let ref _y = 42;
    }
}

macro_rules! gen_function {
    () => {
        fn fun_example(ref _x: usize) {}
    }
}

(playground)

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

Successfully merging a pull request may close this issue.

5 participants