@@ -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,37 @@ 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+ /// ### Example
574+ ///
575+ /// ```rust
576+ /// use core::sync::atomic::{AtomicBool, Ordering};
577+ /// let b = AtomicBool::new(true);
578+ /// // give a ref to `b` to another thread,wait for it to become false
579+ /// while b.load(Ordering::Acquire) {};
580+ /// ```
581+ /// Use instead:
582+ /// ```rust
583+ ///# use core::sync::atomic::{AtomicBool, Ordering};
584+ ///# let b = AtomicBool::new(true);
585+ /// while b.load(Ordering::Acquire) {
586+ /// std::hint::spin_loop()
587+ /// }
588+ /// ```
589+ #[ clippy:: version = "1.59.0" ]
590+ pub MISSING_SPIN_LOOP ,
591+ perf,
592+ "An empty busy waiting loop"
593+ }
594+
563595declare_lint_pass ! ( Loops => [
564596 MANUAL_MEMCPY ,
565597 MANUAL_FLATTEN ,
@@ -579,6 +611,7 @@ declare_lint_pass!(Loops => [
579611 WHILE_IMMUTABLE_CONDITION ,
580612 SAME_ITEM_PUSH ,
581613 SINGLE_ELEMENT_LOOP ,
614+ MISSING_SPIN_LOOP ,
582615] ) ;
583616
584617impl < ' tcx > LateLintPass < ' tcx > for Loops {
@@ -628,6 +661,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
628661
629662 if let Some ( higher:: While { condition, body } ) = higher:: While :: hir ( expr) {
630663 while_immutable_condition:: check ( cx, condition, body) ;
664+ missing_spin_loop:: check ( cx, condition, body) ;
631665 }
632666
633667 needless_collect:: check ( expr, cx) ;
0 commit comments