Skip to content

False positive unit-arg when deriving proptest::arbitrary::Arbitrary #6594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
turion opened this issue Jan 15, 2021 · 1 comment · Fixed by #8694
Closed

False positive unit-arg when deriving proptest::arbitrary::Arbitrary #6594

turion opened this issue Jan 15, 2021 · 1 comment · Fixed by #8694
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@turion
Copy link

turion commented Jan 15, 2021

Lint name: unit-arg

I tried this code:

use proptest_derive::Arbitrary;

#[derive(Debug, Arbitrary)]
struct Foo(i64);

fn main() {
    println!("Hello, world!");
}

I expected to see this happen: clippy passes

Instead, this happened:

error: passing a unit value to a function
 --> src/main.rs:4:12
  |
4 | struct Foo(i64);
  |            ^^^
  |
  = note: `-D clippy::unit-arg` implied by `-D warnings`
  = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unit_arg
help: move the expression in front of the call and replace it with the unit literal `()`
  |
4 | struct Foo({
5 |     Arbitrary;
6 |     i64
7 | });
  |

I've only seen this happen with https://github.com/AltSysrq/proptest/.

A more complicated example passes:

use proptest_derive::Arbitrary;

#[derive(Debug, Arbitrary)]
struct Foo<T>(T);

fn main() {
    println!("Hello, world!");
}

Possibly this is because some macro expanded code triggered the issue? cargo expand yields:

#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
use proptest_derive::Arbitrary;
struct Foo(i64);
#[automatically_derived]
#[allow(unused_qualifications)]
impl ::core::fmt::Debug for Foo {
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match *self {
            Foo(ref __self_0_0) => {
                let mut debug_trait_builder = f.debug_tuple("Foo");
                let _ = debug_trait_builder.field(&&(*__self_0_0));
                debug_trait_builder.finish()
            }
        }
    }
}
#[allow(non_upper_case_globals)]
const _IMPL_ARBITRARY_FOR_Foo: () = {
    extern crate proptest as _proptest;
    impl _proptest::arbitrary::Arbitrary for Foo {
        type Parameters = <i64 as _proptest::arbitrary::Arbitrary>::Parameters;
        type Strategy = _proptest::strategy::Map<
            (<i64 as _proptest::arbitrary::Arbitrary>::Strategy,),
            fn((i64,)) -> Self,
        >;
        fn arbitrary_with(_top: Self::Parameters) -> Self::Strategy {
            {
                let param_0 = _top;
                _proptest::strategy::Strategy::prop_map(
                    (_proptest::arbitrary::any_with::<i64>(param_0),),
                    |(tmp_0,)| Foo { 0: tmp_0 },
                )
            }
        }
    }
};
fn main() {
    {
        ::std::io::_print(::core::fmt::Arguments::new_v1(
            &["Hello, world!\n"],
            &match () {
                () => [],
            },
        ));
    };
}

Possibly the line const _IMPL_ARBITRARY_FOR_Foo: () = { triggers the false positive?

Meta

  • cargo clippy -V: clippy 0.1.51 (e38fb30 2021-01-14)
  • rustc -Vv:
    rustc 1.51.0-nightly (e38fb306b 2021-01-14)
    binary: rustc
    commit-hash: e38fb306b7f5e65cca34df2dab1f0db15e1defb4
    commit-date: 2021-01-14
    host: x86_64-unknown-linux-gnu
    release: 1.51.0-nightly
    LLVM version: 11.0
    
@turion turion added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jan 15, 2021
@ghost
Copy link

ghost commented Mar 27, 2021

It's not the constant that's triggering this. It's because <i64 as _proptest::arbitrary::Arbitrary>::Parameters is () (proptest docs). This is basically the same situation as #6447 which was fixed about a month ago. I tried with nightly and the warning is no longer present.

bors added a commit that referenced this issue Aug 8, 2022
More proc-macro detection

fixes #6514
fixes #8683
fixes #6858
fixes #6594

This is a more general way of checking if an expression comes from a macro and could be trivially applied to other lints. Ideally this would be fixed in rustc's proc-macro api, but I don't see that happening any time soon.

changelog: Don't lint `unit_arg` when generated by a proc-macro.
@bors bors closed this as completed in 10853f7 Aug 8, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant