rust-lang / rust-lang/rust-clippy

unnecessary-wraps ignores cfg/feature

Open
#16,228 0 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

While reviewing the latest clippy lints, I noticed that certain feature combinations triggered a notice for unnecessary-wraps while other feature combinations did not do so. Ideally, this lint would evaluate the function and see that it can sometimes return Error.

A couple of related issues seem to fall in the same vein:
https://github.com/rust-lang/rust-clippy/issues/6613
https://github.com/rust-lang/rust-clippy/issues/16191
https://github.com/rust-lang/rust-clippy/issues/15885

Linking: https://github.com/leftwm/leftwm/pull/1349

Lint Name

unnecessary_wraps

Reproducer

I tried this code:

/// Checks the enabled features and attempts to find their dependencies/binaries as applicable
///
/// # Errors
/// - An enabled feature is missing a dependency
///   Resolutions may include:
///   - Disable the feature (remove from --features at compile time)
///   - Install any dependency/dependencies which are missing
///   - Ensure all binaries are installed to a location in your PATH
#[allow(unused_variables)]
fn check_enabled_features(verbose: bool) -> Result<()> {
    if env!("LEFTWM_FEATURES").is_empty() {
        println!("\x1b[0;94m::\x1b[0m Built with no enabled features.");
        return Ok(());
    }
    println!(
        "\x1b[0;94m::\x1b[0m Enabled features:{}",
        env!("LEFTWM_FEATURES")
    );
    println!("\x1b[0;94m::\x1b[0m Checking feature dependencies . . .");
    #[cfg(feature = "journald-log")]
    check_feature("journald-log", tracing_journald::layer)?;
    #[cfg(feature = "lefthk")]
    // TODO once we refactor all file handling into a utiliy module, we want to call a `path-builder` method from that module
    check_feature("lefthk", || check_binary("lefthk-worker", verbose))?;
    Ok(())
}

// this function is called only when specific features are enabled.
#[allow(dead_code)]
fn check_feature<T, E, F>(name: &str, predicate: F) -> Result<()>
where
    F: FnOnce() -> Result<T, E>,
    E: std::fmt::Debug,
{
    match predicate() {
        Ok(_) => {
            println!("\x1b[0;92m    -> {name} OK\x1b[0m");
            Ok(())
        }
        Err(err) => bail!("Check for feature {name} failed: {err:?}"),
    }
}

With these flags:

cargo +nightly clippy --all-targets --no-default-features --features xlib -- -W clippy::pedantic

I saw this happen:

warning: this function's return value is unnecessary
   --> leftwm/src/bin/leftwm-check.rs:368:1
    |
368 | fn check_enabled_features(verbose: bool) -> Result<()> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_wraps
    = note: `-W clippy::unnecessary-wraps` implied by `-W clippy::pedantic`
    = help: to override `-W clippy::pedantic` add `#[allow(clippy::unnecessary_wraps)]`
help: remove the return type...
    |
368 - fn check_enabled_features(verbose: bool) -> Result<()> {
368 + fn check_enabled_features(verbose: bool) -> () {
    |
help: ...and then remove returned values
    |
371 ~         return ;
372 |     }
...
386 |
387 ~
    |

warning: `leftwm` (bin "leftwm-check") generated 1 warning
warning: `leftwm` (bin "leftwm-check" test) generated 1 warning (1 duplicate)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 5.12s

I expected to see this happen:
No warnings

Version
rustc 1.94.0-nightly (f52090008 2025-12-10)
binary: rustc
commit-hash: f5209000832c9d3bc29c91f4daef4ca9f28dc797
commit-date: 2025-12-10
host: x86_64-unknown-linux-gnu
release: 1.94.0-nightly
LLVM version: 21.1.5
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 unnecessary_wraps lint and reproduce the warning using the leftwm/src/bin/leftwm-check.rs example and the cargo +nightly clippy command with --no-default-features --features xlib. Compare behavior across feature combinations and related issues 6613, 16191, and 15885. Done means the lint no longer warns when cfg-gated paths can return an error, with regression coverage for the reproduced case.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.