Skip to content

Commit a7c1469

Browse files
authored
Do not lint intrinsics as empty loops (#15201)
Fix #15200 changelog: [`empty_loop`]: fix false positive on intrinsic function declaration
2 parents 321542e + 8bc4add commit a7c1469

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

clippy_lints/src/loops/empty_loop.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
use super::EMPTY_LOOP;
22
use clippy_utils::diagnostics::span_lint_and_help;
3-
use clippy_utils::{is_in_panic_handler, is_no_std_crate};
3+
use clippy_utils::{is_in_panic_handler, is_no_std_crate, sym};
44

5-
use rustc_hir::{Block, Expr};
5+
use rustc_hir::{Block, Expr, ItemKind, Node};
66
use rustc_lint::LateContext;
77

88
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, loop_block: &Block<'_>) {
9+
let parent_hir_id = cx.tcx.parent_hir_id(expr.hir_id);
10+
if let Node::Item(parent_node) = cx.tcx.hir_node(parent_hir_id)
11+
&& matches!(parent_node.kind, ItemKind::Fn { .. })
12+
&& let attrs = cx.tcx.hir_attrs(parent_hir_id)
13+
&& attrs.iter().any(|attr| attr.has_name(sym::rustc_intrinsic))
14+
{
15+
// Intrinsic functions are expanded into an empty loop when lowering the AST
16+
// to simplify the job of later passes which might expect any function to have a body.
17+
return;
18+
}
19+
920
if loop_block.stmts.is_empty() && loop_block.expr.is_none() && !is_in_panic_handler(cx, expr) {
1021
let msg = "empty `loop {}` wastes CPU cycles";
1122
let help = if is_no_std_crate(cx) {

tests/ui/empty_loop_intrinsic.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//@check-pass
2+
3+
#![warn(clippy::empty_loop)]
4+
#![feature(intrinsics)]
5+
#![feature(rustc_attrs)]
6+
7+
// From issue #15200
8+
#[rustc_intrinsic]
9+
#[rustc_nounwind]
10+
/// # Safety
11+
pub const unsafe fn simd_insert<T, U>(x: T, idx: u32, val: U) -> T;
12+
13+
fn main() {}

0 commit comments

Comments
 (0)