-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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 fires on code expanded from a macro #4887
Comments
I believe it was an explicit change introduced here, it used to not trigger on macro expansions, after the change it only triggers in local macro expansions. |
@Areredify is right: Issue: #2098, PR: #3627 You can fix a macro like this: macro_rules! mac {
($ty:ident) => {{
impl $ty {
fn new() -> $ty {
$ty { /* Some fields */ };
}
}
}}
}; with: macro_rules! mac {
($ty:ident) => {{
impl $ty {
fn new() -> Self {
Self { /* Some fields */ };
}
}
}}
}; If you found a case, where this is not possible, feel free to report it here and I'll reopen this issue. |
I... absolutely don't follow your example code there. Those are very different. One is a trait impl and the other is an associated function impl. Either way, my macro is this one here: https://github.com/Lokathor/wide/blob/master/src/m_f32x4.rs#L67 (as well as another similar one for And usages... all over the place in the module.
|
Oh, I simplified my example from a Trait impl to a simple impl, but forgot to edit a line. edited it now, sorry for the confusion. Now I see your points. The easiest thing to do here would be just to disable it in macros again. If we could distinguish if the self type came from
in some way. Maybe only lint in macros when we're inside an |
I've stumbled upon this bug when trying to pass the type name of use std::any::type_name;
trait ToI32 {
fn to_i32(&self) -> i32;
}
impl ToI32 for f64 {
fn to_i32(&self) -> i32 {
if !self.is_finite() {
panic!("Cannot convert {}: {} into i32", type_name::<Self>(), self);
}
*self as i32
}
}
fn main() {
} which emitted a warning: warning: unnecessary structure name repetition
--> src/main.rs:10:13
|
10 | panic!("Cannot convert {}: {} into i32", type_name::<Self>(), self);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
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.
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.
When using a macro that expands to a particular fixed type, if you happen to use that macro in a context where
Self
could have been used theuse_self
lint will trigger and tell you to fix the macro.Any way to make it not trigger on macro expansions?
The text was updated successfully, but these errors were encountered: