Annotate functions with custom attributes to use them later in a LintPass


I am working on a compiler plugin that uses custom attributes to annotate functions and needs to later use information of those attributes in the LintPass.

So for the following code, when visiting the function test() in a LintPass, I need to know that the function was annotated with foo="bar".

fn test() {}

My first attempt to solve this was to use a global hashmap that maps function names to those attributes. This hashmap was populated in the syntax extension phase of the plugin. However using function names as keys won’t work when working with ImplItem, because during the syntax expansion stage, the plugin has no access to the name of the implemented trait or struct, so for the follwing code, it can distinguish between the two test() functions

fn test() {}

impl MyStruct {
    fn test(){}

I also tried using ast node ids as keys to the hashmap, but the node ids change between the expansion and lint stage.

Any ideas how to solve this problem?


I had a similar problem with clippy’s len_without_is_empty lint, which actually iterates all implemented traits for a given type in search of a function.

Just yesterday I found a problem with the approach (which I intend to solve soon, anyway), but the reasoning behind it is sound. Note that this is a linear search, so it won’t be too fast for large code bases with lots of trait impls.


Thanks for your response!

Unfortunately I am not sure how a linear search could help with my problem. As far as I can see, all the work in clippy’s LenZero lint happens in a late lint plugin and it seems like there are no custom attributes involved, which is the key problem I try to solve.