la10736 / rstest

Fixture-based test framework for Rust
Apache License 2.0
1.21k stars 43 forks source link

Unable to silence lints #217

Open jhpratt opened 1 year ago

jhpratt commented 1 year ago
#[rstest]
#[case("[optional ", "expected opening bracket at byte index 9")]
fn error_display_owned(#[case] format_description: &'static str, #[case] error: &'static str) {
    assert_eq!(
        format_description::parse_owned::<2>(format_description)
            .unwrap_err()
            .to_string(),
        error
    );
}

This correctly triggers clippy::unwrap_used. However, placing #[allow(clippy::unwrap_used)] on the function does not silence the lint. This is the case regardless of the position of the attribute.

Here is the current expansion (without an #[allow] attribute).

// `#[allow]` needs to be present here.
#[cfg(test)]
fn error_display_owned(format_description: &'static str, error: &'static str) {
    {
        assert_eq!(
            format_description::parse_owned::<2>(format_description)
                .unwrap_err()
                .to_string(),
            error
        );
    }
}
#[cfg(test)]
mod error_display_owned {
    use super::*;
    #[test]
    fn case_1() {
        let format_description = {
            use rstest::magic_conversion::*;
            (&&&Magic::<&'static str>(std::marker::PhantomData)).magic_conversion("[optional ")
        };
        let error = {
            use rstest::magic_conversion::*;
            (&&&Magic::<&'static str>(std::marker::PhantomData))
                .magic_conversion("expected opening bracket at byte index 9")
        };
        error_display_owned(format_description, error)
    }
}

Perhaps #[allow], #[warn], #[deny], and #[forbid] should be special-cased such that they are copied onto the resulting function? I have no idea how feasible this is.

la10736 commented 1 year ago

I should think about that. I guessed that the attributes before the function should act as test's attributes but maybe I was wrong. Maybe only a small subset like should_panic, ignore and skip are test's attributes.

I'm little scared because when the user use the injected tests attribute I cannot know what attributes this test attribute will handle... I can handle all standard attributes as tests attributes and move the others to the function but I should also introduce a syntax that the user can use to force some attribute to be applied to the test instead the function.

I'll com back here to draft the new syntax.