|
| 1 | +use clippy_config::msrvs::Msrv; |
| 2 | +use clippy_utils::diagnostics::span_lint_and_sugg; |
| 3 | +use clippy_utils::fn_has_unsatisfiable_preds; |
| 4 | +use clippy_utils::qualify_min_const_fn::is_min_const_fn; |
| 5 | +use clippy_utils::source::snippet; |
| 6 | +use rustc_errors::Applicability; |
| 7 | +use rustc_hir::{intravisit, ExprKind}; |
| 8 | +use rustc_lint::{LateContext, LateLintPass, LintContext}; |
| 9 | +use rustc_middle::lint::in_external_macro; |
| 10 | +use rustc_session::impl_lint_pass; |
| 11 | +use rustc_span::sym::thread_local_macro; |
| 12 | + |
| 13 | +declare_clippy_lint! { |
| 14 | + /// ### What it does |
| 15 | + /// Suggests to use `const` in `thread_local!` macro if possible. |
| 16 | + /// ### Why is this bad? |
| 17 | + /// |
| 18 | + /// The `thread_local!` macro wraps static declarations and makes them thread-local. |
| 19 | + /// It supports using a `const` keyword that may be used for declarations that can |
| 20 | + /// be evaluated as a constant expression. This can enable a more efficient thread |
| 21 | + /// local implementation that can avoid lazy initialization. For types that do not |
| 22 | + /// need to be dropped, this can enable an even more efficient implementation that |
| 23 | + /// does not need to track any additional state. |
| 24 | + /// |
| 25 | + /// https://doc.rust-lang.org/std/macro.thread_local.html |
| 26 | + /// |
| 27 | + /// ### Example |
| 28 | + /// ```no_run |
| 29 | + /// // example code where clippy issues a warning |
| 30 | + /// thread_local! { |
| 31 | + /// static BUF: String = String::new(); |
| 32 | + /// } |
| 33 | + /// ``` |
| 34 | + /// Use instead: |
| 35 | + /// ```no_run |
| 36 | + /// // example code which does not raise clippy warning |
| 37 | + /// thread_local! { |
| 38 | + /// static BUF: String = const { String::new() }; |
| 39 | + /// } |
| 40 | + /// ``` |
| 41 | + #[clippy::version = "1.76.0"] |
| 42 | + pub THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST, |
| 43 | + perf, |
| 44 | + "suggest using `const` in `thread_local!` macro" |
| 45 | +} |
| 46 | + |
| 47 | +pub struct ThreadLocalInitializerCanBeMadeConst { |
| 48 | + msrv: Msrv, |
| 49 | +} |
| 50 | + |
| 51 | +impl ThreadLocalInitializerCanBeMadeConst { |
| 52 | + #[must_use] |
| 53 | + pub fn new(msrv: Msrv) -> Self { |
| 54 | + Self { msrv } |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl_lint_pass!(ThreadLocalInitializerCanBeMadeConst => [THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST]); |
| 59 | + |
| 60 | +impl<'tcx> LateLintPass<'tcx> for ThreadLocalInitializerCanBeMadeConst { |
| 61 | + fn check_fn( |
| 62 | + &mut self, |
| 63 | + cx: &LateContext<'tcx>, |
| 64 | + fn_kind: rustc_hir::intravisit::FnKind<'tcx>, |
| 65 | + _: &'tcx rustc_hir::FnDecl<'tcx>, |
| 66 | + body: &'tcx rustc_hir::Body<'tcx>, |
| 67 | + span: rustc_span::Span, |
| 68 | + defid: rustc_span::def_id::LocalDefId, |
| 69 | + ) { |
| 70 | + if in_external_macro(cx.sess(), span) |
| 71 | + && let Some(callee) = span.source_callee() |
| 72 | + && let Some(macro_def_id) = callee.macro_def_id |
| 73 | + && cx.tcx.is_diagnostic_item(thread_local_macro, macro_def_id) |
| 74 | + && let intravisit::FnKind::ItemFn(..) = fn_kind |
| 75 | + // Building MIR for `fn`s with unsatisfiable preds results in ICE. |
| 76 | + && !fn_has_unsatisfiable_preds(cx, defid.to_def_id()) |
| 77 | + && let mir = cx.tcx.optimized_mir(defid.to_def_id()) |
| 78 | + && let Ok(()) = is_min_const_fn(cx.tcx, mir, &self.msrv) |
| 79 | + // this is the `__init` function emitted by the `thread_local!` macro |
| 80 | + // when the `const` keyword is not used. We avoid checking the `__init` directly |
| 81 | + // as that is not a public API. |
| 82 | + // we know that the function is const-qualifiable, so now we need only to get the |
| 83 | + // initializer expression to span-lint it. |
| 84 | + && let ExprKind::Block(block, _) = body.value.kind |
| 85 | + && let Some(ret_expr) = block.expr |
| 86 | + && let initializer_snippet = snippet(cx, ret_expr.span, "thread_local! { ... }") |
| 87 | + && initializer_snippet != "thread_local! { ... }" |
| 88 | + { |
| 89 | + span_lint_and_sugg( |
| 90 | + cx, |
| 91 | + THREAD_LOCAL_INITIALIZER_CAN_BE_MADE_CONST, |
| 92 | + ret_expr.span, |
| 93 | + "initializer for `thread_local` value can be made `const`", |
| 94 | + "replace with", |
| 95 | + format!("const {{ {initializer_snippet} }}"), |
| 96 | + Applicability::MachineApplicable, |
| 97 | + ); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + extract_msrv_attr!(LateContext); |
| 102 | +} |
0 commit comments