Skip to content

unnecessary-wraps ignores cfg/feature #16228

@mautamu

Description

@mautamu

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:
#6613
#16191
#15885

Linking: leftwm/leftwm#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

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: Clippy is not doing the correct thingI-false-positiveIssue: The lint was triggered on code it shouldn't have

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions