-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #8174 - rust-lang:missing-spin-loop, r=flip1995
new lint: `missing-spin-loop` This fixes #7809. I went with the shorter name because the function is called `std::hint::spin_loop`. It doesn't yet detect `while let` loops. I left that for a follow-up PR. --- changelog: new lint: [`missing_spin_loop`]
- Loading branch information
Showing
12 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use super::MISSING_SPIN_LOOP; | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::is_no_std_crate; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Block, Expr, ExprKind}; | ||
use rustc_lint::LateContext; | ||
use rustc_middle::ty; | ||
use rustc_span::sym; | ||
|
||
fn unpack_cond<'tcx>(cond: &'tcx Expr<'tcx>) -> &'tcx Expr<'tcx> { | ||
match &cond.kind { | ||
ExprKind::Block( | ||
Block { | ||
stmts: [], | ||
expr: Some(e), | ||
.. | ||
}, | ||
_, | ||
) | ||
| ExprKind::Unary(_, e) => unpack_cond(e), | ||
ExprKind::Binary(_, l, r) => { | ||
let l = unpack_cond(l); | ||
if let ExprKind::MethodCall(..) = l.kind { | ||
l | ||
} else { | ||
unpack_cond(r) | ||
} | ||
}, | ||
_ => cond, | ||
} | ||
} | ||
|
||
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, cond: &'tcx Expr<'_>, body: &'tcx Expr<'_>) { | ||
if_chain! { | ||
if let ExprKind::Block(Block { stmts: [], expr: None, ..}, _) = body.kind; | ||
if let ExprKind::MethodCall(method, [callee, ..], _) = unpack_cond(cond).kind; | ||
if [sym::load, sym::compare_exchange, sym::compare_exchange_weak].contains(&method.ident.name); | ||
if let ty::Adt(def, _substs) = cx.typeck_results().expr_ty(callee).kind(); | ||
if cx.tcx.is_diagnostic_item(sym::AtomicBool, def.did); | ||
then { | ||
span_lint_and_sugg( | ||
cx, | ||
MISSING_SPIN_LOOP, | ||
body.span, | ||
"busy-waiting loop should at least have a spin loop hint", | ||
"try this", | ||
(if is_no_std_crate(cx) { | ||
"{ core::hint::spin_loop() }" | ||
} else { | ||
"{ std::hint::spin_loop() }" | ||
}).into(), | ||
Applicability::MachineApplicable | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// run-rustfix | ||
#![warn(clippy::missing_spin_loop)] | ||
#![allow(clippy::bool_comparison)] | ||
#![allow(unused_braces)] | ||
|
||
use core::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
fn main() { | ||
let b = AtomicBool::new(true); | ||
// Those should lint | ||
while b.load(Ordering::Acquire) { std::hint::spin_loop() } | ||
|
||
while !b.load(Ordering::SeqCst) { std::hint::spin_loop() } | ||
|
||
while b.load(Ordering::Acquire) == false { std::hint::spin_loop() } | ||
|
||
while { true == b.load(Ordering::Acquire) } { std::hint::spin_loop() } | ||
|
||
while b.compare_exchange(true, false, Ordering::Acquire, Ordering::Relaxed) != Ok(true) { std::hint::spin_loop() } | ||
|
||
while Ok(false) != b.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) { std::hint::spin_loop() } | ||
|
||
// This is OK, as the body is not empty | ||
while b.load(Ordering::Acquire) { | ||
std::hint::spin_loop() | ||
} | ||
// TODO: also match on loop+match or while let | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// run-rustfix | ||
#![warn(clippy::missing_spin_loop)] | ||
#![allow(clippy::bool_comparison)] | ||
#![allow(unused_braces)] | ||
|
||
use core::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
fn main() { | ||
let b = AtomicBool::new(true); | ||
// Those should lint | ||
while b.load(Ordering::Acquire) {} | ||
|
||
while !b.load(Ordering::SeqCst) {} | ||
|
||
while b.load(Ordering::Acquire) == false {} | ||
|
||
while { true == b.load(Ordering::Acquire) } {} | ||
|
||
while b.compare_exchange(true, false, Ordering::Acquire, Ordering::Relaxed) != Ok(true) {} | ||
|
||
while Ok(false) != b.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) {} | ||
|
||
// This is OK, as the body is not empty | ||
while b.load(Ordering::Acquire) { | ||
std::hint::spin_loop() | ||
} | ||
// TODO: also match on loop+match or while let | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
error: busy-waiting loop should at least have a spin loop hint | ||
--> $DIR/missing_spin_loop.rs:11:37 | ||
| | ||
LL | while b.load(Ordering::Acquire) {} | ||
| ^^ help: try this: `{ std::hint::spin_loop() }` | ||
| | ||
= note: `-D clippy::missing-spin-loop` implied by `-D warnings` | ||
|
||
error: busy-waiting loop should at least have a spin loop hint | ||
--> $DIR/missing_spin_loop.rs:13:37 | ||
| | ||
LL | while !b.load(Ordering::SeqCst) {} | ||
| ^^ help: try this: `{ std::hint::spin_loop() }` | ||
|
||
error: busy-waiting loop should at least have a spin loop hint | ||
--> $DIR/missing_spin_loop.rs:15:46 | ||
| | ||
LL | while b.load(Ordering::Acquire) == false {} | ||
| ^^ help: try this: `{ std::hint::spin_loop() }` | ||
|
||
error: busy-waiting loop should at least have a spin loop hint | ||
--> $DIR/missing_spin_loop.rs:17:49 | ||
| | ||
LL | while { true == b.load(Ordering::Acquire) } {} | ||
| ^^ help: try this: `{ std::hint::spin_loop() }` | ||
|
||
error: busy-waiting loop should at least have a spin loop hint | ||
--> $DIR/missing_spin_loop.rs:19:93 | ||
| | ||
LL | while b.compare_exchange(true, false, Ordering::Acquire, Ordering::Relaxed) != Ok(true) {} | ||
| ^^ help: try this: `{ std::hint::spin_loop() }` | ||
|
||
error: busy-waiting loop should at least have a spin loop hint | ||
--> $DIR/missing_spin_loop.rs:21:94 | ||
| | ||
LL | while Ok(false) != b.compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed) {} | ||
| ^^ help: try this: `{ std::hint::spin_loop() }` | ||
|
||
error: aborting due to 6 previous errors | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// run-rustfix | ||
#![warn(clippy::missing_spin_loop)] | ||
#![feature(lang_items, start, libc)] | ||
#![no_std] | ||
|
||
use core::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
#[start] | ||
fn main(_argc: isize, _argv: *const *const u8) -> isize { | ||
// This should trigger the lint | ||
let b = AtomicBool::new(true); | ||
// This should lint with `core::hint::spin_loop()` | ||
while b.load(Ordering::Acquire) { core::hint::spin_loop() } | ||
0 | ||
} | ||
|
||
#[panic_handler] | ||
fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// run-rustfix | ||
#![warn(clippy::missing_spin_loop)] | ||
#![feature(lang_items, start, libc)] | ||
#![no_std] | ||
|
||
use core::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
#[start] | ||
fn main(_argc: isize, _argv: *const *const u8) -> isize { | ||
// This should trigger the lint | ||
let b = AtomicBool::new(true); | ||
// This should lint with `core::hint::spin_loop()` | ||
while b.load(Ordering::Acquire) {} | ||
0 | ||
} | ||
|
||
#[panic_handler] | ||
fn panic(_info: &core::panic::PanicInfo) -> ! { | ||
loop {} | ||
} | ||
|
||
#[lang = "eh_personality"] | ||
extern "C" fn eh_personality() {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
error: busy-waiting loop should at least have a spin loop hint | ||
--> $DIR/missing_spin_loop_no_std.rs:13:37 | ||
| | ||
LL | while b.load(Ordering::Acquire) {} | ||
| ^^ help: try this: `{ core::hint::spin_loop() }` | ||
| | ||
= note: `-D clippy::missing-spin-loop` implied by `-D warnings` | ||
|
||
error: aborting due to previous error | ||
|