Skip to content

Commit 2d037aa

Browse files
committed
Check for no_std and no_core attribute in swap lint
This commit adds a `no_std` and `no_core` check on `swap` lint and additionally suggest `core::mem::swap` whenever possible. Remove warning if both `std` and `core` is not present.
1 parent 847a95b commit 2d037aa

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(rustc_private)]
99
#![feature(stmt_expr_attributes)]
1010
#![feature(control_flow_enum)]
11+
#![feature(let_else)]
1112
#![recursion_limit = "512"]
1213
#![cfg_attr(feature = "deny-warnings", deny(warnings))]
1314
#![allow(clippy::missing_docs_in_private_items, clippy::must_use_candidate)]

clippy_lints/src/swap.rs

Lines changed: 27 additions & 21 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};
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,6 +113,8 @@ 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 Some(sugg) = std_or_core(cx) else { return };
117+
116118
span_lint_and_then(
117119
cx,
118120
MANUAL_SWAP,
@@ -122,11 +124,11 @@ fn generate_swap_warning(cx: &LateContext<'_>, e1: &Expr<'_>, e2: &Expr<'_>, spa
122124
diag.span_suggestion(
123125
span,
124126
"try",
125-
format!("std::mem::swap({}, {})", first.mut_addr(), second.mut_addr()),
127+
format!("{}::mem::swap({}, {})", sugg, first.mut_addr(), second.mut_addr()),
126128
applicability,
127129
);
128130
if !is_xor_based {
129-
diag.note("or maybe you should use `std::mem::replace`?");
131+
diag.note(&format!("or maybe you should use `{}::mem::replace`?", sugg));
130132
}
131133
},
132134
);
@@ -187,26 +189,30 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
187189
};
188190

189191
let span = first.span.to(second.span);
192+
let Some(sugg) = std_or_core(cx) else { return };
190193

191194
span_lint_and_then(cx,
192-
ALMOST_SWAPPED,
193-
span,
194-
&format!("this looks like you are trying to swap{}", what),
195-
|diag| {
196-
if !what.is_empty() {
197-
diag.span_suggestion(
198-
span,
199-
"try",
200-
format!(
201-
"std::mem::swap({}, {})",
202-
lhs,
203-
rhs,
204-
),
205-
Applicability::MaybeIncorrect,
206-
);
207-
diag.note("or maybe you should use `std::mem::replace`?");
208-
}
209-
});
195+
ALMOST_SWAPPED,
196+
span,
197+
&format!("this looks like you are trying to swap{}", what),
198+
|diag| {
199+
if !what.is_empty() {
200+
diag.span_suggestion(
201+
span,
202+
"try",
203+
format!(
204+
"{}::mem::swap({}, {})",
205+
sugg,
206+
lhs,
207+
rhs,
208+
),
209+
Applicability::MaybeIncorrect,
210+
);
211+
diag.note(
212+
&format!("or maybe you should use `{}::mem::replace`?", sugg)
213+
);
214+
}
215+
});
210216
}
211217
}
212218
}

clippy_utils/src/lib.rs

Lines changed: 20 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 {
@@ -1819,6 +1829,16 @@ pub fn is_no_std_crate(cx: &LateContext<'_>) -> bool {
18191829
})
18201830
}
18211831

1832+
pub fn is_no_core_crate(cx: &LateContext<'_>) -> bool {
1833+
cx.tcx.hir().attrs(hir::CRATE_HIR_ID).iter().any(|attr| {
1834+
if let ast::AttrKind::Normal(ref attr, _) = attr.kind {
1835+
attr.path == sym::no_core
1836+
} else {
1837+
false
1838+
}
1839+
})
1840+
}
1841+
18221842
/// Check if parent of a hir node is a trait implementation block.
18231843
/// For example, `f` in
18241844
/// ```rust,ignore

0 commit comments

Comments
 (0)