Skip to content

Commit 58bc1fa

Browse files
committed
Add std_or_core util
1 parent 35db700 commit 58bc1fa

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

clippy_lints/src/swap.rs

Lines changed: 3 additions & 23 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::{can_mut_borrow_both, differing_macro_contexts, eq_expr_value, is_no_core_crate, is_no_std_crate};
5+
use clippy_utils::{can_mut_borrow_both, differing_macro_contexts, eq_expr_value, std_or_core};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
@@ -113,17 +113,7 @@ 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 !is_no_std_crate(cx) {
117-
"std"
118-
} else if is_no_core_crate(cx) {
119-
""
120-
} else {
121-
"core"
122-
};
123-
124-
if sugg.is_empty() {
125-
return;
126-
}
116+
let Some(sugg) = std_or_core(cx) else { return };
127117

128118
span_lint_and_then(
129119
cx,
@@ -199,17 +189,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
199189
};
200190

201191
let span = first.span.to(second.span);
202-
let sugg = if !is_no_std_crate(cx) {
203-
"std"
204-
} else if is_no_core_crate(cx) {
205-
""
206-
} else {
207-
"core"
208-
};
209-
210-
if sugg.is_empty() {
211-
return;
212-
}
192+
let Some(sugg) = std_or_core(cx) else { return };
213193

214194
span_lint_and_then(cx,
215195
ALMOST_SWAPPED,

clippy_utils/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,16 @@ pub fn is_expr_final_block_expr(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
18091809
matches!(get_parent_node(tcx, expr.hir_id), Some(Node::Block(..)))
18101810
}
18111811

1812+
pub fn std_or_core(cx: &LateContext<'_>) -> Option<&'static str> {
1813+
if !is_no_std_crate(cx) {
1814+
Some("std")
1815+
} else if !is_no_core_crate(cx) {
1816+
Some("core")
1817+
} else {
1818+
None
1819+
}
1820+
}
1821+
18121822
pub fn is_no_std_crate(cx: &LateContext<'_>) -> bool {
18131823
cx.tcx.hir().attrs(hir::CRATE_HIR_ID).iter().any(|attr| {
18141824
if let ast::AttrKind::Normal(ref attr, _) = attr.kind {

0 commit comments

Comments
 (0)