@@ -7,6 +7,7 @@ mod for_loops_over_fallibles;
7
7
mod iter_next_loop;
8
8
mod manual_flatten;
9
9
mod manual_memcpy;
10
+ mod missing_spin_loop;
10
11
mod mut_range_bound;
11
12
mod needless_collect;
12
13
mod needless_range_loop;
@@ -560,6 +561,37 @@ declare_clippy_lint! {
560
561
"for loops over `Option`s or `Result`s with a single expression can be simplified"
561
562
}
562
563
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
+
563
595
declare_lint_pass ! ( Loops => [
564
596
MANUAL_MEMCPY ,
565
597
MANUAL_FLATTEN ,
@@ -579,6 +611,7 @@ declare_lint_pass!(Loops => [
579
611
WHILE_IMMUTABLE_CONDITION ,
580
612
SAME_ITEM_PUSH ,
581
613
SINGLE_ELEMENT_LOOP ,
614
+ MISSING_SPIN_LOOP ,
582
615
] ) ;
583
616
584
617
impl < ' tcx > LateLintPass < ' tcx > for Loops {
@@ -628,6 +661,7 @@ impl<'tcx> LateLintPass<'tcx> for Loops {
628
661
629
662
if let Some ( higher:: While { condition, body } ) = higher:: While :: hir ( expr) {
630
663
while_immutable_condition:: check ( cx, condition, body) ;
664
+ missing_spin_loop:: check ( cx, condition, body) ;
631
665
}
632
666
633
667
needless_collect:: check ( expr, cx) ;
0 commit comments