@@ -7,6 +7,7 @@ mod for_loops_over_fallibles;
77mod iter_next_loop;
88mod manual_flatten;
99mod manual_memcpy;
10+ mod missing_spin_loop;
1011mod mut_range_bound;
1112mod needless_collect;
1213mod needless_range_loop;
@@ -560,6 +561,42 @@ declare_clippy_lint! {
560561 "for loops over `Option`s or `Result`s with a single expression can be simplified"
561562}
562563
564+ declare_clippy_lint ! {
565+ /// ### What it does
566+ /// Check for empty spin loops
567+ ///
568+ /// ### Why is this bad?
569+ /// The loop body should have something like `thread::park()` or at least
570+ /// `std::hint::spin_loop()` to avoid needlessly burning cycles and conserve
571+ /// energy. Perhaps even better use an actual lock, if possible.
572+ ///
573+ /// ### Known problems
574+ /// This lint doesn't currently trigger on `while let` or
575+ /// `loop { match .. { .. } }` loops, which would be considered idiomatic in
576+ /// combination with e.g. `AtomicBool::compare_exchange_weak`.
577+ ///
578+ /// ### Example
579+ ///
580+ /// ```ignore
581+ /// use core::sync::atomic::{AtomicBool, Ordering};
582+ /// let b = AtomicBool::new(true);
583+ /// // give a ref to `b` to another thread,wait for it to become false
584+ /// while b.load(Ordering::Acquire) {};
585+ /// ```
586+ /// Use instead:
587+ /// ```rust,no_run
588+ ///# use core::sync::atomic::{AtomicBool, Ordering};
589+ ///# let b = AtomicBool::new(true);
590+ /// while b.load(Ordering::Acquire) {
591+ /// std::hint::spin_loop()
592+ /// }
593+ /// ```
594+ #[ clippy:: version = "1.59.0" ]
595+ pub MISSING_SPIN_LOOP ,
596+ perf,
597+ "An empty busy waiting loop"
598+ }
599+
563600declare_lint_pass ! ( Loops => [
564601 MANUAL_MEMCPY ,
565602 MANUAL_FLATTEN ,
@@ -579,6 +616,7 @@ declare_lint_pass!(Loops => [
579616 WHILE_IMMUTABLE_CONDITION ,
580617 SAME_ITEM_PUSH ,
581618 SINGLE_ELEMENT_LOOP ,
619+ MISSING_SPIN_LOOP ,
582620] ) ;
583621
584622impl < ' tcx > LateLintPass < ' tcx > for Loops {
@@ -628,6 +666,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
628666
629667 if let Some ( higher:: While { condition, body } ) = higher:: While :: hir ( expr) {
630668 while_immutable_condition:: check ( cx, condition, body) ;
669+ missing_spin_loop:: check ( cx, condition, body) ;
631670 }
632671
633672 needless_collect:: check ( expr, cx) ;
0 commit comments