Skip to content
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

clippy::missing_const_for_fn triggering for tests is kind of pointless #13938

Open
tv42 opened this issue Jan 3, 2025 · 1 comment · May be fixed by #13945
Open

clippy::missing_const_for_fn triggering for tests is kind of pointless #13938

tv42 opened this issue Jan 3, 2025 · 1 comment · May be fixed by #13945
Assignees
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

@tv42
Copy link

tv42 commented Jan 3, 2025

Summary

I wrote a test to make sure I remembered to keep a type implementing Copy. The real type is defined by a fairly elaborate macro, so that felt like a real risk. It's meant to be a "does this compile" test. Clippy told me I should make my unit test const fn, which seems pointless.

I suggest clippy::missing_const_for_fn should ignore anything #[test].

Lint Name

clippy::missing_const_for_fn

Reproducer

I tried this code:

#[derive(Clone, Copy)]
pub struct Foo {
    pub n: u32,
}

impl Foo {
    #[must_use]
    pub const fn new(n: u32) -> Foo {
        Foo { n }
    }
}

#[test]
fn foo_is_copy() {
    let foo = Foo::new(42);
    let one = foo;
    let two = foo;
    _ = one;
    _ = two;
}

I saw this happen:

cargo clippy --tests -- -W clippy::missing_const_for_fn
+ command cargo clippy --tests -- -W clippy::missing_const_for_fn
    Checking rust-clippy-silly v0.1.0 (/home/tv/src/eagain.net/2024/rust-clippy-silly)
warning: this could be a `const fn`
  --> src/lib.rs:14:1
   |
14 | / fn foo_is_copy() {
15 | |     let foo = Foo::new(42);
16 | |     let one = foo;
17 | |     let two = foo;
18 | |     _ = one;
19 | |     _ = two;
20 | | }
   | |_^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
   = note: requested on the command line with `-W clippy::missing-const-for-fn`
help: make the function `const`
   |
14 | const fn foo_is_copy() {
   | +++++

warning: `rust-clippy-silly` (lib test) generated 1 warning (run `cargo clippy --fix --lib -p rust-clippy-silly --tests` to apply 1 suggestion)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s

I expected to see this happen: *blissful silence*

Version


Additional Labels

No response

@tv42 tv42 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 3, 2025
@alex-semenyuk
Copy link
Member

@rustbot claim

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.

2 participants