1 use crate::utils::{match_def_path, span_lint_and_help};
2 use if_chain::if_chain;
3 use rustc_hir::def_id::DefId;
4 use rustc_hir::{Expr, ExprKind};
5 use rustc_lint::{LateContext, LateLintPass};
7 use rustc_session::{declare_lint_pass, declare_tool_lint};
10 /// **What it does:** Checks for usage of invalid atomic
11 /// ordering in atomic loads/stores and memory fences.
13 /// **Why is this bad?** Using an invalid atomic ordering
14 /// will cause a panic at run-time.
16 /// **Known problems:** None.
20 /// # use std::sync::atomic::{self, AtomicBool, Ordering};
22 /// let x = AtomicBool::new(true);
24 /// let _ = x.load(Ordering::Release);
25 /// let _ = x.load(Ordering::AcqRel);
27 /// x.store(false, Ordering::Acquire);
28 /// x.store(false, Ordering::AcqRel);
30 /// atomic::fence(Ordering::Relaxed);
31 /// atomic::compiler_fence(Ordering::Relaxed);
33 pub INVALID_ATOMIC_ORDERING,
35 "usage of invalid atomic ordering in atomic loads/stores and memory fences"
38 declare_lint_pass!(AtomicOrdering => [INVALID_ATOMIC_ORDERING]);
40 const ATOMIC_TYPES: [&str; 12] = [
55 fn type_is_atomic(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool {
56 if let ty::Adt(&ty::AdtDef { did, .. }, _) = cx.tables.expr_ty(expr).kind {
59 .any(|ty| match_def_path(cx, did, &["core", "sync", "atomic", ty]))
65 fn match_ordering_def_path(cx: &LateContext<'_, '_>, did: DefId, orderings: &[&str]) -> bool {
68 .any(|ordering| match_def_path(cx, did, &["core", "sync", "atomic", "Ordering", ordering]))
71 fn check_atomic_load_store(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
73 if let ExprKind::MethodCall(ref method_path, _, args) = &expr.kind;
74 let method = method_path.ident.name.as_str();
75 if type_is_atomic(cx, &args[0]);
76 if method == "load" || method == "store";
77 let ordering_arg = if method == "load" { &args[1] } else { &args[2] };
78 if let ExprKind::Path(ref ordering_qpath) = ordering_arg.kind;
79 if let Some(ordering_def_id) = cx.tables.qpath_res(ordering_qpath, ordering_arg.hir_id).opt_def_id();
81 if method == "load" &&
82 match_ordering_def_path(cx, ordering_def_id, &["Release", "AcqRel"]) {
85 INVALID_ATOMIC_ORDERING,
87 "atomic loads cannot have `Release` and `AcqRel` ordering",
88 "consider using ordering modes `Acquire`, `SeqCst` or `Relaxed`"
90 } else if method == "store" &&
91 match_ordering_def_path(cx, ordering_def_id, &["Acquire", "AcqRel"]) {
94 INVALID_ATOMIC_ORDERING,
96 "atomic stores cannot have `Acquire` and `AcqRel` ordering",
97 "consider using ordering modes `Release`, `SeqCst` or `Relaxed`"
104 fn check_memory_fence(cx: &LateContext<'_, '_>, expr: &Expr<'_>) {
106 if let ExprKind::Call(ref func, ref args) = expr.kind;
107 if let ExprKind::Path(ref func_qpath) = func.kind;
108 if let Some(def_id) = cx.tables.qpath_res(func_qpath, func.hir_id).opt_def_id();
109 if ["fence", "compiler_fence"]
111 .any(|func| match_def_path(cx, def_id, &["core", "sync", "atomic", func]));
112 if let ExprKind::Path(ref ordering_qpath) = &args[0].kind;
113 if let Some(ordering_def_id) = cx.tables.qpath_res(ordering_qpath, args[0].hir_id).opt_def_id();
114 if match_ordering_def_path(cx, ordering_def_id, &["Relaxed"]);
118 INVALID_ATOMIC_ORDERING,
120 "memory fences cannot have `Relaxed` ordering",
121 "consider using ordering modes `Acquire`, `Release`, `AcqRel` or `SeqCst`"
127 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AtomicOrdering {
128 fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr<'_>) {
129 check_atomic_load_store(cx, expr);
130 check_memory_fence(cx, expr);