-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
171 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,33 @@ | ||
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; | ||
|
||
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, _ordering], _) = cond.kind; | ||
if method.ident.name == sym::load; | ||
//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,17 @@ | ||
// run-rustfix | ||
#![warn(clippy::missing_spin_loop)] | ||
|
||
use core::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
fn main() { | ||
let b = AtomicBool::new(true); | ||
// This should lint | ||
while b.load(Ordering::Acquire) { 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 compare_exchange(_weak), but that could be | ||
// 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,17 @@ | ||
// run-rustfix | ||
#![warn(clippy::missing_spin_loop)] | ||
|
||
use core::sync::atomic::{AtomicBool, Ordering}; | ||
|
||
fn main() { | ||
let b = AtomicBool::new(true); | ||
// This should lint | ||
while b.load(Ordering::Acquire) {} | ||
|
||
// This is OK, as the body is not empty | ||
while b.load(Ordering::Acquire) { | ||
std::hint::spin_loop() | ||
} | ||
// TODO: also match on compare_exchange(_weak), but that could be | ||
// 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,10 @@ | ||
error: busy-waiting loop should at least have a spin loop hint | ||
--> $DIR/missing_spin_loop.rs:9: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: aborting due to previous error | ||
|
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 | ||
|