Skip to content

False positive with clippy::unwrap_unsed in integration tests with allow-unwrap-in-tests=true and helper function #11119

@FirelightFlagboy

Description

@FirelightFlagboy

Summary

I have a codebase where I want to prevent the use of unwrap outside of tests (unitest, integration).

For that I tried to use the lint clippy::unwrap_unsed with the configuration allow-unwrap-in-tests=true.
That work for unit tests and in integration tests when the function have #[test] above it,
but do not work for helper functions that are a part of the integration test.

Lint Name

clippy::unwrap_unsed

Reproducer

I start a new project with:

cargo new --lib clippy-unwrap-in-integration-test
cd clippy-unwrap-in-integration-test

I create an integration test in tests/foo.rs

fn helper() -> u32 { Some(42).unwrap() }

#[test]
fn test() {
  let _ = helper();
  let _ = Some(21).unwrap();
}

I've configured clippy by creating clippy.toml with:

allow-unwrap-in-tests=true

Then with the clippy command:

cargo clippy --tests -- --deny=clippy::unwrap_used

I saw this happen:

    Checking clippy-unwrap-in-integration-test v0.1.0 (/tmp/clippy-unwrap-in-integration-test)
error: used `unwrap()` on an `Option` value
 --> tests/foo.rs:1:22
  |
1 | fn helper() -> u32 { Some(42).unwrap() }
  |                      ^^^^^^^^^^^^^^^^^
  |
  = help: if you don't want to handle the `None` case gracefully, consider using `expect()` to provide a better panic message
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
  = note: requested on the command line with `-D clippy::unwrap-used`

error: could not compile `clippy-unwrap-in-integration-test` (test "foo") due to previous error
warning: build failed, waiting for other jobs to finish...

I expected that clippy don't raise an error for the used unwrap in the function helper present in the integration test.

Version

rustc 1.70.0 (90c541806 2023-05-31)
binary: rustc
commit-hash: 90c541806f23a127002de5b4038be731ba1458ca
commit-date: 2023-05-31
host: x86_64-unknown-linux-gnu
release: 1.70.0
LLVM version: 16.0.2

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