Skip to content

compiler error in correct program with specific declarative macros #147855

@PlexSheep

Description

@PlexSheep

Summary

In my project, I use declarative macros to better work with string IDs for GTK actions. For this, I use declarative macros declared by a second declarative macro. I found that clippy fails while trying to use cargo clippy --fix on it, while compiling and running it with cargo run works without fail.

I understand this is rather specific and an edge case, but since the output specifically says to report this, I'm opening this bug report so that it is at least reported.

Clippy Output
plex@theseus /tmp/tmp.kly9o3CJTt/aa % cargo run                       
   Compiling aa v0.1.0 (/tmp/tmp.kly9o3CJTt/aa)
warning: unused macro definition: `simple_action`
 --> src/main.rs:4:22
  |
4 |         macro_rules! simple_action {
  |                      ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `A_ID_SETTINGS_DELETE_IDENTITY`
  --> src/main.rs:40:14
   |
40 |         aid!(A_ID_SETTINGS_DELETE_IDENTITY, "settings.delete_identity");
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused import: `$id`
  --> src/main.rs:31:32
   |
31 |                 pub(crate) use $id;
   |                                ^^^
...
40 |         aid!(A_ID_SETTINGS_DELETE_IDENTITY, "settings.delete_identity");
   |         --------------------------------------------------------------- in this macro invocation
   |
   = note: `#[warn(unused_imports)]` on by default
   = note: this warning originates in the macro `aid` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `aa` (bin "aa") generated 3 warnings (run `cargo fix --bin "aa"` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.11s
     Running `target/debug/aa`
Hello, world! settings.delete_everything
plex@theseus /tmp/tmp.kly9o3CJTt/aa % cargo clippy --fix --allow-dirty
    Checking aa v0.1.0 (/tmp/tmp.kly9o3CJTt/aa)
warning: failed to automatically apply fixes suggested by rustc to crate `aa`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0433]: failed to resolve: could not find `A_ID_SETTINGS_DELETE_EVERYTHING` in `ids`
  --> src/main.rs:47:27
   |
47 |         crate::modu::ids::A_ID_SETTINGS_DELETE_EVERYTHING!()
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `A_ID_SETTINGS_DELETE_EVERYTHING` in `ids`

warning: unused macro definition: `simple_action`
 --> src/main.rs:4:22
  |
4 |         macro_rules! simple_action {
  |                      ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `A_ID_SETTINGS_DELETE_EVERYTHING`
  --> src/main.rs:36:13
   |
36 |             A_ID_SETTINGS_DELETE_EVERYTHING,
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused macro definition: `A_ID_SETTINGS_DELETE_IDENTITY`
  --> src/main.rs:40:14
   |
40 |         aid!(A_ID_SETTINGS_DELETE_IDENTITY, "settings.delete_identity");
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error; 3 warnings emitted

For more information about this error, try `rustc --explain E0433`.
Original diagnostics will follow.

warning: unused macro definition: `simple_action`
 --> src/main.rs:4:22
  |
4 |         macro_rules! simple_action {
  |                      ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `A_ID_SETTINGS_DELETE_IDENTITY`
  --> src/main.rs:40:14
   |
40 |         aid!(A_ID_SETTINGS_DELETE_IDENTITY, "settings.delete_identity");
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused import: `$id`
  --> src/main.rs:31:32
   |
31 |                 pub(crate) use $id;
   |                                ^^^
...
40 |         aid!(A_ID_SETTINGS_DELETE_IDENTITY, "settings.delete_identity");
   |         --------------------------------------------------------------- in this macro invocation
   |
   = note: `#[warn(unused_imports)]` on by default
   = note: this warning originates in the macro `aid` (in Nightly builds, run with -Z macro-backtrace for more info)

warning: `aa` (bin "aa") generated 3 warnings (run `cargo clippy --fix --bin "aa"` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `aa`

after fixes were automatically applied the compiler reported errors within these files:

  * src/main.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0433]: failed to resolve: could not find `A_ID_SETTINGS_DELETE_EVERYTHING` in `ids`
  --> src/main.rs:47:27
   |
47 |         crate::modu::ids::A_ID_SETTINGS_DELETE_EVERYTHING!()
   |                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ could not find `A_ID_SETTINGS_DELETE_EVERYTHING` in `ids`

warning: unused macro definition: `simple_action`
 --> src/main.rs:4:22
  |
4 |         macro_rules! simple_action {
  |                      ^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_macros)]` on by default

warning: unused macro definition: `A_ID_SETTINGS_DELETE_EVERYTHING`
  --> src/main.rs:36:13
   |
36 |             A_ID_SETTINGS_DELETE_EVERYTHING,
   |             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused macro definition: `A_ID_SETTINGS_DELETE_IDENTITY`
  --> src/main.rs:40:14
   |
40 |         aid!(A_ID_SETTINGS_DELETE_IDENTITY, "settings.delete_identity");
   |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 1 previous error; 3 warnings emitted

For more information about this error, try `rustc --explain E0433`.
Original diagnostics will follow.

warning: `aa` (bin "aa" test) generated 3 warnings (3 duplicates)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.15s

Playing around a bit, I was able to reproduce the error with the smaller code attached below.

Interestingly, the code fixes happen (at least partially) even if errors occur.

This CI log shows how this occured in my project.

Reproducer

The following (a bit complicated) program can be compiled and runs with cargo run but cargo clippy fix fails on it.

mod modu {
    pub(crate) mod macros {

        macro_rules! simple_action {
            ($app:expr, $id:expr, $body:block) => {{
                let action = gtk::gio::SimpleAction::new($id, None);
                action.connect_activate(move |_, _| $body);
                $app.add_action(&action);
            }};
            ($app:expr, $state:expr, $app_c:ident, $state_c:ident, $id:expr, $body:block) => {{
                let $app_c = $app.clone();
                let $state_c = $state.clone();
                let action = gtk::gio::SimpleAction::new($id, None);
                action.connect_activate(move |_, _| $body);
                $app.add_action(&action);
            }};
        }
    }

    pub(crate) mod ids {
        macro_rules! aid {
            ($id:ident, $text:literal) => {
                macro_rules! $id {
                    () => {
                        $text
                    };
                    (app) => {
                        concat!("app.", $text)
                    };
                }
                pub(crate) use $id;
            };
        }

        aid!(
            A_ID_SETTINGS_DELETE_EVERYTHING,
            "settings.delete_everything"
        );
        // NOTE: Clippy only fails if there is another A_ID declared that is NOT USED
        aid!(A_ID_SETTINGS_DELETE_IDENTITY, "settings.delete_identity");
    }
}

fn main() {
    println!(
        "Hello, world! {}",
        crate::modu::ids::A_ID_SETTINGS_DELETE_EVERYTHING!()
    );
}

Version

rustc 1.90.0 (1159e78c4 2025-09-14)
binary: rustc
commit-hash: 1159e78c4747b02ef996e55082b704c09b970588
commit-date: 2025-09-14
host: x86_64-unknown-linux-gnu
release: 1.90.0
LLVM version: 20.1.8

Additional Labels

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions