bb1ee8746e
`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>
38 lines
1.3 KiB
Rust
38 lines
1.3 KiB
Rust
#![warn(clippy::thread_local_initializer_can_be_made_const)]
|
|
|
|
use std::cell::RefCell;
|
|
|
|
fn main() {
|
|
// lint and suggest const
|
|
thread_local! {
|
|
static BUF_1: RefCell<String> = const { RefCell::new(String::new()) };
|
|
}
|
|
//~^^ ERROR: initializer for `thread_local` value can be made `const`
|
|
|
|
// don't lint
|
|
thread_local! {
|
|
static BUF_2: RefCell<String> = const { RefCell::new(String::new()) };
|
|
}
|
|
|
|
thread_local! {
|
|
static SIMPLE:i32 = const { 1 };
|
|
}
|
|
//~^^ ERROR: initializer for `thread_local` value can be made `const`
|
|
|
|
// lint and suggest const for all non const items
|
|
thread_local! {
|
|
static BUF_3_CAN_BE_MADE_CONST: RefCell<String> = const { RefCell::new(String::new()) };
|
|
static CONST_MIXED_WITH:i32 = const { 1 };
|
|
static BUF_4_CAN_BE_MADE_CONST: RefCell<String> = const { RefCell::new(String::new()) };
|
|
}
|
|
//~^^^^ ERROR: initializer for `thread_local` value can be made `const`
|
|
//~^^^ ERROR: initializer for `thread_local` value can be made `const`
|
|
|
|
thread_local! {
|
|
static PEEL_ME: i32 = const { 1 };
|
|
//~^ ERROR: initializer for `thread_local` value can be made `const`
|
|
static PEEL_ME_MANY: i32 = const { { let x = 1; x * x } };
|
|
//~^ ERROR: initializer for `thread_local` value can be made `const`
|
|
}
|
|
}
|