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

use_self lint false positive in macros #3881

Closed
pavel-mukhanov opened this issue Mar 14, 2019 · 6 comments · Fixed by #6179
Closed

use_self lint false positive in macros #3881

pavel-mukhanov opened this issue Mar 14, 2019 · 6 comments · Fixed by #6179
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@pavel-mukhanov
Copy link

Minimal example

macro_rules! impl_binary_form_scalar {
    ($type:tt, $read:ident) => {
        impl BinaryValue for $type {
            fn to_bytes(&self) -> Vec<u8> {
                vec![*self as u8]
            }
        }
    };
}

This macros is invoked for

impl_binary_form_scalar! { u8,  read_u8 }

and

impl_binary_form_scalar! { i8,  read_i8 }

Clippy suggests to replace u8 with Self vec![*self as Self] which in case with i8 is not possible.
Also, I can't figure out how to disable lint in this particular place because

#![cfg_attr(feature="cargo-clippy", allow(clippy::use_self))]
fn to_bytes(&self) -> Vec<u8> {

is not working.

cargo +nightly clippy -V
clippy 0.0.212 (016d92d6 2019-03-10)
@oli-obk oli-obk added good-first-issue These issues are a good way to get started with Clippy T-macros Type: Issues with macros and macro expansion labels Mar 14, 2019
@oli-obk
Copy link
Contributor

oli-obk commented Mar 14, 2019

we should just silence this lint inside macros

@mati865
Copy link
Contributor

mati865 commented Mar 14, 2019

🤔 #2098

@oli-obk
Copy link
Contributor

oli-obk commented Mar 14, 2019

😆 ok, the arguments go both ways. I guess we just need to figure out how to disable the lint with an allow.

@pavel-mukhanov have you tried #[allow(clippy::use_self)] without the cfg-attr?

@pavel-mukhanov
Copy link
Author

@oli-obk it works if applied to impl defenition 👍

#[allow(clippy::use_self)]
impl BinaryValue for $type {
     fn to_bytes(&self) -> Vec<u8> {
           vec![*self as u8]
     }
}

@oli-obk oli-obk added C-bug Category: Clippy is not doing the correct thing and removed T-macros Type: Issues with macros and macro expansion good-first-issue These issues are a good way to get started with Clippy labels Mar 14, 2019
@mati865
Copy link
Contributor

mati865 commented Mar 14, 2019

I was about to say you can't use _ as Self without triggering E0605 and lint should skip not trigger in that cases.
After deeper thought it's possible to write such code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=62c6d2c8d16f5e39702ccfc65242cbd6

@oli-obk
Copy link
Contributor

oli-obk commented Mar 14, 2019

Ah, we should have more fine grained lint attribute checks. We're probably using check_item for the Self and then a visitor to look into the methods. That visitor should not use cx.span_lint directly. Instead we might have to resort to https://doc.rust-lang.org/nightly/nightly-rustc/rustc/ty/struct.TyCtxt.html#method.struct_span_lint_hir

@phansch phansch added the I-false-positive Issue: The lint was triggered on code it shouldn't have label Dec 22, 2020
bors added a commit that referenced this issue Jan 19, 2021
Rework use_self impl based on ty::Ty comparison #3410 | Take 2

This builds on top of #5531

I already reviewed and approved the commits by `@montrivo.` So only the review of my commits should be necessary.

I would also appreciate your review `@montrivo,` since you are familiar with the challenges here.

Fixes #3410 and Fixes #4143 (same problem)
Fixes #2843
Fixes #3859
Fixes #4734 and fixes #6221
Fixes #4305
Fixes #5078 (even at expression level now 🎉)
Fixes #3881 and Fixes #4887 (same problem)
Fixes #3909

Not yet: #4140 (test added)

All the credit for the fixes goes to `@montrivo.` I only refactored and copy and pasted his code.

changelog: rewrite [`use_self`] lint and fix multiple (8) FPs. One to go.
bors added a commit that referenced this issue Feb 10, 2021
Rework use_self impl based on ty::Ty comparison #3410 | Take 2

This builds on top of #5531

I already reviewed and approved the commits by `@montrivo.` So only the review of my commits should be necessary.

I would also appreciate your review `@montrivo,` since you are familiar with the challenges here.

Fixes #3410 and Fixes #4143 (same problem)
Fixes #2843
Fixes #3859
Fixes #4734 and fixes #6221
Fixes #4305
Fixes #5078 (even at expression level now 🎉)
Fixes #3881 and Fixes #4887 (same problem)
Fixes #3909

Not yet: #4140 (test added)

All the credit for the fixes goes to `@montrivo.` I only refactored and copy and pasted his code.

changelog: rewrite [`use_self`] lint and fix multiple (8) FPs. One to go.
@bors bors closed this as completed in 605e9ba Feb 12, 2021
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 I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants