Skip to content

Commit 1ccd284

Browse files
committed
Auto merge of #5086 - Areredify:issue-3746, r=phansch
don't fire `empty_loop` in `no_std` crates closes #3746. changelog: move no_std detection to utils, don't fire empty_loop in no_std crates
2 parents c48594c + 634774b commit 1ccd284

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

clippy_lints/src/loops.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use crate::utils::paths;
44
use crate::utils::usage::{is_unused, mutated_variables};
55
use crate::utils::{
66
get_enclosing_block, get_parent_expr, get_trait_def_id, has_iter_method, higher, implements_trait,
7-
is_integer_const, is_refutable, last_path_segment, match_trait_method, match_type, match_var, multispan_sugg,
8-
snippet, snippet_opt, snippet_with_applicability, span_help_and_lint, span_lint, span_lint_and_sugg,
9-
span_lint_and_then, SpanlessEq,
7+
is_integer_const, is_no_std_crate, is_refutable, last_path_segment, match_trait_method, match_type, match_var,
8+
multispan_sugg, snippet, snippet_opt, snippet_with_applicability, span_help_and_lint, span_lint,
9+
span_lint_and_sugg, span_lint_and_then, SpanlessEq,
1010
};
1111
use crate::utils::{is_type_diagnostic_item, qpath_res, same_tys, sext, sugg};
1212
use if_chain::if_chain;
@@ -502,7 +502,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Loops {
502502
// (even if the "match" or "if let" is used for declaration)
503503
if let ExprKind::Loop(ref block, _, LoopSource::Loop) = expr.kind {
504504
// also check for empty `loop {}` statements
505-
if block.stmts.is_empty() && block.expr.is_none() {
505+
if block.stmts.is_empty() && block.expr.is_none() && !is_no_std_crate(cx.tcx.hir().krate()) {
506506
span_lint(
507507
cx,
508508
EMPTY_LOOP,

clippy_lints/src/main_recursion.rs

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
use rustc_hir::{Crate, Expr, ExprKind, QPath};
22
use rustc_lint::{LateContext, LateLintPass};
33
use rustc_session::{declare_tool_lint, impl_lint_pass};
4-
use rustc_span::symbol::sym;
5-
use syntax::ast::AttrKind;
64

7-
use crate::utils::{is_entrypoint_fn, snippet, span_help_and_lint};
5+
use crate::utils::{is_entrypoint_fn, is_no_std_crate, snippet, span_help_and_lint};
86
use if_chain::if_chain;
97

108
declare_clippy_lint! {
@@ -35,13 +33,7 @@ impl_lint_pass!(MainRecursion => [MAIN_RECURSION]);
3533

3634
impl LateLintPass<'_, '_> for MainRecursion {
3735
fn check_crate(&mut self, _: &LateContext<'_, '_>, krate: &Crate<'_>) {
38-
self.has_no_std_attr = krate.attrs.iter().any(|attr| {
39-
if let AttrKind::Normal(ref attr) = attr.kind {
40-
attr.path == sym::no_std
41-
} else {
42-
false
43-
}
44-
});
36+
self.has_no_std_attr = is_no_std_crate(krate);
4537
}
4638

4739
fn check_expr_post(&mut self, cx: &LateContext<'_, '_>, expr: &Expr<'_>) {

clippy_lints/src/utils/mod.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use rustc_hir::Node;
4343
use rustc_hir::*;
4444
use rustc_lint::{LateContext, Level, Lint, LintContext};
4545
use rustc_span::hygiene::ExpnKind;
46-
use rustc_span::symbol::{kw, Symbol};
46+
use rustc_span::symbol::{self, kw, Symbol};
4747
use rustc_span::{BytePos, Pos, Span, DUMMY_SP};
4848
use smallvec::SmallVec;
4949
use syntax::ast::{self, Attribute, LitKind};
@@ -1344,3 +1344,13 @@ pub fn is_must_use_func_call(cx: &LateContext<'_, '_>, expr: &Expr<'_>) -> bool
13441344
false
13451345
}
13461346
}
1347+
1348+
pub fn is_no_std_crate(krate: &Crate<'_>) -> bool {
1349+
krate.attrs.iter().any(|attr| {
1350+
if let ast::AttrKind::Normal(ref attr) = attr.kind {
1351+
attr.path == symbol::sym::no_std
1352+
} else {
1353+
false
1354+
}
1355+
})
1356+
}

tests/ui/issue-3746.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// ignore-macos
2+
// ignore-windows
3+
4+
#![warn(clippy::empty_loop)]
5+
#![feature(lang_items, link_args, start, libc)]
6+
#![link_args = "-nostartfiles"]
7+
#![no_std]
8+
9+
use core::panic::PanicInfo;
10+
11+
#[start]
12+
fn main(argc: isize, argv: *const *const u8) -> isize {
13+
loop {}
14+
}
15+
16+
#[panic_handler]
17+
fn panic(_info: &PanicInfo) -> ! {
18+
loop {}
19+
}
20+
21+
#[lang = "eh_personality"]
22+
extern "C" fn eh_personality() {}

0 commit comments

Comments
 (0)