Skip to content

Commit e9dbbc2

Browse files
committed
Use is_no_std_crate for swap
1 parent 3cff4e1 commit e9dbbc2

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

clippy_lints/src/swap.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::is_type_diagnostic_item;
5-
use clippy_utils::{any_parent_has_attr, can_mut_borrow_both, differing_macro_contexts, eq_expr_value};
5+
use clippy_utils::{can_mut_borrow_both, differing_macro_contexts, eq_expr_value, is_no_core_crate, is_no_std_crate};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
@@ -113,9 +113,9 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
113113

114114
let first = Sugg::hir_with_applicability(cx, e1, "..", &mut applicability);
115115
let second = Sugg::hir_with_applicability(cx, e2, "..", &mut applicability);
116-
let sugg = if !any_parent_has_attr(cx.tcx, e1.hir_id, sym::no_std) {
116+
let sugg = if !is_no_std_crate(cx) {
117117
"std"
118-
} else if any_parent_has_attr(cx.tcx, e1.hir_id, sym::no_core) {
118+
} else if is_no_core_crate(cx) {
119119
""
120120
} else {
121121
"core"
@@ -199,9 +199,9 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
199199
};
200200

201201
let span = first.span.to(second.span);
202-
let sugg = if !any_parent_has_attr(cx.tcx, block.hir_id, sym::no_std) {
202+
let sugg = if !is_no_std_crate(cx) {
203203
"std"
204-
} else if any_parent_has_attr(cx.tcx, block.hir_id, sym::no_core) {
204+
} else if is_no_core_crate(cx) {
205205
""
206206
} else {
207207
"core"

clippy_utils/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,16 @@ pub fn is_no_std_crate(cx: &LateContext<'_>) -> bool {
18561856
})
18571857
}
18581858

1859+
pub fn is_no_core_crate(cx: &LateContext<'_>) -> bool {
1860+
cx.tcx.hir().attrs(hir::CRATE_HIR_ID).iter().any(|attr| {
1861+
if let ast::AttrKind::Normal(ref attr, _) = attr.kind {
1862+
attr.path == sym::no_core
1863+
} else {
1864+
false
1865+
}
1866+
})
1867+
}
1868+
18591869
/// Check if parent of a hir node is a trait implementation block.
18601870
/// For example, `f` in
18611871
/// ```rust,ignore

0 commit comments

Comments
 (0)