Skip to content

Commit 8389df9

Browse files
committed
Auto merge of rust-lang#7877 - dswij:no-std-fp, r=camsteffen
[`swap`] lints now check if there is `no_std` or `no_core` attribute Closes rust-lang#7858 changelog: [`swap`] lints now check if there is `no_std` or `no_core` attribute
2 parents 2e17035 + dcd1a16 commit 8389df9

File tree

5 files changed

+87
-24
lines changed

5 files changed

+87
-24
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: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ pub fn recurse_or_patterns<'tcx, F: FnMut(&'tcx Pat<'tcx>)>(pat: &'tcx Pat<'tcx>
14021402
/// Checks for the `#[automatically_derived]` attribute all `#[derive]`d
14031403
/// implementations have.
14041404
pub fn is_automatically_derived(attrs: &[ast::Attribute]) -> bool {
1405-
attrs.iter().any(|attr| attr.has_name(sym::automatically_derived))
1405+
has_attr(attrs, sym::automatically_derived)
14061406
}
14071407

14081408
/// Remove blocks around an expression.
@@ -1524,20 +1524,29 @@ pub fn clip(tcx: TyCtxt<'_>, u: u128, ity: rustc_ty::UintTy) -> u128 {
15241524
(u << amt) >> amt
15251525
}
15261526

1527-
pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_>, node: HirId) -> bool {
1527+
pub fn has_attr(attrs: &[ast::Attribute], symbol: Symbol) -> bool {
1528+
attrs.iter().any(|attr| attr.has_name(symbol))
1529+
}
1530+
1531+
pub fn any_parent_has_attr(tcx: TyCtxt<'_>, node: HirId, symbol: Symbol) -> bool {
15281532
let map = &tcx.hir();
15291533
let mut prev_enclosing_node = None;
15301534
let mut enclosing_node = node;
15311535
while Some(enclosing_node) != prev_enclosing_node {
1532-
if is_automatically_derived(map.attrs(enclosing_node)) {
1536+
if has_attr(map.attrs(enclosing_node), symbol) {
15331537
return true;
15341538
}
15351539
prev_enclosing_node = Some(enclosing_node);
15361540
enclosing_node = map.get_parent_item(enclosing_node);
15371541
}
1542+
15381543
false
15391544
}
15401545

1546+
pub fn any_parent_is_automatically_derived(tcx: TyCtxt<'_>, node: HirId) -> bool {
1547+
any_parent_has_attr(tcx, node, sym::automatically_derived)
1548+
}
1549+
15411550
/// Matches a function call with the given path and returns the arguments.
15421551
///
15431552
/// Usage:
@@ -1800,6 +1809,16 @@ pub fn is_expr_final_block_expr(tcx: TyCtxt<'_>, expr: &Expr<'_>) -> bool {
18001809
matches!(get_parent_node(tcx, expr.hir_id), Some(Node::Block(..)))
18011810
}
18021811

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+
18031822
pub fn is_no_std_crate(cx: &LateContext<'_>) -> bool {
18041823
cx.tcx.hir().attrs(hir::CRATE_HIR_ID).iter().any(|attr| {
18051824
if let ast::AttrKind::Normal(ref attr, _) = attr.kind {
@@ -1810,6 +1829,16 @@ pub fn is_no_std_crate(cx: &LateContext<'_>) -> bool {
18101829
})
18111830
}
18121831

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+
18131842
/// Check if parent of a hir node is a trait implementation block.
18141843
/// For example, `f` in
18151844
/// ```rust,ignore
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![no_std]
2+
#![feature(lang_items, start, libc)]
3+
#![crate_type = "lib"]
4+
5+
use core::panic::PanicInfo;
6+
7+
#[warn(clippy::all)]
8+
fn main() {
9+
// TODO: do somethjing with swap
10+
let mut a = 42;
11+
let mut b = 1337;
12+
13+
a = b;
14+
b = a;
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: this looks like you are trying to swap `a` and `b`
2+
--> $DIR/no_std_swap.rs:13:5
3+
|
4+
LL | / a = b;
5+
LL | | b = a;
6+
| |_________^ help: try: `core::mem::swap(&mut a, &mut b)`
7+
|
8+
= note: `-D clippy::almost-swapped` implied by `-D warnings`
9+
= note: or maybe you should use `core::mem::replace`?
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)