70024e16c0
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`]
30 lines
1.5 KiB
Plaintext
30 lines
1.5 KiB
Plaintext
error: initializer for `thread_local` value can be made `const`
|
|
--> $DIR/thread_local_initializer_can_be_made_const.rs:8:41
|
|
|
|
|
LL | static BUF_1: RefCell<String> = RefCell::new(String::new());
|
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { RefCell::new(String::new()) }`
|
|
|
|
|
= note: `-D clippy::thread-local-initializer-can-be-made-const` implied by `-D warnings`
|
|
= help: to override `-D warnings` add `#[allow(clippy::thread_local_initializer_can_be_made_const)]`
|
|
|
|
error: initializer for `thread_local` value can be made `const`
|
|
--> $DIR/thread_local_initializer_can_be_made_const.rs:18:29
|
|
|
|
|
LL | static SIMPLE:i32 = 1;
|
|
| ^ help: replace with: `const { 1 }`
|
|
|
|
error: initializer for `thread_local` value can be made `const`
|
|
--> $DIR/thread_local_initializer_can_be_made_const.rs:24:59
|
|
|
|
|
LL | static BUF_3_CAN_BE_MADE_CONST: RefCell<String> = RefCell::new(String::new());
|
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { RefCell::new(String::new()) }`
|
|
|
|
error: initializer for `thread_local` value can be made `const`
|
|
--> $DIR/thread_local_initializer_can_be_made_const.rs:26:59
|
|
|
|
|
LL | static BUF_4_CAN_BE_MADE_CONST: RefCell<String> = RefCell::new(String::new());
|
|
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { RefCell::new(String::new()) }`
|
|
|
|
error: aborting due to 4 previous errors
|
|
|