rust-lang / rust-lang/rust-clippy

feature flag `if` not being taken into consideration

Open
#8,784 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Summary

Code is here -> https://github.com/librespot-org/librespot/blob/650d41b020691fec6c7d2c90c0f6de91ac8635cf/discovery/src/lib.rs#L103-L126

For this block:

        let svc;

        cfg_if! {
            if #[cfg(feature = "with-dns-sd")] {
                svc = dns_sd::DNSService::register(
                    Some(name.as_ref()),
                    "_spotify-connect._tcp",
                    None,
                    None,
                    port,
                    &["VERSION=1.0", "CPath=/"],
                )
                .unwrap();

            } else {
                let responder = libmdns::Responder::spawn(&tokio::runtime::Handle::current())?;
                svc = responder.register(
                    "_spotify-connect._tcp".to_owned(),
                    name,
                    port,
                    &["VERSION=1.0", "CPath=/"],
                )
            }
        };

clippy reports unneeded late initalization. I think it can't "see" the that this feature-based if exists due to how the macro works.

Lint Name

needless_late_init

Version
rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-unknown-linux-gnu
release: 1.60.0
LLVM version: 14.0.0
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 cfg_if! block in discovery/src/lib.rs at lines 103-126 and reproduce the needless_late_init warning using the reported Rust 1.60.0 context. Read the lint's handling of macro-expanded, feature-gated branches and determine how this case should be recognized. Done means the valid feature-based initialization no longer produces the false-positive warning.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.