Skip to content

function "in_external_macro” judgment is incorrect #131993

Open
@yu532758082

Description

@yu532758082

I use the derive macro provided by the clap crate in my code, and then call the in_external_macro function in the context of a custom lint diagnostic code. The function determines that the code derived from the derive macro is local code.

I tried this code:

fn main() {
    let _a = 1;
}

use clap::Args;


#[derive(Args)]
pub struct ToFrom {
    #[arg(long = "workspace")]
    a: i32,
}

dependency is clap = { version = "=4.2.0", features = ["derive"] }

My lint code is:

    fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx Block<'_>) {
           let res = in_external_macro(cx.sess(), block.span);
        info!("check block in_external_macro res {:?}", res);
        if res == false {
            info!("check block {:?}", block);
        }
}

I expected to see this happen: I think all code generated by derived macros should belong to external macros derived from external crates.

Instead, this happened: Some of the code generated by the derived macro is considered to be local code

The error macro code is expanded as follows.
The blocks inside the .arg function are considered native code.

                .arg({
                    #[allow(deprecated)]
                    let arg = clap::Arg::new("a")
                        .value_name("A")
                        .required(true && clap::ArgAction::Set.takes_values())
                        .value_parser({
                            use ::clap_builder::builder::via_prelude::*;
                            let auto = ::clap_builder::builder::_AutoValueParser::<
                                i32,
                            >::new();
                            (&&&&&&auto).value_parser()
                        })
                        .action(clap::ArgAction::Set);
                    let arg = arg.long("workspace");
                    let arg = arg.required(false);
                    arg
                });

rustc --version --verbose:

+nightly-2024-03-07

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-proc-macrosArea: Procedural macrosC-bugCategory: This is a bug.E-needs-investigationCall for partcipation: This issues needs some investigation to determine current statusT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions