rust
rust copied to clipboard
False positive dead code warning
When code is generated via a proc macro there is a case which creates a false positive for the dead code warning.
Proc macro (src/lib.rs):
use proc_macro2::Span;
use quote::quote;
use syn::spanned::Spanned;
use syn::{parse_macro_input, DeriveInput, Ident};
#[proc_macro_derive(Test)]
pub fn proc_dead_code_test(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(tokens as DeriveInput);
let repr = Ident::new("i8", input.span());
let repr_call_size = Ident::new("i8", Span::call_site());
let vis = input.vis;
quote! {
impl MyEnum {
#vis fn false_positive(self) -> #repr {
self as i8
}
pub(crate) fn ok_1(self) -> #repr {
self as i8
}
#vis fn ok_2(self) -> i8 {
self as i8
}
#vis fn ok_3(self) -> #repr_call_size {
self as i8
}
}
}
.into()
}
Test (tests/all.rs):
use proc_dead_code_test::Test;
#[allow(dead_code)]
#[derive(Test)]
#[repr(i8)]
pub(crate) enum MyEnum {
MyValue,
}
The current output is:
warning: associated function `false_positive` is never used
--> tests/all.rs:4:10
|
4 | #[derive(Test)]
| ^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: `proc_dead_code_test` (test "all") generated 1 warning
The generated code for all four functions are identical (except the name, obviusly), tested with cargo expand
.
One is reported as dead code, though no report should be given since #[allow(dead_code)]
is used.
On 1.63.0 this is not a warning, but with 1.65.0-beta.1 it is.
I'm aware that this is a very special case and I don't mind if it doesn't get fixed but I thought I'll report it anyway.
For a quick copy-and-paste run here the Cargo.toml:
[package]
name = "proc_dead_code_test"
version = "0.1.0"
edition = "2021"
[lib]
proc-macro = true
[dependencies]
syn = { version = "1.0.99", features = ["derive", "parsing", "extra-traits"] }
quote = "1.0.21"
proc-macro2 = "1.0.43"
(I don't know how to setup play.rust-lang.org to create a proc-macro and a real test, thats why it's missing.)
We discuss this in the wg-macros triage meeting and the conclusion was that we would like to fix this warning, but there are no enough people interested currently in this area.
So if someone is interested in working on it, please open a discussion on our Zulip stream #wg-macros