Skip to content

Rename thread_local_initializer_can_be_made_const to missing_const_for_thread_local #12974

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5589,6 +5589,7 @@ Released 2018-09-13
[`missing_assert_message`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_assert_message
[`missing_asserts_for_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_asserts_for_indexing
[`missing_const_for_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
[`missing_const_for_thread_local`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_thread_local
[`missing_docs_in_private_items`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_docs_in_private_items
[`missing_enforced_import_renames`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_enforced_import_renames
[`missing_errors_doc`]: https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc
Expand Down
2 changes: 1 addition & 1 deletion clippy_config/src/msrvs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ msrv_aliases! {
1,65,0 { LET_ELSE, POINTER_CAST_CONSTNESS }
1,63,0 { CLONE_INTO }
1,62,0 { BOOL_THEN_SOME, DEFAULT_ENUM_ATTRIBUTE, CONST_EXTERN_FN }
1,59,0 { THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST }
1,59,0 { THREAD_LOCAL_CONST_INIT }
1,58,0 { FORMAT_ARGS_CAPTURE, PATTERN_TRAIT_CHAR_ARRAY, CONST_RAW_PTR_DEREF }
1,56,0 { CONST_FN_UNION }
1,55,0 { SEEK_REWIND }
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::missing_assert_message::MISSING_ASSERT_MESSAGE_INFO,
crate::missing_asserts_for_indexing::MISSING_ASSERTS_FOR_INDEXING_INFO,
crate::missing_const_for_fn::MISSING_CONST_FOR_FN_INFO,
crate::missing_const_for_thread_local::MISSING_CONST_FOR_THREAD_LOCAL_INFO,
crate::missing_doc::MISSING_DOCS_IN_PRIVATE_ITEMS_INFO,
crate::missing_enforced_import_rename::MISSING_ENFORCED_IMPORT_RENAMES_INFO,
crate::missing_fields_in_debug::MISSING_FIELDS_IN_DEBUG_INFO,
Expand Down Expand Up @@ -682,7 +683,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::tabs_in_doc_comments::TABS_IN_DOC_COMMENTS_INFO,
crate::temporary_assignment::TEMPORARY_ASSIGNMENT_INFO,
crate::tests_outside_test_module::TESTS_OUTSIDE_TEST_MODULE_INFO,
crate::thread_local_initializer_can_be_made_const::THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST_INFO,
crate::to_digit_is_some::TO_DIGIT_IS_SOME_INFO,
crate::to_string_trait_impl::TO_STRING_TRAIT_IMPL_INFO,
crate::trailing_empty_array::TRAILING_EMPTY_ARRAY_INFO,
Expand Down
7 changes: 3 additions & 4 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ mod mismatching_type_param_order;
mod missing_assert_message;
mod missing_asserts_for_indexing;
mod missing_const_for_fn;
mod missing_const_for_thread_local;
mod missing_doc;
mod missing_enforced_import_rename;
mod missing_fields_in_debug;
Expand Down Expand Up @@ -341,7 +342,6 @@ mod swap_ptr_to_ref;
mod tabs_in_doc_comments;
mod temporary_assignment;
mod tests_outside_test_module;
mod thread_local_initializer_can_be_made_const;
mod to_digit_is_some;
mod to_string_trait_impl;
mod trailing_empty_array;
Expand Down Expand Up @@ -1156,9 +1156,8 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
behavior: pub_underscore_fields_behavior,
})
});
store.register_late_pass(move |_| {
Box::new(thread_local_initializer_can_be_made_const::ThreadLocalInitializerCanBeMadeConst::new(msrv()))
});
store
.register_late_pass(move |_| Box::new(missing_const_for_thread_local::MissingConstForThreadLocal::new(msrv())));
store.register_late_pass(move |_| Box::new(incompatible_msrv::IncompatibleMsrv::new(msrv())));
store.register_late_pass(|_| Box::new(to_string_trait_impl::ToStringTraitImpl));
store.register_early_pass(|| Box::new(multiple_bound_locations::MultipleBoundLocations));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ declare_clippy_lint! {
/// }
/// ```
#[clippy::version = "1.77.0"]
pub THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST,
pub MISSING_CONST_FOR_THREAD_LOCAL,
perf,
"suggest using `const` in `thread_local!` macro"
}

pub struct ThreadLocalInitializerCanBeMadeConst {
pub struct MissingConstForThreadLocal {
msrv: Msrv,
}

impl ThreadLocalInitializerCanBeMadeConst {
impl MissingConstForThreadLocal {
#[must_use]
pub fn new(msrv: Msrv) -> Self {
Self { msrv }
}
}

impl_lint_pass!(ThreadLocalInitializerCanBeMadeConst => [THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST]);
impl_lint_pass!(MissingConstForThreadLocal => [MISSING_CONST_FOR_THREAD_LOCAL]);

#[inline]
fn is_thread_local_initializer(
Expand Down Expand Up @@ -102,7 +102,7 @@ fn initializer_can_be_made_const(cx: &LateContext<'_>, defid: rustc_span::def_id
false
}

impl<'tcx> LateLintPass<'tcx> for ThreadLocalInitializerCanBeMadeConst {
impl<'tcx> LateLintPass<'tcx> for MissingConstForThreadLocal {
fn check_fn(
&mut self,
cx: &LateContext<'tcx>,
Expand All @@ -113,7 +113,7 @@ impl<'tcx> LateLintPass<'tcx> for ThreadLocalInitializerCanBeMadeConst {
local_defid: rustc_span::def_id::LocalDefId,
) {
let defid = local_defid.to_def_id();
if self.msrv.meets(msrvs::THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST)
if self.msrv.meets(msrvs::THREAD_LOCAL_CONST_INIT)
&& is_thread_local_initializer(cx, fn_kind, span).unwrap_or(false)
// Some implementations of `thread_local!` include an initializer fn.
// In the case of a const initializer, the init fn is also const,
Expand All @@ -139,7 +139,7 @@ impl<'tcx> LateLintPass<'tcx> for ThreadLocalInitializerCanBeMadeConst {
{
span_lint_and_sugg(
cx,
THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST,
MISSING_CONST_FOR_THREAD_LOCAL,
unpeeled.span,
"initializer for `thread_local` value can be made `const`",
"replace with",
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/renamed_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pub static RENAMED_LINTS: &[(&str, &str)] = &[
("clippy::result_unwrap_used", "clippy::unwrap_used"),
("clippy::single_char_push_str", "clippy::single_char_add_str"),
("clippy::stutter", "clippy::module_name_repetitions"),
("clippy::thread_local_initializer_can_be_made_const", "clippy::missing_const_for_thread_local"),
("clippy::to_string_in_display", "clippy::recursive_format_impl"),
("clippy::unwrap_or_else_default", "clippy::unwrap_or_default"),
("clippy::zero_width_space", "clippy::invisible_characters"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::thread_local_initializer_can_be_made_const)]
#![warn(clippy::missing_const_for_thread_local)]

use std::cell::{Cell, RefCell};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![warn(clippy::thread_local_initializer_can_be_made_const)]
#![warn(clippy::missing_const_for_thread_local)]

use std::cell::{Cell, RefCell};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
error: initializer for `thread_local` value can be made `const`
--> tests/ui/thread_local_initializer_can_be_made_const.rs:8:41
--> tests/ui/missing_const_for_thread_local.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)]`
= note: `-D clippy::missing-const-for-thread-local` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::missing_const_for_thread_local)]`

error: initializer for `thread_local` value can be made `const`
--> tests/ui/thread_local_initializer_can_be_made_const.rs:18:29
--> tests/ui/missing_const_for_thread_local.rs:18:29
|
LL | static SIMPLE:i32 = 1;
| ^ help: replace with: `const { 1 }`

error: initializer for `thread_local` value can be made `const`
--> tests/ui/thread_local_initializer_can_be_made_const.rs:24:59
--> tests/ui/missing_const_for_thread_local.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`
--> tests/ui/thread_local_initializer_can_be_made_const.rs:26:59
--> tests/ui/missing_const_for_thread_local.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: initializer for `thread_local` value can be made `const`
--> tests/ui/thread_local_initializer_can_be_made_const.rs:32:31
--> tests/ui/missing_const_for_thread_local.rs:32:31
|
LL | static PEEL_ME: i32 = { 1 };
| ^^^^^ help: replace with: `const { 1 }`

error: initializer for `thread_local` value can be made `const`
--> tests/ui/thread_local_initializer_can_be_made_const.rs:34:36
--> tests/ui/missing_const_for_thread_local.rs:34:36
|
LL | static PEEL_ME_MANY: i32 = { let x = 1; x * x };
| ^^^^^^^^^^^^^^^^^^^^ help: replace with: `const { { let x = 1; x * x } }`
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/rename.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#![allow(clippy::needless_borrow)]
#![allow(clippy::single_char_add_str)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::missing_const_for_thread_local)]
#![allow(clippy::recursive_format_impl)]
#![allow(clippy::unwrap_or_default)]
#![allow(clippy::invisible_characters)]
Expand Down Expand Up @@ -83,6 +84,7 @@
#![warn(clippy::unwrap_used)]
#![warn(clippy::single_char_add_str)]
#![warn(clippy::module_name_repetitions)]
#![warn(clippy::missing_const_for_thread_local)]
#![warn(clippy::recursive_format_impl)]
#![warn(clippy::unwrap_or_default)]
#![warn(clippy::invisible_characters)]
Expand Down
2 changes: 2 additions & 0 deletions tests/ui/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#![allow(clippy::needless_borrow)]
#![allow(clippy::single_char_add_str)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::missing_const_for_thread_local)]
#![allow(clippy::recursive_format_impl)]
#![allow(clippy::unwrap_or_default)]
#![allow(clippy::invisible_characters)]
Expand Down Expand Up @@ -83,6 +84,7 @@
#![warn(clippy::result_unwrap_used)]
#![warn(clippy::single_char_push_str)]
#![warn(clippy::stutter)]
#![warn(clippy::thread_local_initializer_can_be_made_const)]
#![warn(clippy::to_string_in_display)]
#![warn(clippy::unwrap_or_else_default)]
#![warn(clippy::zero_width_space)]
Expand Down
Loading
Loading