Commit Graph

4 Commits

Author SHA1 Message Date
Quinn Sinclair
206b1a1ac9 Threadlocal_initializer_can_be_made_const will not trigger for unreachable initializers
This commit introduces a check to ensure that the lint won't trigger when the initializer is
unreachable, such as:

```
thread_local! {
    static STATE: Cell<usize> = panic!();
}
```

This is achieved by looking at the unpeeled initializer expression and ensuring that the parent
macro is not `panic!()`, `todo!()`, `unreachable!()`, `unimplemented!()`.

fixes #12637

changelog: [`threadlocal_initializer_can_be_made_const`] will no longer trigger on `unreachable` macros.
2024-04-19 23:21:33 +02:00
Quinn Sinclair
08459b4dae Added msrv to threadlocal initializer 2024-03-03 15:43:09 +01:00
Quinn Sinclair
bb1ee8746e Fixed FP for thread_local_initializer_can_be_made_const for os_local
`os_local` impl of `thread_local` — regardless of whether it is const and
unlike other implementations — includes an `fn __init(): EXPR`.

Existing implementation of the lint checked for the presence of said
function and whether the expr can be made const. Because for `os_local`
we always have an `__init()`, it triggers for const implementations.

The solution is to check whether the `__init()` function is already const.
If it is `const`, there is nothing to do. Otherwise, we verify that we can
make it const.

Co-authored-by: Alejandra González <blyxyas@gmail.com>
2024-03-01 00:41:14 +01:00
Quinn Sinclair
70024e16c0 New Lint: [thread_local_initializer_can_be_made_const]
Adds a new lint to suggest using `const` on `thread_local!`
initializers that can be evaluated at compile time.

Impl details:

The lint relies on the expansion of `thread_local!`. For non
const-labelled initializers, `thread_local!` produces a function
called `__init` that lazily initializes the value. We check the function
and decide whether the body can be const. The body of the function is
exactly the initializer. If so, we lint the body.

changelog: new lint [`thread_local_initializer_can_be_made_const`]
2024-01-01 18:06:10 +02:00