Skip to content

missing-const-for-fn might want to skip macros #13014

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

Open
matthiaskrgr opened this issue Jun 28, 2024 · 1 comment
Open

missing-const-for-fn might want to skip macros #13014

matthiaskrgr opened this issue Jun 28, 2024 · 1 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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-nursery Lint: Currently in the nursery group

Comments

@matthiaskrgr
Copy link
Member

Summary

.

Reproducer

I tried this code:

#![recursion_limit = "10"]
macro_rules! link {
    ($outer:ident, $inner:ident) => {
        struct $outer($inner);
        impl $outer {
            fn new() -> $outer {
                $outer($inner::new())
            }
        }
        impl std::ops::Deref for $outer {
            type Target = $inner;
            fn deref(&self) -> &$inner {
                &self.0
            }
        }
    };
}

struct Bottom;

impl Bottom {
  const  fn new() -> Bottom {
        Bottom
    }
}


link!(A, B);
link!(B, C);
link!(C, D);
link!(D, E);
link!(E, F);
link!(F, G);
link!(G, H);
link!(H, I);
link!(I, J);
link!(J, K);
link!(K, Bottom);

clippy sugttests touching the macro :s


warning: this could be a `const fn`
  --> a.rs:6:13
   |
6  | /             fn new() -> $outer {
7  | |                 $outer($inner::new())
8  | |             }
   | |_____________^
...
38 |   link!(K, Bottom);
   |   ---------------- in this macro invocation
   |
   = 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`
   = note: this warning originates in the macro `link` (in Nightly builds, run with -Z macro-backtrace for more info)
help: make the function `const`
   |
6  |             const fn new() -> $outer {
   |             +++++

the resulting code causes overflows:

error[E0275]: overflow evaluating the requirement `J: std::marker::Freeze`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "20"]` attribute to your crate (`a`)
note: required because it appears within the type `I`
  --> a.rs:36:7
   |
36 | link!(I, J);
   |       ^
note: required because it appears within the type `H`
  --> a.rs:35:7
   |
35 | link!(H, I);
   |       ^
note: required because it appears within the type `G`
  --> a.rs:34:7
   |
34 | link!(G, H);
   |       ^
note: required because it appears within the type `F`
  --> a.rs:33:7
   |
33 | link!(F, G);
   |       ^
note: required because it appears within the type `E`
  --> a.rs:32:7
   |
32 | link!(E, F);
   |       ^
note: required because it appears within the type `D`
  --> a.rs:31:7
   |
31 | link!(D, E);
   |       ^
note: required because it appears within the type `C`
  --> a.rs:30:7
   |
30 | link!(C, D);
   |       ^
note: required because it appears within the type `B`
  --> a.rs:29:7
   |
29 | link!(B, C);
   |       ^
note: required because it appears within the type `A`
  --> a.rs:28:7
   |
28 | link!(A, B);
   |       ^

error[E0275]: overflow evaluating the requirement `K: std::marker::Freeze`
   |
   = help: consider increasing the recursion limit by adding a `#![recursion_limit = "20"]` attribute to your crate (`a`)
note: required because it appears within the type `J`
  --> a.rs:37:7
   |
37 | link!(J, K);
   |       ^
note: required because it appears within the type `I`
  --> a.rs:36:7
   |
36 | link!(I, J);
   |       ^
note: required because it appears within the type `H`
  --> a.rs:35:7
   |
35 | link!(H, I);
   |       ^
note: required because it appears within the type `G`
  --> a.rs:34:7
   |
34 | link!(G, H);
   |       ^
note: required because it appears within the type `F`
  --> a.rs:33:7
   |
33 | link!(F, G);
   |       ^
note: required because it appears within the type `E`
  --> a.rs:32:7
   |
32 | link!(E, F);
   |       ^
note: required because it appears within the type `D`
  --> a.rs:31:7
   |
31 | link!(D, E);
   |       ^
note: required because it appears within the type `C`
  --> a.rs:30:7
   |
30 | link!(C, D);
   |       ^
note: required because it appears within the type `B`
  --> a.rs:29:7
   |
29 | link!(B, C);
   |       ^

error: aborting due to 2 previous errors

Version

rustc 1.81.0-nightly (9c3bc805d 2024-06-27)
binary: rustc
commit-hash: 9c3bc805dd9cb84019c124b9a50fdff1e62a7ec9
commit-date: 2024-06-27
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Additional Labels

No response

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied I-false-positive Issue: The lint was triggered on code it shouldn't have labels Jun 28, 2024
@J-ZhengLi J-ZhengLi added the L-nursery Lint: Currently in the nursery group label Jul 9, 2024
@DaniPopes
Copy link
Contributor

I'm guessing this would still happen even without the macro; idk what the precedent is for lints with recursion_limit, but otherwise this seems correct to me

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 I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-nursery Lint: Currently in the nursery group
Projects
None yet
Development

No branches or pull requests

3 participants