Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions clippy_lints/src/loops/empty_loop.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
use super::EMPTY_LOOP;
use clippy_utils::diagnostics::span_lint_and_help;
use clippy_utils::{is_in_panic_handler, is_no_std_crate};
use clippy_utils::{is_in_panic_handler, is_no_std_crate, sym};

use rustc_hir::{Block, Expr};
use rustc_hir::{Block, Expr, ItemKind, Node};
use rustc_lint::LateContext;

pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, loop_block: &Block<'_>) {
let parent_hir_id = cx.tcx.parent_hir_id(expr.hir_id);
if let Node::Item(parent_node) = cx.tcx.hir_node(parent_hir_id)
&& matches!(parent_node.kind, ItemKind::Fn { .. })
&& let attrs = cx.tcx.hir_attrs(parent_hir_id)
&& attrs.iter().any(|attr| attr.has_name(sym::rustc_intrinsic))
{
// Intrinsic functions are expanded into an empty loop when lowering the AST
// to simplify the job of later passes which might expect any function to have a body.
return;
}

if loop_block.stmts.is_empty() && loop_block.expr.is_none() && !is_in_panic_handler(cx, expr) {
let msg = "empty `loop {}` wastes CPU cycles";
let help = if is_no_std_crate(cx) {
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/empty_loop_intrinsic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//@check-pass

#![warn(clippy::empty_loop)]
#![feature(intrinsics)]
#![feature(rustc_attrs)]

// From issue #15200
#[rustc_intrinsic]
#[rustc_nounwind]
/// # Safety
pub const unsafe fn simd_insert<T, U>(x: T, idx: u32, val: U) -> T;

fn main() {}
Loading