Add check for 'in_external_macro' and 'is_from_proc_macro' inside [infinite_loop] lint.

This commit is contained in:
Christopher B. Speir 2024-02-20 20:07:54 -06:00
parent 250fd09405
commit b72996e322
3 changed files with 52 additions and 20 deletions

View File

@ -1,17 +1,18 @@
use clippy_utils::diagnostics::span_lint_and_then; use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::{fn_def_id, is_lint_allowed}; use clippy_utils::{fn_def_id, is_from_proc_macro, is_lint_allowed};
use hir::intravisit::{walk_expr, Visitor}; use hir::intravisit::{walk_expr, Visitor};
use hir::{Expr, ExprKind, FnRetTy, FnSig, Node}; use hir::{Expr, ExprKind, FnRetTy, FnSig, Node};
use rustc_ast::Label; use rustc_ast::Label;
use rustc_errors::Applicability; use rustc_errors::Applicability;
use rustc_hir as hir; use rustc_hir as hir;
use rustc_lint::LateContext; use rustc_lint::{LateContext, LintContext};
use rustc_middle::lint::in_external_macro;
use super::INFINITE_LOOP; use super::INFINITE_LOOP;
pub(super) fn check<'tcx>( pub(super) fn check<'tcx>(
cx: &LateContext<'tcx>, cx: &LateContext<'tcx>,
expr: &Expr<'_>, expr: &Expr<'tcx>,
loop_block: &'tcx hir::Block<'_>, loop_block: &'tcx hir::Block<'_>,
label: Option<Label>, label: Option<Label>,
) { ) {
@ -34,6 +35,10 @@ pub(super) fn check<'tcx>(
return; return;
} }
if in_external_macro(cx.sess(), expr.span) || is_from_proc_macro(cx, expr) {
return;
}
let mut loop_visitor = LoopVisitor { let mut loop_visitor = LoopVisitor {
cx, cx,
label, label,

View File

@ -1,7 +1,12 @@
//@no-rustfix //@no-rustfix
//@aux-build:proc_macros.rs
#![allow(clippy::never_loop)] #![allow(clippy::never_loop)]
#![warn(clippy::infinite_loop)] #![warn(clippy::infinite_loop)]
extern crate proc_macros;
use proc_macros::{external, with_span};
fn do_something() {} fn do_something() {}
fn no_break() { fn no_break() {
@ -363,4 +368,26 @@ fn inf_loop_in_res() -> Result<(), i32> {
}) })
} }
with_span! { span
fn no_loop() {}
}
with_span! { span
fn with_loop() {
loop {
do_nothing();
}
}
}
fn do_nothing() {}
fn span_inside_fn() {
with_span! { span
loop {
do_nothing();
}
}
}
fn main() {} fn main() {}

View File

@ -1,5 +1,5 @@
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:8:5 --> tests/ui/infinite_loops.rs:13:5
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -15,7 +15,7 @@ LL | fn no_break() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:15:5 --> tests/ui/infinite_loops.rs:20:5
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -32,7 +32,7 @@ LL | fn all_inf() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:17:9 --> tests/ui/infinite_loops.rs:22:9
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -49,7 +49,7 @@ LL | fn all_inf() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:19:13 --> tests/ui/infinite_loops.rs:24:13
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -63,7 +63,7 @@ LL | fn all_inf() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:33:5 --> tests/ui/infinite_loops.rs:38:5
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -74,7 +74,7 @@ LL | | }
= help: if this is not intended, try adding a `break` or `return` condition in the loop = help: if this is not intended, try adding a `break` or `return` condition in the loop
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:46:5 --> tests/ui/infinite_loops.rs:51:5
| |
LL | / loop { LL | / loop {
LL | | fn inner_fn() -> ! { LL | | fn inner_fn() -> ! {
@ -90,7 +90,7 @@ LL | fn no_break_never_ret_noise() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:89:5 --> tests/ui/infinite_loops.rs:94:5
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -107,7 +107,7 @@ LL | fn break_inner_but_not_outer_1(cond: bool) -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:100:5 --> tests/ui/infinite_loops.rs:105:5
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -124,7 +124,7 @@ LL | fn break_inner_but_not_outer_2(cond: bool) -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:114:9 --> tests/ui/infinite_loops.rs:119:9
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -138,7 +138,7 @@ LL | fn break_outer_but_not_inner() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:137:9 --> tests/ui/infinite_loops.rs:142:9
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -155,7 +155,7 @@ LL | fn break_wrong_loop(cond: bool) -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:177:5 --> tests/ui/infinite_loops.rs:182:5
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -172,7 +172,7 @@ LL | fn match_like() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:218:5 --> tests/ui/infinite_loops.rs:223:5
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -186,7 +186,7 @@ LL | fn match_like() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:223:5 --> tests/ui/infinite_loops.rs:228:5
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -203,7 +203,7 @@ LL | fn match_like() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:328:9 --> tests/ui/infinite_loops.rs:333:9
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -217,7 +217,7 @@ LL | fn problematic_trait_method() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:338:9 --> tests/ui/infinite_loops.rs:343:9
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -231,7 +231,7 @@ LL | fn could_be_problematic() -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:347:9 --> tests/ui/infinite_loops.rs:352:9
| |
LL | / loop { LL | / loop {
LL | | LL | |
@ -245,7 +245,7 @@ LL | let _loop_forever = || -> ! {
| ++++ | ++++
error: infinite loop detected error: infinite loop detected
--> tests/ui/infinite_loops.rs:361:8 --> tests/ui/infinite_loops.rs:366:8
| |
LL | Ok(loop { LL | Ok(loop {
| ________^ | ________^