rust-lang / rust-lang/rust-clippy

`manual_unwrap_or_default` invalid suggestion on macro expanded code

Open
#12,967 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

manual_unwrap_or_default invalid suggestion on macro expanded code

Reproducer

I tried this code: https://github.com/dj8yfo/clppy_bug/blob/master/src/lib.rs#L1-L10

I expected to see this happen: no clippy suggestion is produced

Instead, this happened:

❯ cargo clippy
warning: if let can be simplified with `.unwrap_or_default()`
 --> src/lib.rs:8:15
  |
8 |     #[darling(skip)]
  |               ^^^^
  |
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or_default
  = note: `#[warn(clippy::manual_unwrap_or_default)]` on by default
help: replace it with
  |
8 ~     #[darling(skip)]
9 ~     bar.unwrap_or_default())]
  |
Version
rustc 1.79.0 (129f3b996 2024-06-10)
binary: rustc
commit-hash: 129f3b9964af4d4a709d1383930ade12dfe7c081
commit-date: 2024-06-10
host: x86_64-unknown-linux-gnu
release: 1.79.0
LLVM version: 18.1.7
KK
Additional Notes

It looks like clippy suggests to change this code inside of darling macro expansion:

        ::darling::export::Ok(Self {
            qux: qux
                .1
                .expect("Uninitialized fields without defaults were already checked"),
            bar: if let Some(__val) = bar.1 {
                __val
            } else {
                ::darling::export::Default::default()
            },
        })
Additional Labels

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked src/lib.rs reproducer and run cargo clippy to observe the manual_unwrap_or_default warning at the darling attribute. Trace the manual_unwrap_or_default lint and its handling of macro-expanded code. Done means the reproducer no longer receives an invalid suggestion for code generated inside the macro expansion.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.