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

extra-unused-lifetimes should not lint inside macros #8895

Closed
matthiaskrgr opened this issue May 26, 2022 · 5 comments
Closed

extra-unused-lifetimes should not lint inside macros #8895

matthiaskrgr opened this issue May 26, 2022 · 5 comments
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented May 26, 2022

Summary

If we have a macro that generates impls for something, obviously it may look like like the lifetime is unused inside the macro, but it might be used in the macro callsite.

macro_rules! forward_display_to_print {
    ($($ty:ty),+) => {
        // Some of the $ty arguments may not actually use 'tcx
        $(#[allow(unused_lifetimes)] impl<'tcx> fmt::Display for $ty {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                ty::tls::with(|tcx| {
                    let cx = tcx.lift(*self)
                        .expect("could not lift for printing")
                        .print(FmtPrinter::new(tcx, Namespace::TypeNS))?;
                    f.write_str(&cx.into_buffer())?;
                    Ok(())
                })
            }
        })+
    };
}

clippy warns about <'tcx> here but removing it breaks the code.

Lint Name

extra-unused-lifetimes

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing T-macros Type: Issues with macros and macro expansion I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels May 26, 2022
@smoelius
Copy link
Contributor

Are we sure that the proposed behavior is the desired behavior?

Like wouldn't the same reasoning imply that unused_extern_crates shouldn't lint inside macros?

#![warn(unused_extern_crates)]

macro_rules! impl_f {
    ($($stmts:stmt)*) => {
        extern crate rand;
        pub fn f() {
            $($stmts)*
        }
    };
}

mod foo {
    impl_f!(println!("{:?}", rand::random::<u32>()));
}

mod bar {
    impl_f!();
}

fn main() {
    foo::f();
    bar::f();
}

(playground)

@MrPiao
Copy link

MrPiao commented Aug 2, 2022

Duplicate of #9014 right?

@dswij
Copy link
Member

dswij commented Aug 3, 2022

Fixed by #9014. extra-unused-lifetimes does not lint the example code anymore in nightly.

Thanks @MrPiao for taking a look

@dswij dswij closed this as completed Aug 3, 2022
@oren0e
Copy link

oren0e commented Sep 11, 2022

@dswij do you know when this is going to get to stable?

@dswij
Copy link
Member

dswij commented Sep 12, 2022

@oren0e the fix is in beta. So, we should be getting this in the next stable release

tgecho added a commit to tgecho/canrun_rs that referenced this issue Sep 17, 2022
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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied T-macros Type: Issues with macros and macro expansion
Projects
None yet
Development

No branches or pull requests

5 participants